package advice

import (
	"ai_scheduler/internal/biz"
	"ai_scheduler/internal/biz/llm_service/third_party"
	"ai_scheduler/internal/config"
	"ai_scheduler/internal/data/impl"
	"ai_scheduler/internal/data/mongo_model"
	"ai_scheduler/internal/pkg"
	"ai_scheduler/utils"
	"context"

	"encoding/json"

	"os"
	"testing"

	"github.com/gofiber/fiber/v2"
	"github.com/valyala/fasthttp"
)

func Test_WordAna(t *testing.T) {
	Run(context.Background(), nil)
	ana, err := file.WordAnat("https://attachment-public.oss-cn-hangzhou.aliyuncs.com/ai-scheduler/data-analytics/word/content2.docx")
	t.Log(ana, err)
}

func Test_AdvicerInit(t *testing.T) {
	reqBody := `{"advicer_id": 124, "name": "张三111", "birth": "1990-01-01", "gender": 1, "working_years": 10}`
	Run(context.Background(), []byte(reqBody))

	err := advicer.AdvicerUpdate(fiberCtx)
	t.Log(err)
}

func Test_AdvicerVersionAdd(t *testing.T) {
	reqBody := `{"advicerId":124,"versionDesc":"第三个版本","dialectFeatures":{"region":"四川成都话","intensity":0.6,"KeyWords":null},"personalityTags":["耐心细致","专业务实","经验丰富","善于引导"],"sentencePatterns":{"openingMode":["我给你介绍一下","我们先来看一下","这边请"],"explanationMode":["是这样的","我跟你讲","你发现没得","说白了"],"confirmationMode":["对吧？","是不是嘛？","你晓得不？","明白了噻？","对不对？"],"summaryMode":["所以说","简单说就是","其实"],"transitionMode":["然后的话","再其次","还有一点","另外"]},"signatureDialogues":[{"context":"客户质疑地块大小","dialogue":"哥，14亩确实不大，但你要在成都2.5环内城买房，这种是普遍现象。你看万景和绿城都是13亩，中铁建只有8.8亩，339那个帮泰只有11亩。我们虽然地小，但楼间距开阔啊，看过去都是200多米！而且小小区人少安静，圈层更纯粹！"},{"context":"客户担心物业费高","dialogue":"姐，我懂你意思，我们也觉得物业费是有点贵。但招商物业是铂金服务，有夜间送外卖、免费宠物喂养、年度保洁这些增值服务。而且前三年开发商补贴一块钱，只需要交5块，跟其他盘差不多！好物业能让房子后期保值增值更多！"},{"context":"客户犹豫价格","dialogue":"说实话，这个地段的地价都比二八板块贵5000多，但我们单价只贵3000。你看龙湖滨江云河颂套内单价都36000了，我们才33000，真的性价比高！现在不买，以后这个板块可能就买不起了。"},{"context":"客户担心小区小不保值","dialogue":"哥，你不用担心小地块不保值，东大街的九龙仓擎天半岛只有两栋楼，现在二手房还能卖3万左右，是当年的豪宅项目。还有望江名门、仁和春天29号院，都是小地块但照样是高端保值盘。核心还是地段，我们在槐树店这个成华区最贵的板块，保值根本没问题！"}],"toneTags":{"enthusiasm":0.8,"patience":0.9,"confidence":0.85,"friendliness":0.8,"persuasion":0.75}}`
	Run(context.Background(), []byte(reqBody))
	err := advicer.AdvicerVersionAdd(fiberCtx)
	t.Log(err)
}

func Test_AdvicerVersionUpdate(t *testing.T) {
	reqBody := `{"id":"69804b5a6532131383aeda3a","advicerId":124,"versionDesc":"第三个版本","dialectFeatures":{"region":"四川成都话","intensity":0.6,"KeyWords":null},"personalityTags":["耐心细致","专业务实","经验丰富","善于引导"],"sentencePatterns":{"openingMode":["我给你介绍一下","我们先来看一下","这边请"],"explanationMode":["是这样的","我跟你讲","你发现没得","说白了"],"confirmationMode":["对吧？","是不是嘛？","你晓得不？","明白了噻？","对不对？"],"summaryMode":["所以说","简单说就是","其实"],"transitionMode":["然后的话","再其次","还有一点","另外"]},"signatureDialogues":[{"context":"客户质疑地块大小","dialogue":"哥，14亩确实不大，但你要在成都2.5环内城买房，这种是普遍现象。你看万景和绿城都是13亩，中铁建只有8.8亩，339那个帮泰只有11亩。我们虽然地小，但楼间距开阔啊，看过去都是200多米！而且小小区人少安静，圈层更纯粹！"},{"context":"客户担心物业费高","dialogue":"姐，我懂你意思，我们也觉得物业费是有点贵。但招商物业是铂金服务，有夜间送外卖、免费宠物喂养、年度保洁这些增值服务。而且前三年开发商补贴一块钱，只需要交5块，跟其他盘差不多！好物业能让房子后期保值增值更多！"},{"context":"客户犹豫价格","dialogue":"说实话，这个地段的地价都比二八板块贵5000多，但我们单价只贵3000。你看龙湖滨江云河颂套内单价都36000了，我们才33000，真的性价比高！现在不买，以后这个板块可能就买不起了。"},{"context":"客户担心小区小不保值","dialogue":"哥，你不用担心小地块不保值，东大街的九龙仓擎天半岛只有两栋楼，现在二手房还能卖3万左右，是当年的豪宅项目。还有望江名门、仁和春天29号院，都是小地块但照样是高端保值盘。核心还是地段，我们在槐树店这个成华区最贵的板块，保值根本没问题！"}],"toneTags":{"enthusiasm":0.8,"patience":0.9,"confidence":0.85,"friendliness":0.8,"persuasion":0.75}}`
	Run(context.Background(), []byte(reqBody))
	err := advicer.AdvicerVersionUpdate(fiberCtx)
	t.Log(err)
}

