package model

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

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

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

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

// Query 查询数据
func (a *ChatMessage) Query(ctx context.Context, params schema.ChatMessageQueryParam, opts ...schema.ChatMessageQueryOptions) (*schema.ChatMessageQueryResult, error) {
	db := entity.GetChatMessageDB(ctx, a.db)
	if v := params.AssistantId; v != "" {
		db = db.Where("assistant_id = ?", v)
	}

	if v := params.SessionId; v != "" {
		db = db.Where("session_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)
	}
	db = db.Order("id ASC")

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

	return qr, nil
}

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

	return item.ToSchemaChatMessage(), nil
}

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

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

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

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