package aimod

import (
	"time"

	"github.com/beego/beego/v2/client/orm"
)

type AiChatTopic struct {
	AiChatTopicId   int `orm:"column(ai_chat_topic_id);pk"`
	TopicName       string
	SysUserId       int
	SysUserRealName string
	AssistantId     string
	ThreadId        string
	CreateTime      time.Time
	ModifyTime      time.Time
}

type AiChat struct {
	AiChatId        int `orm:"column(ai_chat_id);pk"`
	AiChatTopicId   int
	Ask             string
	AskUuid         string
	Answer          string
	Model           string
	SysUserId       int
	SysUserRealName string `description:"提问人名称"`
	OpenaiFileId    string `description:"openai返回的文件id"`
	OpenaiFileName  string `description:"文件名称"`
	OpenaiFilePath  string `description:"文件路径"`
	CreateTime      time.Time
	ModifyTime      time.Time
}

type ChatReq struct {
	AiChatTopicId int    `description:"主题id"`
	Ask           string `description:"提问"`
	Model         string `description:"模型"`
}

func GetAiChatByAsk(askUuid string) (item *AiChat, err error) {
	sql := `SELECT * FROM ai_chat WHERE ask_uuid=?`
	o := orm.NewOrmUsingDB("ai")
	err = o.Raw(sql, askUuid).QueryRow(&item)
	return
}

type ChatResp struct {
	AiChatTopicId int    `description:"主题id"`
	Ask           string `description:"提问"`
	Answer        string `description:"回答"`
	Model         string
}

// AddAiChatTopic 新增主题
func AddAiChatTopic(item *AiChatTopic) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("ai")
	lastId, err = o.Insert(item)
	return
}

// AddAiChat 新增聊天
func AddAiChat(item *AiChat) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("ai")
	lastId, err = o.Insert(item)
	return
}

type AiChatTopicView struct {
	AiChatTopicId int    `description:"主题id"`
	TopicName     string `description:"主题名称"`
	CreateTime    string `description:"创建时间"`
	ModifyTime    string `description:"修改时间"`
	AssistantId   string
	ThreadId      string
}

func GetAiChatTopicList(sysUserId int) (item []*AiChatTopicView, err error) {
	sql := ` SELECT * FROM ai_chat_topic WHERE sys_user_id=? ORDER BY create_time DESC `
	o := orm.NewOrmUsingDB("ai")
	_, err = o.Raw(sql, sysUserId).QueryRows(&item)
	return
}

type AiChatTopicListResp struct {
	List []*AiChatTopicView
}

type AiChatView struct {
	AiChatId       int    `description:"记录id"`
	AiChatTopicId  int    `description:"主题id"`
	Ask            string `description:"提问"`
	Answer         string `description:"答案"`
	Model          string
	OpenaiFileId   string `description:"文件ID"`
	OpenaiFileName string `description:"文件名称"`
	OpenaiFilePath string `description:"文件路径"`
	CreateTime     string `description:"创建时间"`
	ModifyTime     string `description:"修改时间"`
}

var ModelViewMap = map[string]string{
	"gpt-4-1106-preview": "GPT-4 Turbo",
	"moonshot-v1-32k":    "Kimi",
	// "moonshot-v1-8k":     "Kimi",
}

func GetAiChatList(aiChatTopicId int) (item []*AiChatView, err error) {
	sql := ` SELECT * FROM ai_chat WHERE ai_chat_topic_id=? ORDER BY create_time ASC `
	o := orm.NewOrmUsingDB("ai")
	_, err = o.Raw(sql, aiChatTopicId).QueryRows(&item)
	return
}

type AiChatDetailResp struct {
	List []*AiChatView
}

type TopicDeleteReq struct {
	AiChatTopicId int `description:"主题id"`
}

func DeleteTopic(topicId int) (err error) {
	o := orm.NewOrmUsingDB("ai")
	tx, err := o.Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()
	sql := ` DELETE FROM ai_chat_topic WHERE  ai_chat_topic_id=? `
	_, err = tx.Raw(sql, topicId).Exec()
	if err != nil {
		return err
	}
	sql = ` DELETE FROM ai_chat WHERE  ai_chat_topic_id=? `
	_, err = tx.Raw(sql, topicId).Exec()
	if err != nil {
		return err
	}
	return err
}

type TopicEditReq struct {
	AiChatTopicId int    `description:"主题id"`
	TopicName     string `description:"主题名称"`
}

func GetAiChatTopicByTopicName(topicName string) (item *AiChatTopicView, err error) {
	sql := ` SELECT * FROM ai_chat_topic WHERE topic_name=? `
	o := orm.NewOrmUsingDB("ai")
	err = o.Raw(sql, topicName).QueryRow(&item)
	return
}

func EditTopic(topicId int, topicName string) (err error) {
	o := orm.NewOrmUsingDB("ai")
	sql := ` UPDATE ai_chat_topic SET topic_name=? WHERE  ai_chat_topic_id=? `
	_, err = o.Raw(sql, topicName, topicId).Exec()
	return err
}

func (obj *AiChatTopic) GetAiChatTopicById() (item *AiChatTopicView, err error) {
	o := orm.NewOrmUsingDB("ai")
	sql := ` SELECT * FROM ai_chat_topic WHERE ai_chat_topic_id=? `
	err = o.Raw(sql, obj.AiChatTopicId).QueryRow(&item)
	return
}

type HistoryChat struct {
	Ask          string
	Answer       string
	OpenaiFileId []string `description:"文件ID"`
}

// 修改
func (obj *AiChatTopic) Update(updateParams, whereParam map[string]interface{}) (err error) {
	to := orm.NewOrmUsingDB("ai")
	ptrStructOrTableName := "ai_chat_topic"

	qs := to.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	return
}
