package model

import (
	"context"
	"gorm.io/gorm"
	"yx-dataset-server/app/errors"
	"yx-dataset-server/app/model/entity"
	"yx-dataset-server/app/schema"
)

// NewChatAssistant 创建ChatAssistant存储实例
func NewChatAssistant(db *gorm.DB) *ChatAssistant {
	return &ChatAssistant{db}
}

// ChatAssistant ChatAssistant存储
type ChatAssistant struct {
	db *gorm.DB
}

func (a *ChatAssistant) getQueryOption(opts ...schema.ChatAssistantQueryOptions) schema.ChatAssistantQueryOptions {
	var opt schema.ChatAssistantQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}
	return opt
}

// Query 查询数据
func (a *ChatAssistant) Query(ctx context.Context, params schema.ChatAssistantQueryParam, opts ...schema.ChatAssistantQueryOptions) (*schema.ChatAssistantQueryResult, error) {
	db := entity.GetChatAssistantDB(ctx, a.db)
	if v := params.OrgId; v != "" {
		db = db.Where("org_id=?", v)
	}

	if v := params.LikeName; v != "" {
		db = db.Where("name LIKE ?", "%"+v+"%")
	}

	if v := params.RecordIDs; len(v) > 0 {
		db = db.Where("record_id in (?)", v)
	}
	if v := params.CreatorId; v != "" {
		db = db.Where("creator_id=?", v)
	}
	if v := params.UserId; v != "" {
		db = db.Where("user_id=?", v)
	}
	db = db.Order("id DESC")

	opt := a.getQueryOption(opts...)
	var list entity.ChatAssistants
	pr, err := WrapPageQuery(ctx, db, opt.PageParam, &list)
	if err != nil {
		return nil, errors.ErrDBServerInternalError
	}
	qr := &schema.ChatAssistantQueryResult{
		PageResult: pr,
		Data:       list.ToSchemaChatAssistants(),
	}

	return qr, nil
}

// Get 查询指定数据
func (a *ChatAssistant) Get(ctx context.Context, recordID string, opts ...schema.ChatAssistantQueryOptions) (*schema.ChatAssistant, error) {
	db := entity.GetChatAssistantDB(ctx, a.db).Where("record_id=?", recordID)
	var item entity.ChatAssistant
	ok, err := FindOne(ctx, db, &item)
	if err != nil {
		return nil, errors.ErrDBServerInternalError
	} else if !ok {
		return nil, nil
	}

	return item.ToSchemaChatAssistant(), nil
}

// Create 创建数据
func (a *ChatAssistant) Create(ctx context.Context, item schema.ChatAssistant) error {
	ChatAssistant := entity.SchemaChatAssistant(item).ToChatAssistant()
	result := entity.GetChatAssistantDB(ctx, a.db).Create(ChatAssistant)
	if err := result.Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}

// Update 更新数据
func (a *ChatAssistant) Update(ctx context.Context, recordID string, item schema.ChatAssistant) error {
	ChatAssistant := entity.SchemaChatAssistant(item).ToChatAssistant()
	result := entity.GetChatAssistantDB(ctx, a.db).Where("record_id=?", recordID).Omit("record_id", "creator").Updates(ChatAssistant)
	if err := result.Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}

// Delete 删除数据
func (a *ChatAssistant) Delete(ctx context.Context, recordID string) error {
	result := entity.GetChatAssistantDB(ctx, a.db).Where("record_id=?", recordID).Delete(entity.ChatAssistant{})
	if err := result.Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}

// UpdateStatus 更新状态
func (a *ChatAssistant) UpdateStatus(ctx context.Context, recordID string, status int) error {
	result := entity.GetChatAssistantDB(ctx, a.db).Where("record_id=?", recordID).Update("status", status)
	if err := result.Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}
