package com.zsElectric.boot.thirdParty.controller;

import com.zsElectric.boot.common.annotation.Log;
import com.zsElectric.boot.common.enums.LogModuleEnum;
import com.zsElectric.boot.common.util.AESCryptoUtils;
import com.zsElectric.boot.thirdParty.model.ThirdPartyRequest;
import com.zsElectric.boot.thirdParty.model.ThirdPartyResponse;
import com.zsElectric.boot.thirdParty.service.ThirdPartyTokenService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * 第三方接入控制器
 * 提供给第三方平台调用的接口
 *
 * @author wzq
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Tag(name = "第三方接入接口")
@RequestMapping("/third-party/v1")
public class ThirdPartyController {

    private final ThirdPartyTokenService thirdPartyTokenService;

    /**
     * 获取Token
     * Token作为全局唯一凭证,调用各接口时均需要使用
     * <p>
     * 请求格式:
     * - 消息头: Content-Type: application/json
     * - 入参消息体: operatorId, data(加密), timeStamp, seq, sig
     * <p>
     * 加密说明:
     * - data 里的内容为接口的实际入参,需要密文传输
     * - 加解密方法: AES 128位, CBC模式, PKCS5Padding
     * - 签名(sig): HMAC-MD5算法, 入参拼接顺序: operatorId+data+timeStamp+seq
     * - 签名必须大写
     *
     * @param request 请求参数
     * @return 响应结果
     */
    @Operation(summary = "获取Token", description = "第三方平台获取访问令牌")
    @PostMapping("/query_token")
    @Log(value = "第三方获取Token", module = LogModuleEnum.OTHER, params = true, result = true)
    public ThirdPartyResponse queryToken(@RequestBody ThirdPartyRequest request) {
        log.info("收到query_token请求, operatorId: {}", request.getOperatorId());
        return thirdPartyTokenService.queryToken(request);
    }

    @Operation(summary = "充值档位信息分页列表", description = "第三方获取充值档位分页数据，需要在Header中携带Authorization")
    @PostMapping("/query_recharge_level_page")
    @Log(value = "第三方查询充值档位", module = LogModuleEnum.OTHER, params = true, result = true)
    public ThirdPartyResponse queryRechargeLevelPage(
            @RequestBody ThirdPartyRequest request,
            @Parameter(description = "访问令牌，格式: Bearer {accessToken}") @RequestHeader(value = "Authorization", required = false) String authorization) {
        return thirdPartyTokenService.queryRechargeLevelPage(request, authorization);
    }

    @Operation(summary = "获取用户信息", description = "第三方根据手机号获取用户信息，需要在Header中携带Authorization")
    @PostMapping("/query_user_info")
    @Log(value = "第三方根据手机号获取用户信息", module = LogModuleEnum.OTHER, params = true, result = true)
    public ThirdPartyResponse queryUserInfo(
            @RequestBody ThirdPartyRequest request,
            @Parameter(description = "访问令牌，格式: Bearer {accessToken}") @RequestHeader(value = "Authorization", required = false) String authorization) {
        return thirdPartyTokenService.queryUserInfo(request, authorization);
    }

    @Operation(summary = "充点券购买", description = "第三方充点券购买，需要在Header中携带Authorization")
    @PostMapping("/charge_order_pay")
    @Log(value = "第三方充点券购买", module = LogModuleEnum.OTHER, params = true, result = true)
    public ThirdPartyResponse charge_order_pay(
            @RequestBody ThirdPartyRequest request,
            @Parameter(description = "访问令牌，格式: Bearer {accessToken}") @RequestHeader(value = "Authorization", required = false) String authorization) {
        return thirdPartyTokenService.chargeOrderPay(request, authorization);
    }

    @Operation(summary = "获取充电站列表", description = "第三方获取充电站列表，需要在Header中携带Authorization")
    @PostMapping("/query_charge_station_list")
    @Log(value = "第三方获取充电站列表", module = LogModuleEnum.OTHER, params = true, result = true)
    public ThirdPartyResponse queryChargeStationList(
            @RequestBody ThirdPartyRequest request,
            @Parameter(description = "访问令牌，格式: Bearer {accessToken}") @RequestHeader(value = "Authorization", required = false) String authorization) {
        return thirdPartyTokenService.queryChargeStationList(request, authorization);
    }

    @Operation(summary = "获取充电站详情与充电终端列表", description = "第三方获取充电站详情与充电设备列表，需要在Header中携带Authorization")
    @PostMapping("/query_charge_station_detail")
    @Log(value = "第三方获取充电站详情与充电终端列表", module = LogModuleEnum.OTHER, params = true, result = true)
    public ThirdPartyResponse queryChargeStationDetail(
            @RequestBody ThirdPartyRequest request,
            @Parameter(description = "访问令牌，格式: Bearer {accessToken}") @RequestHeader(value = "Authorization", required = false) String authorization) {
        return thirdPartyTokenService.queryChargeStationDetail(request, authorization);
    }

    @Operation(summary = "获取充电终端详情", description = "第三方获取充电终端详情，需要在Header中携带Authorization")
    @PostMapping("/query_charge_device_detail")
    @Log(value = "第三方获取充电终端详情", module = LogModuleEnum.OTHER, params = true, result = true)
    public ThirdPartyResponse queryChargeDeviceDetail(
            @RequestBody ThirdPartyRequest request,
            @Parameter(description = "访问令牌，格式: Bearer {accessToken}") @RequestHeader(value = "Authorization", required = false) String authorization) {
        return thirdPartyTokenService.queryChargeDeviceDetail(request, authorization);
    }
}
