package model

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

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

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

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

// Query 查询数据
func (a *ChatSession) Query(ctx context.Context, params schema.ChatSessionQueryParam, opts ...schema.ChatSessionQueryOptions) (*schema.ChatSessionQueryResult, error) {
	db := entity.GetChatSessionDB(ctx, a.db)
	if v := params.AssistantId; v != "" {
		db = db.Where("assistant_id=?", v)
	}

	if v := params.RecordIDs; len(v) > 0 {
		db = db.Where("record_id in (?)", v)
	}
	if v := params.UserId; v != "" {
		db = db.Where("user_id=?", v)
	}
	if v := params.Source; v > 0 {
		db = db.Where("source = ?", v)
	}

	db = db.Order("id DESC")

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

	return qr, nil
}

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

	return item.ToSchemaChatSession(), nil
}

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

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

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

// UpdateStatus 更新状态
func (a *ChatSession) UpdateStatus(ctx context.Context, recordID string, status int) error {
	result := entity.GetChatSessionDB(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 *ChatSession) DeleteByAssistantId(ctx context.Context, assistantId string) error {
	result := entity.GetChatSessionDB(ctx, a.db).Where("assistant_id=?", assistantId).Delete(entity.ChatSession{})
	if err := result.Error; err != nil {
		return errors.ErrDBServerInternalError
	}
	return nil
}
