package goods_category_add

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

type Client struct {
	cfg config.ToolConfig
}

func New(cfg config.ToolConfig) *Client {
	return &Client{
		cfg: cfg,
	}
}

func (c *Client) Call(ctx context.Context, req *GoodsCategoryAddRequest) (bool, error) {
	apiReq, _ := util.StructToMap(req)

	r := l_request.Request{
		Method: "Post",
		Url:    c.cfg.BaseURL,
		Json:   apiReq,
		Headers: map[string]string{
			"Content-Type": "application/json",
		},
	}

	res, err := r.Send()
	if err != nil {
		return false, fmt.Errorf("请求失败，err: %v", err)
	}

	var resData GoodsCategoryAddResponse
	if err := json.Unmarshal([]byte(res.Text), &resData); err != nil {
		return false, fmt.Errorf("解析响应失败，err: %v", err)
	}

	if resData.Code != 200 {
		return false, fmt.Errorf("业务错误，code: %d, msg: %s", resData.Code, resData.Msg)
	}

	return resData.Data.IsSuccess, nil
}
