package aimod

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type AiChatTopic struct {
	AiChatTopicId   int `orm:"column(ai_chat_topic_id);pk"`
	TopicName       string
	SysUserId       int
	SysUserRealName 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
	CreateTime      time.Time
	ModifyTime      time.Time
}

type ChatReq struct {
	AiChatTopicId int    `description:"主题id"`
	Model         string `description:"模型:gpt-3.5-turbo,eta,gpt-3.5-turbo-16k"`
	Ask           string `description:"提问"`
}

func GetAiChatByAsk(askUuid, model string) (item *AiChat, err error) {
	sql := `SELECT * FROM ai_chat WHERE ask_uuid=? AND model =? `
	o := orm.NewOrm()
	err = o.Raw(sql, askUuid, model).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.NewOrm()
	lastId, err = o.Insert(item)
	return
}

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

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

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.NewOrm()
	_, 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
	CreateTime    string `description:"创建时间"`
	ModifyTime    string `description:"修改时间"`
}

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.NewOrm()
	_, 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.NewOrm()
	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.NewOrm()
	err = o.Raw(sql, topicName).QueryRow(&item)
	return
}

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