package fastgpt

import (
	"context"
	"fmt"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/zeromicro/go-zero/core/logx"
	"net/http"
	"strconv"
	"wechat-api/internal/svc"
	"wechat-api/internal/types"
)

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

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

func (l *DeleteAppLogic) DeleteApp(req *types.DeleteAppsReq) (resp *types.BaseMsgResp, err error) {
	organizationId := l.ctx.Value("organizationId").(uint64)

	organizationIdStr := strconv.FormatUint(organizationId, 10)
	token, err := GetToken(organizationIdStr)
	if err != nil {
		return nil, fmt.Errorf("invalid token")
	}

	AppDelete(req.Id, token)

	return &types.BaseMsgResp{Msg: errormsg.Success}, nil
}

func AppDelete(id string, token string) {
	url := fmt.Sprintf("https://fastgpt.gkscrm.com/api/core/app/del?appId=%s", id)

	// 创建一个新的请求
	req1, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Printf("Error creating request: %v\n", err)
		return
	}

	// 设置请求头
	req1.Header.Set("Content-Type", "application/json")
	req1.Header.Set("Cookie", fmt.Sprintf("fastgpt_token=%s", token))

	// 使用 http.Client 发送请求
	client := &http.Client{}
	resp1, err := client.Do(req1)
	if err != nil {
		fmt.Printf("Error sending request: %v\n", err)
		return
	}
	defer resp1.Body.Close()

	return
}
