package advice

import (
	"ai_scheduler/internal/biz"
	"ai_scheduler/internal/config"
	errorcode "ai_scheduler/internal/data/error"
	"ai_scheduler/internal/pkg"

	"ai_scheduler/internal/data/mongo_model"
	"ai_scheduler/internal/entitys"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/log"
)

// FileService 文件处理
type ChatService struct {
	adviceChatBiz    *biz.AdviceChatBiz
	adviceClientBiz  *biz.AdviceClientBiz
	adviceAdvicerBiz *biz.AdviceAdvicerBiz
	adviceProjectBiz *biz.AdviceProjectBiz
	adviceSkillBiz   *biz.AdviceSkillBiz
	cfg              *config.Config
}

// NewFileService
func NewChatService(
	adviceChatBiz *biz.AdviceChatBiz,
	adviceClientBiz *biz.AdviceClientBiz,
	adviceAdvicerBiz *biz.AdviceAdvicerBiz,
	adviceProjectBiz *biz.AdviceProjectBiz,
	adviceSkillBiz *biz.AdviceSkillBiz,
	cfg *config.Config,
) *ChatService {
	return &ChatService{
		adviceChatBiz:    adviceChatBiz,
		cfg:              cfg,
		adviceClientBiz:  adviceClientBiz,
		adviceAdvicerBiz: adviceAdvicerBiz,
		adviceProjectBiz: adviceProjectBiz,
		adviceSkillBiz:   adviceSkillBiz,
	}
}

func (a *ChatService) Regis(c *fiber.Ctx) error {
	req := &entitys.AdvicerChatRegistReq{}
	if err := c.BodyParser(req); err != nil {
		return err
	}
	if len(req.AdvicerVersionId) == 0 {
		return errorcode.ParamErr("AdvicerVersionId is empty")
	}
	if len(req.TalkSkillId) == 0 {
		return errorcode.ParamErr("talkSkillId is empty")
	}
	if len(req.Mission) == 0 {
		return errorcode.ParamErr("misiion is empty")
	}

	//顾问版本信息
	versionInfo, err := a.adviceAdvicerBiz.VersionInfo(c.UserContext(), &entitys.AdvicerVersionInfoReq{
		Id: req.AdvicerVersionId,
	})
	if err != nil {
		return err
	}

	//顾问信息
	advicerInfo, err := a.adviceAdvicerBiz.AdvicerInfo(c.UserContext(), &entitys.AdvicerInfoReq{
		AdvicerID: versionInfo.AdvicerId,
	})
	if err != nil {
		return err
	}

	//项目信息
	projectInfo, err := a.adviceProjectBiz.Info(c.UserContext(), &entitys.AdvicerProjectInfoReq{
		ProjectId: advicerInfo.ProjectID,
	})
	if err != nil {
		return err
	}

	if len(projectInfo.ModelInfo.Key) == 0 {
		return errorcode.ParamErr("项目未设置模型key")
	}

	if len(projectInfo.ModelInfo.ChatModel) == 0 {
		return errorcode.ParamErr("项目未设置模型对话模型")
	}

	//销售技巧
	talkSkill, err := a.adviceSkillBiz.Info(c.UserContext(), &entitys.AdvicerTalkSkillInfoReq{
		Id: req.TalkSkillId,
	})
	if err != nil {
		return err
	}
	//客户信息
	var clientInfo mongo_model.AdvicerClientMongo
	if len(req.ClientId) != 0 {

		clientInfo, err = a.adviceClientBiz.Info(c.UserContext(), &entitys.AdvicerClientInfoReq{
			Id: req.ClientId,
		})
		if err != nil {
			return err
		}
	}
	chat := entitys.ChatData{
		ClientInfo:     clientInfo.Entity(),
		TalkSkill:      talkSkill.Entity(),
		ProjectInfo:    projectInfo.ConfigInfo.Entity(),
		AdvicerInfo:    advicerInfo.Entity(),
		AdvicerVersion: versionInfo.Entity(),
	}
	sessionId, err := a.adviceChatBiz.Regis(c.UserContext(), &chat, req, projectInfo)

	log.Info(sessionId)
	return pkg.HandleResponse(c, sessionId, err)
}

func (a *ChatService) Chat(c *fiber.Ctx) error {
	req := &entitys.AdvicerChatReq{}
	if err := c.BodyParser(req); err != nil {
		return err
	}
	if len(req.SessionId) == 0 {
		return errorcode.ParamErr("SessionId is empty")
	}
	if len(req.Content) == 0 {
		return errorcode.ParamErr("Content is empty")
	}
	res, err := a.adviceChatBiz.Chat(c.UserContext(), req)
	log.Info(res)
	return pkg.HandleResponse(c, res, err)
}

func (a *ChatService) ChatContext(c *fiber.Ctx) error {
	req := &entitys.AdvicerChatReq{}
	if err := c.BodyParser(req); err != nil {
		return err
	}
	if len(req.SessionId) == 0 {
		return errorcode.ParamErr("SessionId is empty")
	}
	if len(req.Content) == 0 {
		return errorcode.ParamErr("Content is empty")
	}
	res, err := a.adviceChatBiz.Chat(c.UserContext(), req)
	log.Info(res)
	return pkg.HandleResponse(c, res, err)
}
