package xiaoice

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"github.com/russross/blackfriday"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/zeromicro/go-zero/core/errorx"
	"io"
	"net/http"
	"net/url"
	"regexp"
	"strconv"
	"strings"
	"wechat-api/internal/svc"
	"wechat-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GptbotsMessageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGptbotsMessageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GptbotsMessageLogic {
	return &GptbotsMessageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx}
}

func (l *GptbotsMessageLogic) GptbotsMessage(req *types.MessageReq) (resp *types.BaseDataInfo, err error) {
	apikey := l.svcCtx.Config.Xiaoice.GptbotsAuthorization
	conversationId, err := l.GetConversation(apikey, strconv.FormatUint(*req.UserId, 10))
	if conversationId == nil || err != nil {
		return nil, err
	}

	baseURL, err := url.Parse("https://api.gptbots.ai/v1/conversation/message")
	if err != nil {
		return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
	}

	// 构建请求体
	requestBody := map[string]string{
		"text":            *req.Text,
		"conversation_id": *conversationId,
		"response_mode":   "blocking",
	}
	jsonBody, err := json.Marshal(requestBody)
	if err != nil {
		return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
	}

	// 创建HTTP请求
	httpReq, err := http.NewRequest("POST", baseURL.String(), bytes.NewBuffer(jsonBody))
	if err != nil {
		return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
	}

	// 添加必要的Header信息
	httpReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apikey))
	httpReq.Header.Set("Content-Type", "application/json")

	// 创建HTTP客户端并执行请求
	client := &http.Client{}
	response, err := client.Do(httpReq)
	if err != nil {
		return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
	}
	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {
			l.Error("生成内容失败: %v", err)
		}
	}(response.Body)

	// 读取和输出响应
	body, err := io.ReadAll(response.Body)
	if err != nil {
		return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
	}

	// 检查响应状态
	if response.StatusCode != http.StatusOK {
		//log.Fatalf("请求失败，状态码：%d，响应: %s", response.StatusCode, string(body))
		return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败：%d，响应: %s", response.StatusCode, string(body)))
	}

	// 解析 JSON 响应
	var responseMap types.GptbotsMessageResp
	if err := json.Unmarshal(body, &responseMap); err != nil {
		return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
	}

	data := ""
	if responseMap.FlowOutput != nil && len(responseMap.FlowOutput) > 0 {
		data = TrimHtml(Markdown2Html(responseMap.FlowOutput[0].Content))
	}

	return &types.BaseDataInfo{Msg: errormsg.Success, Data: data}, nil
}

func (l *GptbotsMessageLogic) GetConversation(apikey string, userId string) (conversationId *string, err error) {
	val, _ := l.svcCtx.Rds.HGet(l.ctx, "xiaoice_conversation", userId).Result()
	if val == "" {
		baseURL, err := url.Parse("https://api.gptbots.ai/v1/conversation")
		if err != nil {
			return nil, err
		}

		// 构建请求体
		requestBody := map[string]string{
			"user_id": userId,
		}
		jsonBody, err := json.Marshal(requestBody)
		if err != nil {
			return nil, err
		}

		// 创建HTTP请求
		req, err := http.NewRequest("POST", baseURL.String(), bytes.NewBuffer(jsonBody))
		if err != nil {
			return nil, err
		}

		// 添加必要的Header信息
		req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apikey))
		req.Header.Set("Content-Type", "application/json")

		// 创建HTTP客户端并执行请求
		client := &http.Client{}
		response, err := client.Do(req)
		if err != nil {
			return nil, err
		}
		defer func(Body io.ReadCloser) {
			err := Body.Close()
			if err != nil {
				l.Error("创建会话失败败: %v", err)
			}
		}(response.Body)

		// 读取和输出响应
		body, err := io.ReadAll(response.Body)
		if err != nil {
			return nil, err
		}

		// 检查响应状态
		if response.StatusCode != http.StatusOK {
			//log.Fatalf("请求失败，状态码：%d，响应: %s", response.StatusCode, string(body))
			return nil, errorx.NewDefaultError(fmt.Sprintf("创建会话失败：%d，响应: %s", response.StatusCode, string(body)))
		}

		// 解析 JSON 响应
		var responseMap types.ConversationResp
		if err := json.Unmarshal(body, &responseMap); err != nil {
			return nil, err
		}
		l.svcCtx.Rds.HSet(l.ctx, "xiaoice_conversation", userId, *responseMap.ConversationId)
		return responseMap.ConversationId, nil
	}
	return &val, nil
}

// TrimHtml 去除HTML标签
func TrimHtml(html string) string {
	//将HTML标签全转换成小写
	re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
	html = re.ReplaceAllStringFunc(html, strings.ToLower)
	//去除STYLE
	re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
	html = re.ReplaceAllString(html, "")
	//去除SCRIPT
	re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
	html = re.ReplaceAllString(html, "")
	//去除所有尖括号内的HTML代码，并换成换行符
	re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
	html = re.ReplaceAllString(html, "\n")
	//去除连续的换行符
	re, _ = regexp.Compile("\\s{2,}")
	html = re.ReplaceAllString(html, "\n")
	return strings.TrimSpace(html)
}

// Markdown2Html Markdown format to HTML format
func Markdown2Html(markdown string) string {
	html := blackfriday.MarkdownCommon([]byte(markdown))
	return string(html)
}