func Test_VersionList(t *testing.T) {
	reqBody := `{"id":"69804060c17976e5e21858a8"}`
	Run(context.Background(), []byte(reqBody))
	err := advicer.AdvicerVersionList(fiberCtx)
	t.Log(err)
}

func Test_AdvicerVersionDel(t *testing.T) {
	reqBody := `{"id":"698056073059550befc4f0da"}`
	Run(context.Background(), []byte(reqBody))
	err := advicer.AdvicerVersionDel(fiberCtx)
	t.Log(err)
}

func Test_Json(t *testing.T) {
	responseByte, err := os.ReadFile("./res.json")
	if err != nil {
		panic(err)
	}
	var (
		result map[string]interface{}
		res    = make(map[string]mongo_model.AdviceData)
	)

	if err = json.Unmarshal(responseByte, &result); err != nil {
		panic(err)
	}
	for k, v := range result {
		if _, ok := dataMap[k]; !ok {
			continue
		}
		var vbyte []byte
		if vbyte, err = json.Marshal(v); err != nil {
			panic(err)
		}
		newData := dataMap[k].Copy()

		if err = json.Unmarshal(vbyte, newData); err != nil {
			panic(err)
		}
		res[k] = newData
	}
	t.Log(result)
}

var (
	file         *FileService
	advicer      *AdvicerService
	configConfig *config.Config
	fiberCtx     *fiber.Ctx
)

// run 函数是程序的入口函数，负责初始化和配置各个组件
func Run(ctx context.Context, reqBody []byte) {
	if reqBody != nil {
		app := fiber.New()
		fctx := &fasthttp.RequestCtx{}
		fctx.Request.Header.SetMethod("POST")
		fctx.Request.SetBody(reqBody)
		fctx.Request.Header.SetContentType("application/json")
		fiberCtx = app.AcquireCtx(fctx)
	}
	configConfig, _ = config.LoadConfigWithEnv()
	// 初始化数据库连接
	db, _ := utils.NewGormDb(configConfig)
	rdb := utils.NewRdb(configConfig)
	advicerImpl := impl.NewAdviceAdvicerImpl(db)
	advicerVersionMongo := mongo_model.NewAdvicerVersionMongo()
	advicerTalkSkillMongo := mongo_model.NewAdvicerTalkSkillMongo()
	advicerClientMongo := mongo_model.NewAdvicerClientMongo()
	advicerProjectMongo := mongo_model.NewAdvicerProjectMongo()
	hsyq := third_party.NewHsyq()
	advicerfilebiz := biz.NewAdviceFileBiz(hsyq)
	mongo, _ := pkg.NewMongoDb(ctx, configConfig)
	adviceAdvicerBiz := biz.NewAdviceAdvicerBiz(advicerImpl, advicerVersionMongo, mongo)
	skillBiz := biz.NewAdviceSkillBiz(advicerTalkSkillMongo, mongo)
	clientBiz := biz.NewAdviceClientBiz(advicerClientMongo, mongo)
	projectBiz := biz.NewAdviceProjectBiz(advicerProjectMongo, mongo)
	chatBiz := biz.NewAdviceChatBiz(hsyq, rdb)
	file = NewFileService(advicerfilebiz, configConfig)
	advicer = NewAdvicerService(adviceAdvicerBiz, configConfig)
	skill = NewTalkSkillService(skillBiz, configConfig)
	client = NewClientService(clientBiz, configConfig)
	project = NewProjectService(projectBiz, configConfig)
	chat = NewChatService(chatBiz, clientBiz, adviceAdvicerBiz, projectBiz, skillBiz, configConfig)
}

var dataMap = map[string]mongo_model.AdviceData{
	"DialectFeatures":       &mongo_model.DialectFeatures{},
	"SentencePatterns":      &mongo_model.SentencePatterns{},
	"PersonalityTags":       &mongo_model.PersonalityTags{},
	"ToneTags":              &mongo_model.ToneTags{},
	"SignatureDialogues":    &mongo_model.SignatureDialogues{},
	"RegionValue":           &mongo_model.RegionValue{},
	"CompetitionComparison": &mongo_model.CompetitionComparison{},
	"CoreSellingPoints":     &mongo_model.CoreSellingPoints{},
	"SupportingFacilities":  &mongo_model.SupportingFacilities{},
	"DeveloperBacking":      &mongo_model.DeveloperBacking{},
	"NeedsMining":           &mongo_model.NeedsMining{},
	"PainPointResponse":     &mongo_model.PainPointResponse{},
	"ValueBuilding":         &mongo_model.ValueBuilding{},
	"ClosingTechniques":     &mongo_model.ClosingTechniques{},
	"CommunicationRhythm":   &mongo_model.CommunicationRhythm{},
}
