package com.gxwebsoft.shop.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * 微信支付回调别名入口（兼容历史 notify_url）
 *
 * 说明：
 * - 旧代码曾使用 /api/system/wx-pay/notify/{tenantId} 或 /api/shop/wx-pay/notify/{tenantId}
 * - 微信支付“请求重入”要求 notify_url 等参数与首次一致，因此需要保留旧回调地址可用
 */
@Tag(name = "微信支付回调(别名)")
@RestController
public class WxPayNotifyAliasController {

    @Resource
    private ShopOrderController shopOrderController;

    @Operation(summary = "微信支付回调别名(兼容旧回调地址)")
    @PostMapping({"/api/system/wx-pay/notify/{tenantId}", "/api/shop/wx-pay/notify/{tenantId}"})
    public String wxNotifyAlias(@RequestHeader Map<String, String> header,
                                @RequestBody String body,
                                @PathVariable("tenantId") Integer tenantId) {
        // ShopOrderController.wxNotify 读取的是小写 header key，这里做一次兼容转换
        Map<String, String> lower = new HashMap<>();
        if (header != null) {
            header.forEach((k, v) -> {
                if (k != null) {
                    lower.put(k.toLowerCase(Locale.ROOT), v);
                }
            });
        }
        return shopOrderController.wxNotify(lower, body, tenantId);
    }
}

