package com.tzld.piaoquan.api.component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tzld.piaoquan.api.common.enums.ApiChannelEnum;
import com.tzld.piaoquan.api.model.dto.AIOfficialApiResponse;
import com.tzld.piaoquan.api.model.dto.AIResult;
import com.tzld.piaoquan.growth.common.utils.MapBuilder;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class DeepSeekApiService {

    private OkHttpClient client;

    @Value("${deepseek.default.model:deepseek-v4-flash}")
    private String defaultModel;

    @Value("${deepseek.official.url:https://api.deepseek.com/chat/completions}")
    private String officialUrl;

    @Value("${deepseek.official.apiKey:sk-62d7b2c37f824735aa4985852c919c1f}")
    private String officialApiKey;

    @Value("${deepseek.volcengine.url:https://ark.cn-beijing.volces.com/api/v3/chat/completions}")
    private String volcengineUrl;

    @Value("${deepseek.volcengine.apiKey:ark-b5d6fcbb-14f9-4f70-a92d-605ec6a72c8d-40883}")
    private String volcengineApiKey;

    @Value("${deepseek.volcengine.model:ep-20250717193758-8gvmz}")
    private String volcengineModel;

    @PostConstruct
    public void init() {
        client = new OkHttpClient().newBuilder()
                .connectTimeout(5, TimeUnit.MINUTES)
                .readTimeout(5, TimeUnit.MINUTES)
                .writeTimeout(5, TimeUnit.MINUTES)
                .build();
    }

    /**
     * 默认调用火山引擎 API
     */
    public AIResult request(String prompt, String model, Double temperature, Boolean isJSON) {
        return request(prompt, model, temperature, isJSON, ApiChannelEnum.VOLCENGINE);
    }

    /**
     * 支持指定渠道的 API 调用
     */
    public AIResult request(String prompt, String model, Double temperature, Boolean isJSON, ApiChannelEnum channel) {
        AIResult result = new AIResult();
        result.setSuccess(false);
        if (TextUtils.isBlank(prompt) || TextUtils.isBlank(prompt.trim())) {
            result.setFailReason("prompt is empty");
            return result;
        }

        String url;
        String apiKey;
        String defaultChannelModel;
        String channelName;

        if (channel == ApiChannelEnum.OFFICIAL) {
            url = officialUrl;
            apiKey = officialApiKey;
            defaultChannelModel = defaultModel;
            channelName = "official";
        } else {
            url = volcengineUrl;
            apiKey = volcengineApiKey;
            defaultChannelModel = volcengineModel;
            channelName = "volcengine";
        }

        if (TextUtils.isBlank(apiKey)) {
            result.setFailReason(channelName + " apiKey is not configured");
            return result;
        }

        try {
            JSONArray jsonArray = new JSONArray();
            JSONObject message = new JSONObject();
            message.put("role", "user");
            message.put("content", prompt);
            jsonArray.add(message);

            String useModel = Optional.ofNullable(model).orElse(defaultChannelModel);
            if (TextUtils.isBlank(useModel)) {
                result.setFailReason("model is empty");
                return result;
            }

            Map<Object, Object> bodyParam = MapBuilder
                    .builder()
                    .put("model", useModel)
                    .put("temperature", Optional.ofNullable(temperature).orElse(0.3))
                    .put("messages", jsonArray)
                    .build();
            if (isJSON) {
                JSONObject formatJSON = new JSONObject();
                formatJSON.put("type", "json_object");
                bodyParam.put("response_format", formatJSON);
            }

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, JSONObject.toJSONString(bodyParam));
            Request request = new Request.Builder()
                    .url(url)
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Accept", "application/json")
                    .addHeader("Authorization", "Bearer " + apiKey)
                    .build();
            Response response = client.newCall(request).execute();

            String responseContent = response.body().string();
            result.setResponseStr(responseContent);
            log.info("deepseek {} api responseContent = {}", channelName, responseContent);
            if (response.isSuccessful()) {
                AIOfficialApiResponse obj = JSONObject.parseObject(responseContent, AIOfficialApiResponse.class);
                if (CollectionUtils.isNotEmpty(obj.getChoices())) {
                    result.setSuccess(true);
                    result.setResponse(obj);
                } else {
                    result.setFailReason("response empty");
                }
            } else {
                JSONObject json = JSONObject.parseObject(responseContent);
                result.setFailReason("request error code:" + response.code() + " message:" + json.getString("error"));
            }
        } catch (Exception e) {
            log.error("deepseek {} api fail: {}", channelName, e.getMessage());
            result.setFailReason(e.getMessage());
        }
        return result;
    }

    /**
     * 调用 DeepSeek 官方 API（向后兼容）
     */
    public AIResult requestOfficialApi(String prompt, String model, Double temperature, Boolean isJSON) {
        return request(prompt, model, temperature, isJSON, ApiChannelEnum.OFFICIAL);
    }
}
