package entity

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

// GetChatMessageDB 获取ChatMessage存储
func GetChatMessageDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return getDBWithModel(ctx, defDB, ChatMessage{})
}

// SchemaChatMessage ChatMessage对象
type SchemaChatMessage schema.ChatMessage

// ToChatMessage 转换为ChatMessage实体
func (a SchemaChatMessage) ToChatMessage() *ChatMessage {
	item := &ChatMessage{
		RecordID:     a.RecordID,
		UserId:       a.UserId,
		AssistantId:  a.AssistantId,
		SessionId:    a.SessionId,
		RagSessionId: a.RagSessionId,
		Question:     a.Question,
		Answer:       a.Answer,
		CreatorId:    a.CreatorId,
	}
	return item
}

// ToSchemaChatMessage 转换为ChatMessage对象
func (a ChatMessage) ToSchemaChatMessage() *schema.ChatMessage {
	item := &schema.ChatMessage{
		RecordID:     a.RecordID,
		UserId:       a.UserId,
		AssistantId:  a.AssistantId,
		SessionId:    a.SessionId,
		RagSessionId: a.RagSessionId,
		Question:     a.Question,
		Answer:       a.Answer,
		CreatorId:    a.CreatorId,
		CreatedAt:    a.CreatedAt,
	}
	return item
}

// ChatMessage ChatMessage实体
type ChatMessage struct {
	gorm.Model
	RecordID     string `gorm:"column:record_id;size:32;index;"`  //  记录id
	UserId       string `gorm:"column:user_id;size:32;"`          // 用户id
	AssistantId  string `gorm:"column:assistant_id;size:32;"`     // 助手id
	SessionId    string `gorm:"column:session_id;size:32;index;"` // 会话id
	RagSessionId string `gorm:"column:rag_session_id;size:32;"`   // rag_session_id
	Question     string `gorm:"column:question;type:longtext;"`   // 对话问题
	Answer       string `gorm:"column:answer;type:longtext;"`     // 对话答案
	CreatorId    string `gorm:"column:creator_id;size:32;index;"` // 创建者id
}

// ChatMessages ChatMessage列表
type ChatMessages []*ChatMessage

// ToSchemaChatMessages 转换为ChatMessage对象列表
func (a ChatMessages) ToSchemaChatMessages() []*schema.ChatMessage {
	list := make([]*schema.ChatMessage, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaChatMessage()
	}
	return list
}
