package model

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

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

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

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

// Query 查询数据
func (a *ChatDataset) Query(ctx context.Context, params schema.ChatDatasetQueryParam, opts ...schema.ChatDatasetQueryOptions) (*schema.ChatDatasetQueryResult, error) {
	db := entity.GetChatDatasetDB(ctx, a.db)
	if v := params.ChatAssistantId; v != "" {
		db = db.Where("chat_assistant_id=?", v)
	}

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

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

	return qr, nil
}

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

	return item.ToSchemaChatDataset(), nil
}

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

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

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

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

// DeleteByAssistantId 按助手id删除
func (a *ChatDataset) DeleteByAssistantId(ctx context.Context, assistantId string) error {
	result := entity.GetChatDatasetDB(ctx, a.db).Where("chat_assistant_id=?", assistantId).Delete(entity.ChatDataset{})
	if err := result.Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}
