package com.ai.da.common.utils;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class SendRequestUtil {

    @Value("${ALIYUN_API_KEY}")
    private String ALIYUN_API_KEY;
    @Value("${FREEPIK_API_KEY}")
    private String FREEPIK_API_KEY;

    public String sendAliYunPostAsync(String apiUrl, String requestBody){
        // 发送POST请求 todo 异常处理
        HttpResponse execute = HttpRequest.post(apiUrl)
                .header(Header.AUTHORIZATION, "Bearer " + ALIYUN_API_KEY)
                .header("X-DashScope-Async", "enable")
                .header(Header.CONTENT_TYPE, "application/json")
                .body(requestBody)
                .timeout(20000) // 设置超时时间20秒
                .execute();
        int status = execute.getStatus();
        if (status == 200){
            String body = execute.body();
            JSONObject bodyJson = JSONUtil.parseObj(body);
            return body;
        }
        log.warn("请求失败，状态码为 ： {}", status);
        return null;
    }

    public String sendAliYunPost(String apiUrl, String requestBody){
        // 发送POST请求 todo 异常处理
        HttpResponse execute = HttpRequest.post(apiUrl)
                .header(Header.AUTHORIZATION, "Bearer " + ALIYUN_API_KEY)
                .header(Header.CONTENT_TYPE, "application/json")
                .body(requestBody)
                .timeout(20000) // 设置超时时间20秒
                .execute();
        int status = execute.getStatus();
        if (status == 200){
            String body = execute.body();
            JSONObject bodyJson = JSONUtil.parseObj(body);
            return body;
        }
        log.warn("请求失败，状态码为 ： {}", status);
        return null;
    }

    public static final String FREE_PIK = "https://api.freepik.com/v1/ai/beta/text-to-image/reimagine-flux";
    public String sendFreepikPost( String requestBody){
        // 发送POST请求 todo 异常处理
        HttpResponse execute = HttpRequest.post(FREE_PIK)
                .header(Header.CONTENT_TYPE, "application/json")
                .header("x-freepik-api-key", FREEPIK_API_KEY)
                .body(requestBody)
                .timeout(20000) // 设置超时时间20秒
                .execute();
        int status = execute.getStatus();
        if (status == 200){
            return execute.body();
        }
        log.warn("请求失败，状态码为 ： {}", status);
        return null;
    }

    public String sendAliYunGet(String fullUrl){
        // 发送GET请求 todo 异常处理
        HttpResponse httpResponse = HttpRequest.get(fullUrl)
                .header(Header.AUTHORIZATION, "Bearer " + ALIYUN_API_KEY)
                .timeout(20000) // 设置超时时间20秒
                .execute();
        int status = httpResponse.getStatus();
        if (status == 200){
            return httpResponse.body();
        }else {
            return null;
        }
    }

    public String sendFluxPost(String url, String requestBodyStr){
        int status;
        String body;
        try (HttpResponse execute = HttpRequest.post(url)
                .header(Header.CONTENT_TYPE, "application/json")
                .header("x-key", "d447a0ac-2291-4f1c-9a36-f7614c385989")
                .body(requestBodyStr) // Hutool 会自动处理 JSON 序列化
                .timeout(180000) // 设置超时（毫秒）
                .execute()) {

            status = execute.getStatus();
            body = execute.body();
            if (status == 200) {
                return body;
            }
        }
        log.warn("请求失败，状态码为 ： {}", status);
        return null;
    }

    public String sendPost(String url, String requestBodyStr){
        int status;
        String body;
        try (HttpResponse execute = HttpRequest.post(url)
                .header("Content-Type", "application/json") // 必须设置 Content-Type
                .body(requestBodyStr) // Hutool 会自动处理 JSON 序列化
                .timeout(180000) // 设置超时（毫秒）
                .execute()) {

            status = execute.getStatus();
            body = execute.body();
            if (status == 200) {
                return body;
            }
        }
        log.warn("请求失败，状态码为 ： {}, body: {}", status, body);
        return null;
    }

    public String sendGet(String url, Map<String, Object> params) {
        int status;
        String body;
        try (HttpResponse execute = HttpRequest.get(url)
                .form(params) // 直接传入Map，Hutool会正确处理
                .timeout(180000)
                .execute()) {

            status = execute.getStatus();
            body = execute.body();
            if (status == 200) {
                return body;
            }
        } catch (Exception e) {
            log.error("请求发生异常: {}", e.getMessage(), e);
            return null;
        }
        log.warn("请求失败，状态码为: {}, body: {}", status, body);
        return body;
    }



}
