package com.liquidnet.service.goblin.service.impl;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.liquidnet.commons.lang.util.MD5Utils;
import com.liquidnet.service.goblin.config.properties.ShouqianbaProperties;
import com.liquidnet.service.goblin.param.shouqianba.callback.CallbackParams;
import com.liquidnet.service.goblin.param.shouqianba.request.*;
import com.liquidnet.service.goblin.param.shouqianba.response.*;
import com.liquidnet.service.goblin.param.shouqianba.response.data.*;
import com.liquidnet.service.goblin.service.IGoblinShouQianBaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 收钱吧对接实现类
 *
 * @author liquidnet
 * @since 2026-03-16
 */
@Slf4j
@Service
public class GoblinShouQianBaServiceImpl implements IGoblinShouQianBaService {


    private final ShouqianbaProperties shouqianbaProperties;
    private final CommonRequest.Seller cachedSeller;

    public GoblinShouQianBaServiceImpl(ShouqianbaProperties properties) {
        this.shouqianbaProperties = properties;
        if (this.shouqianbaProperties == null) {
            throw new IllegalArgumentException("找不到应用配置");
        }
        // 商户信息来自配置，整个生命周期内不变，提前构建并缓存
        this.cachedSeller = new CommonRequest.Seller();
        this.cachedSeller.setMerchantId(properties.getMerchantId());
        this.cachedSeller.setMerchantUserId(properties.getMerchantUserId());
        this.cachedSeller.setRole(properties.getRole());
    }

    private static final ObjectMapper objectMapper;
    private static final RestTemplate restTemplate;


