package zltx

import (
	"ai_scheduler/internal/config"
	"ai_scheduler/internal/entitys"
	"ai_scheduler/internal/pkg/l_request"
	"context"
	"encoding/json"
	"fmt"
)

type ZltxOrderAfterSaleDetailTool struct {
	config config.ToolConfig
}

// NewZltxOrderAfterSaleDetailTool 创建售后订单详情工具
func NewZltxOrderAfterSaleDetailTool(config config.ToolConfig) *ZltxOrderAfterSaleDetailTool {
	return &ZltxOrderAfterSaleDetailTool{config: config}
}

// Definition 返回工具定义
func (this *ZltxOrderAfterSaleDetailTool) Definition() entitys.ToolDefinition {
	return entitys.ToolDefinition{
		Type: "function",
		Function: entitys.FunctionDef{
			Name:        this.Name(),
			Description: this.Description(),
			Parameters: map[string]interface{}{
				"type": "object",
				"properties": map[string]interface{}{
					"direct_order_number": map[string]interface{}{
						"type":        "string",
						"description": "售后订单号",
					},
				},
				"required": []string{"direct_order_number"},
			},
		},
	}
}

type ZltxOrderAfterSaleDetailRequest struct {
	DirectOrderNumber string `json:"direct_order_number"`
}

type ZltxOrderAfterSaleDetailResponse struct {
	Code int `json:"code"`
	Data struct {
		AfterSaleOrder ZltxOrderAfterSaleDetailData `json:"afterSaleOrder"`
	} `json:"data"`
	Error string `json:"error"`
}

type ZltxOrderAfterSaleDetailData struct {
	SerialNumber        string  `json:"serialNumber"`
	PlatformName        string  `json:"platformName"`
	SignCompany         int     `json:"signCompany"`
	PlatformProductName string  `json:"platformProductName"`
	PlatformPrice       float64 `json:"platformPrice"`
	TerminalAccount     string  `json:"terminalAccount"`
	Status              int     `json:"status"`
	PlatformProductID   int     `json:"platformProductId"`
	PlatformID          int     `json:"platformId"`
	SignCompanyName     string  `json:"signCompanyName"`
	ExecuteTime         int     `json:"executeTime"`
}

func (this *ZltxOrderAfterSaleDetailTool) Execute(ctx context.Context, requireData *entitys.RequireData) error {
	var req ZltxOrderAfterSaleDetailRequest
	if err := json.Unmarshal([]byte(requireData.Match.Parameters), &req); err != nil {
		return err
	}
	if req.DirectOrderNumber == "" {
		return fmt.Errorf("direct_order_number is required")
	}
	return this.getZltxOrderAfterSaleDetail(ctx, requireData, req.DirectOrderNumber)
}

func (this *ZltxOrderAfterSaleDetailTool) getZltxOrderAfterSaleDetail(ctx context.Context, requireData *entitys.RequireData, directOrderNumber string) error {
	//查询订单详情
	url := fmt.Sprintf("%s%s", this.config.BaseURL, directOrderNumber)
	req := l_request.Request{
		Url: url,
		Headers: map[string]string{
			"Authorization": fmt.Sprintf("Bearer %s", requireData.Auth),
		},
		Method: "GET",
	}
	res, err := req.Send()
	var resData ZltxOrderAfterSaleDetailResponse
	if err != nil {
		return err
	}
	if err := json.Unmarshal(res.Content, &resData); err != nil {
		return err
	}
	if resData.Code != 200 {
		return fmt.Errorf("为获取到数据，请检查权限: %s", string(res.Content))
	}
	jsonByte, err := json.Marshal(resData)
	if err != nil {
		return err
	}
	entitys.ResJson(requireData.Ch, this.Name(), string(jsonByte))
	return nil
}

func (this *ZltxOrderAfterSaleDetailTool) GetConfig() config.ToolConfig {
	return this.config
}

// Name 工具名称
func (this *ZltxOrderAfterSaleDetailTool) Name() string {
	return "zltx_order_after_sale_detail"
}

func (this *ZltxOrderAfterSaleDetailTool) Description() string {
	return "查询直连天下上游售后订单详情"
}
