package com.fs.wxcid.service.impl;

import com.fs.wxcid.dto.common.ApiResponse;
import com.fs.wxcid.dto.admin.GenAuthKey1Request;
import com.fs.wxcid.dto.admin.GenAuthKey3Request;
import com.fs.wxcid.dto.user.DelayAuthKeyRequest;
import com.fs.wxcid.dto.user.DeleteAuthKeyRequest;
import com.fs.wxcid.dto.user.DisableAuthKeyRequest;
import com.fs.wxcid.service.AdminLicenseService;
import com.fs.wxwork.utils.WxWorkHttpUtil;
import com.alibaba.fastjson.TypeReference;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AdminLicenseServiceImpl implements AdminLicenseService {

    private static final String BASE_URL = "http://114.117.215.244:7006";


    @Override
    public ApiResponse delayAuthKey(String key, DelayAuthKeyRequest request) {
        return post("/admin/DelayAuthKey", key, request);
    }

    @Override
    public ApiResponse deleteAuthKey(String key, DeleteAuthKeyRequest request) {
        return post("/admin/DeleteAuthKey", key, request);
    }

    @Override
    public ApiResponse disableAuthKey(String key, DisableAuthKeyRequest request) {
        return post("/admin/DisableAuthKey", key, request);
    }

    @Override
    public ApiResponse genAuthKey1(String key, GenAuthKey1Request request) {
        return get("/admin/GenAuthKey1", key);
    }

    @Override
    public ApiResponse genAuthKey2(String key) {
        return get("/admin/GenAuthKey2", key);
    }

    @Override
    public ApiResponse genAuthKey3(String key, GenAuthKey3Request request) {
        return post("/admin/GenAuthKey3", key, request);
    }

    @Override
    public ApiResponse getActiveLicenseKeys(String key) {
        return get("/admin/GetActiveLicenseKeys", key);
    }

    @Override
    public ApiResponse getProxyMappingList(String key) {
        return get("/admin/GetProxyMappingList", key);
    }

    @Override
    public ApiResponse httpSyncLicenseKey(String key) {
        return get("/admin/HttpSyncLicenseKey", key);
    }

    // ------------------ 工具方法 ------------------
    /**
     * 通用 POST 请求方法
     *
     * @param path   接口路径，如 "/friend/AgreeAdd"
     * @param key    账号唯一标识（query 参数）
     * @param request 请求体对象
     * @return 统一响应结果
     */
    private ApiResponse post(String path, String key, Object request) {
        String url = BASE_URL + path + "?key=" + key;
        return WxWorkHttpUtil.postWithType(url, request, new TypeReference<ApiResponse>() {});
    }

    /**
     * 通用 GET 请求方法（无请求体）
     *
     * @param path 接口路径
     * @param key  账号唯一标识
     * @return 统一响应结果
     */
    private ApiResponse get(String path, String key) {
        String url = BASE_URL + path;
        Map<String, Object> params = new HashMap<>();
        params.put("key", key);
        String resp = WxWorkHttpUtil.get(url, params);
        return com.alibaba.fastjson.JSON.parseObject(resp, ApiResponse.class);
    }
}