    static {
        // 1. 初始化 RestTemplate 并强制设置 String 转换器为 UTF-8
        restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));

        // 2. 初始化紧凑型 ObjectMapper (签名专用：无空格、无换行、不含空字段)
        objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, false);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    /**
     * 创建结算明细
     *
     * @param request 创建结算明细请求参数
     * @return 收钱吧响应结果；调用失败时返回 null
     */
    @Override
    public SettlementCreateData createSettlement(SettlementCreateRequest request) {
        return executeShouQianBaRequest("/optimus/core/mall/createCheckoutItems", "创建结算明细", request, SettlementCreateResponse.class);
    }

    @Override
    public SettlementCreateData createSettlement(String mallSn, String signature, String userId, List<SettlementCreateRequest.CheckoutItem> checkoutItems) {
        SettlementCreateRequest request = new SettlementCreateRequest();
        request.setAppid(shouqianbaProperties.getAppId());
        request.setMallID(buildMall(mallSn, signature));
        request.setSeller(cachedSeller);
        request.setBuyer(buildBuyer(userId));
        request.setCheckoutItems(checkoutItems);
        return createSettlement(request);
    }

    private CommonRequest.Mall buildMall(String mallSn, String signature) {
        CommonRequest.Mall mall = new CommonRequest.Mall();
        mall.setMallSn(mallSn);
        mall.setSignature(signature);
        return mall;
    }

    private CommonRequest.Buyer buildBuyer(String userId) {
        CommonRequest.Buyer buyer = new CommonRequest.Buyer();
        buyer.setBuyerId(userId);
        return buyer;
    }

    /**
     * 创建订单
     *
     * @param request
     * @return
     */
    @Override
    public OrderCreateData createOrder(OrderCreateRequest request) {
        return executeShouQianBaRequest("/optimus/core/order/placeOrder", "创建订单", request, OrderCreateResponse.class);
    }

    @Override
    public OrderCreateData createOrder(String mallSn, String signature, String checkoutItemsId, String userId, String requestId, String subject) {
        OrderCreateRequest orderCreateRequest = new OrderCreateRequest();
        orderCreateRequest.setAppid(shouqianbaProperties.getAppId());
        orderCreateRequest.setMallID(buildMall(mallSn, signature));
        orderCreateRequest.setSeller(cachedSeller);
        orderCreateRequest.setCheckoutItemsId(checkoutItemsId);
        orderCreateRequest.setBuyer(buildBuyer(userId));
        orderCreateRequest.setRequestId(requestId);
        orderCreateRequest.setSubject(subject);
        return createOrder(orderCreateRequest);
    }

    /**
     * 创建收单
     * 如果上一步创建收单失败 可以使用这个接口重新创建收单
     *
     * @return
     */
    @Override
    public AcquiringCreateData createAcquiring(AcquiringCreateRequest request) {
        return executeShouQianBaRequest("/optimus/core/order/createAcquiring", "创建收单环节", request, AcquiringCreateResponse.class);
    }

    /**
     * 查询收银台
     *
     * @param request
     * @return
     */
    @Override
    public CashierQueryData queryCashier(CashierQueryRequest request) {
        return executeShouQianBaRequest("/optimus/core/cashier/queryCashierPage", "查询收银台", request, CashierQueryResponse.class);
    }

    @Override
    public CashierQueryData queryCashier(String acquiringSn, String acquiringSignature, String userId) {
        CashierQueryRequest.PaymentEnv paymentEnv = new CashierQueryRequest.PaymentEnv();
        paymentEnv.setClient("wechat");

        CashierQueryRequest cashierQueryRequest = new CashierQueryRequest();
        cashierQueryRequest.setAppid(shouqianbaProperties.getAppId());
        cashierQueryRequest.setSeller(cachedSeller);
        cashierQueryRequest.setPaymentMode(4);
        cashierQueryRequest.setPaymentEnv(paymentEnv);
        cashierQueryRequest.setPayer(new CommonRequest.Payer(userId));
        cashierQueryRequest.setAcquiringInfo(new CommonRequest.Acquiring(acquiringSn, acquiringSignature));

        return queryCashier(cashierQueryRequest);
    }

    /**
     * 创建微信预支付订单
     *
     * @param request
     * @return
     */
    @Override
    public CreateWechatPrepayOrderData createWechatPrepayOrder(CreateWechatPrepayOrderRequest request) {
        return executeShouQianBaRequest("/optimus/core/cashier/proxyPreCreatedPay", "创建微信预支付订单", request, CreateWechatPrepayOrderResponse.class);
    }

    @Override
    public CreateWechatPrepayOrderData createWechatPrepayOrder(String acquiringSn,
                                                               String signature,
                                                               String userId,
                                                               String amount,
                                                               String requestSn,
                                                               CashierQueryData.PayTool payTool,
                                                               Map<String, Object> channelExt,
                                                               String selectedSignature,
                                                               String seq) {
        CreateWechatPrepayOrderRequest orderRequest = new CreateWechatPrepayOrderRequest();
        orderRequest.setAppid(shouqianbaProperties.getAppId());
        orderRequest.setSeller(cachedSeller);
        orderRequest.setAcquiringSn(acquiringSn);
        orderRequest.setSignature(signature);
        orderRequest.setUsingPayTools(buildUsingPayTools(requestSn, payTool, channelExt, amount, userId));
        orderRequest.setSelectedSignature(selectedSignature);
        orderRequest.setSeq(seq);
        return createWechatPrepayOrder(orderRequest);
    }

    private List<CreateWechatPrepayOrderRequest.UsingPayTool> buildUsingPayTools(String requestSn,
                                                                                 CashierQueryData.PayTool payTool,
                                                                                 Map<String, Object> channelExt,
                                                                                 String amount,
                                                                                 String userId) {
        CreateWechatPrepayOrderRequest.UsingPayTool tool = new CreateWechatPrepayOrderRequest.UsingPayTool();
        tool.setId(payTool.getId());
        tool.setPayTool(Integer.valueOf(payTool.getCode()));
        tool.setPayMode(4); // TODO 需要确认
        tool.setAmount(amount);
        tool.setIdentity(userId);
        tool.setAmountComposition(payTool.getAmountComposition());
        tool.setRequestSn(requestSn);
        tool.setChannelExt(channelExt);
        return Collections.singletonList(tool);

    }

    /**
     * 查询券码
     *
     * @param request
     * @return
     */
    @Override
    public CouponQueryData queryCoupon(CouponQueryRequest request) {
        return executeShouQianBaRequest("/optimus/core/voucher/queryOrderVoucherList", "查询优惠券", request, CouponQueryResponse.class);
    }

    @Override
    public CouponQueryData queryCoupon(String sn, String signature) {
        CouponQueryRequest couponQueryRequest = new CouponQueryRequest();
        couponQueryRequest.setAppid(shouqianbaProperties.getAppId());
        couponQueryRequest.setSeller(cachedSeller);
        couponQueryRequest.setOrderID(new CommonRequest.OrderInfo(sn, signature));
        return queryCoupon(couponQueryRequest);
    }

    /**
     * 券码状态同步
     *
     * @param request
     * @return
     */
    @Override
    public boolean syncCouponStatus(CouponStatusSyncRequest request) {
        CouponStatusSyncResponse response = executeRequestAndGetResponse("/optimus/core/voucher/modifyOrderVoucherList", "同步优惠券状态", request, CouponStatusSyncResponse.class);
        return response != null && Boolean.TRUE.equals(response.getSuccess());
    }

    @Override
    public boolean syncCouponStatus(String redeemMerchantId, List<String> voucherNos, String redeemExternalOrderSn, String clientSn, Byte status) {
        CouponStatusSyncRequest couponStatusSyncRequest = new CouponStatusSyncRequest();
        couponStatusSyncRequest.setAppid(shouqianbaProperties.getAppId());
        couponStatusSyncRequest.setVoucherNos(voucherNos);
        couponStatusSyncRequest.setRedeemSource("EXTERN");
        couponStatusSyncRequest.setRedeemExternalOrderSn(redeemExternalOrderSn);
        couponStatusSyncRequest.setRedeemMerchantId(redeemMerchantId);
        couponStatusSyncRequest.setClientSn(clientSn);
        couponStatusSyncRequest.setStatus(status);
        return syncCouponStatus(couponStatusSyncRequest);
    }

    /**
     * 券退款
     *
     * @param request
     * @return
     */
    @Override
    public CouponRefundData refundCoupon(CouponRefundRequest request) {
        return executeShouQianBaRequest("/optimus/core/aftersale/applyRefund", "退款优惠券", request, CouponRefundResponse.class);
    }

    @Override
    public CouponRefundData refundCoupon(String sn,
                                         String signature,
                                         Long applyAmount,
                                         Byte type,
                                         List<CouponRefundRequest.RefundItem> item,
                                         String refundReason,
                                         String requestId) {
        CouponRefundRequest couponRefundRequest = new CouponRefundRequest();
        couponRefundRequest.setAppid(shouqianbaProperties.getAppId());
        couponRefundRequest.setSeller(cachedSeller);
        couponRefundRequest.setOrderID(new CommonRequest.OrderInfo(sn, signature));
        couponRefundRequest.setRefundInfo(new CouponRefundRequest.RefundInfo(applyAmount, type, item, refundReason));
        couponRefundRequest.setRequestId(requestId);
        couponRefundRequest.setRequestSource("EXTERN");
        return refundCoupon(couponRefundRequest);
    }

    /**
     * 商城列表接口
     *
     * @param request
     * @return
     */
    @Override
    public List<MallListQueryData> queryMallList(MallListQueryRequest request) {
        return executeShouQianBaRequest("/optimus/core/open/mall/queryMallList", "查询门店列表", request, MallListQueryResponse.class);
    }

    @Override
    public List<MallListQueryData> queryMallList(String endCursor, Integer count) {

        MallListQueryRequest.Filter filter = new MallListQueryRequest.Filter();
        filter.setSeller(cachedSeller);

        MallListQueryRequest.Cursor cursor = new MallListQueryRequest.Cursor();
        cursor.setCursorField("id");
        cursor.setEndCursor(endCursor);
        cursor.setCount(count);

        MallListQueryRequest.Sort sort = new MallListQueryRequest.Sort();
        sort.setSortField("id");
        sort.setSort("DESC");

        MallListQueryRequest mallListQueryRequest = new MallListQueryRequest();
        mallListQueryRequest.setAppid(shouqianbaProperties.getAppId());
        mallListQueryRequest.setFilter(filter);
        mallListQueryRequest.setCursor(cursor);
        mallListQueryRequest.setSort(sort);
        return queryMallList(mallListQueryRequest);
    }

    /**
     * 商城商品接口
     *
     * @param request
     * @return
     */
    @Override
    public List<MallProductsQueryData> queryMallProducts(MallProductsQueryRequest request) {
        return executeShouQianBaRequest("/optimus/core/open/mall/queryMallProducts", "查询门店商品", request, MallProductsQueryResponse.class);
    }

    @Override
    public List<MallProductsQueryData> queryMallProducts(String mallSn, String signature) {
        MallProductsQueryRequest request = new MallProductsQueryRequest();
        request.setAppid(shouqianbaProperties.getAppId());
        request.setSeller(cachedSeller);
        request.setMallID(new CommonRequest.Mall(mallSn, signature));
        return queryMallProducts(request);
    }

    /**
     * 发送HTTP请求并返回Response对象
     */
    private <T extends BaseResponse<?>> T executeRequestAndGetResponse(
            String apiPath, String businessName, Object request, Class<T> responseClass) {
        final String url = shouqianbaProperties.getBaseApi() + apiPath;
        log.info("[收钱吧] {}, 请求URL: {}, 请求参数: {}", businessName, url, request);

        try {
            String requestBody = objectMapper.writeValueAsString(request);
            log.info("request body: {}", requestBody);

            // 构建请求头（添加签名参数）
            final String sign = getSign(requestBody);
            log.info("request header: {}", sign);

            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
            headers.add("Authorization", sign);
            headers.add("Content-Type", "application/json");
            headers.add("X-env-flag", "41508");

            // 4. 发送请求
            // 注意：传入的是 String jsonBody，接收的也是 String.class，确保不被二次处理
            HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

            String responseStr = responseEntity.getBody();
            log.info("[收钱吧] {}, 响应体: {}", businessName, responseStr);

            // 转换响应报文
            T response = objectMapper.readValue(responseStr, responseClass);
            if (response != null && !Boolean.TRUE.equals(response.getSuccess())) {
                log.error("[收钱吧] {}业务失败, code: {}, msg: {}", businessName,
                        response.getCode(), response.getMsg());
            }
            return response;
        } catch (Exception e) {
            log.error("[收钱吧] {}发生异常, 请求参数: {}", businessName, request, e);
            return null;
        }
    }

    /**
     * 封装收钱吧接口调用，提取公共逻辑并发起请求，默认返回Data
     */
    private <D, T extends BaseResponse<D>> D executeShouQianBaRequest(
            String apiPath, String businessName, Object request, Class<T> responseClass) {
        T response = executeRequestAndGetResponse(apiPath, businessName, request, responseClass);
        if (response != null && Boolean.TRUE.equals(response.getSuccess())) {
            return response.getData();
        }
        return null;
    }

    private String getSign(String bodyJsonStr) {
        try {
            // 签名算法: sign = MD5( CONCAT( body + appkey ) )
            String rawStr = bodyJsonStr + shouqianbaProperties.getAppKey();
            String sign = MD5Utils.md5(rawStr);

            // 签名首部: Authorization: appid + " " + sign
            return shouqianbaProperties.getAppId() + " " + sign;
        } catch (Exception e) {
            log.error("[收钱吧] 生成签名发生异常", e);
            return "";
        }
    }

    /**
     * 收钱吧回调推送验签
     *
     * @param callbackParams@return 验签是否通过
     */
    public boolean verifySignature(CallbackParams callbackParams) {
        // 签名原串 = eventId + timestamp + nonce + content
        String plaintext = callbackParams.getEventId() + callbackParams.getTimestamp() + callbackParams.getNonce() + callbackParams.getContent();
        return verifySignatureSHA256WithRSA(plaintext, callbackParams.getSignature(), shouqianbaProperties.getPublicKey());
    }

    public boolean verifySignatureSHA256WithRSA(String plaintext, String inputSignature, String pubKey) {
        return verifySignatureSHA256WithRSA(plaintext.getBytes(StandardCharsets.UTF_8), inputSignature, pubKey);
    }

    public boolean verifySignatureSHA256WithRSA(byte[] plaintextByte, String inputSignature, String pubKey) {
        try {
            X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(pubKey));
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = keyFactory.generatePublic(bobPubKeySpec);
            byte[] signed = Base64.getDecoder().decode(inputSignature);
            Signature signature = Signature.getInstance("SHA256WithRSA");
            signature.initVerify(publicKey);
            signature.update(plaintextByte);
            return signature.verify(signed);
        } catch (Exception e) {
            log.error("[收钱吧] 验签异常, signature={}, pubKey={}", inputSignature, pubKey, e);
            return false;
        }
    }
}
