package org.dromara.mp.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.mp.api.WxMpService;
import org.dromara.common.core.domain.R;
import org.dromara.common.web.core.BaseController;
import org.dromara.mp.annotation.SwitchoverCheck;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 微信公众号JSAPI控制器
 *
 * @author pengles
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wx/mp/jsapi/{appid}")
public class WxMpJsapiController extends BaseController {

    private final WxMpService wxMpService;

    /**
     * 获取JSAPI票据
     *
     * @param appid        公众号appid
     * @param forceRefresh 是否强制刷新
     * @return JSAPI票据
     */
    @GetMapping("/ticket")
    @SwitchoverCheck
    public R<String> getJsapiTicket(@PathVariable String appid,
                                    @RequestParam(defaultValue = "false") boolean forceRefresh) {
        try {
            String ticket = wxMpService.getJsapiTicket(forceRefresh);
            return R.ok(ticket);
        } catch (Exception e) {
            log.error("获取JSAPI票据失败: {}", e.getMessage(), e);
            return R.fail("获取JSAPI票据失败: " + e.getMessage());
        }
    }

    /**
     * 创建JSAPI签名
     *
     * @param appid 公众号appid
     * @param url   当前网页的URL，不包含#及其后面部分
     * @return JSAPI签名信息
     */
    @GetMapping("/signature")
    @SwitchoverCheck
    public R<WxJsapiSignature> createJsapiSignature(@PathVariable String appid,
                                                    @RequestParam String url) {
        try {
            WxJsapiSignature signature = wxMpService.createJsapiSignature(url);
            return R.ok(signature);
        } catch (Exception e) {
            log.error("创建JSAPI签名失败: {}", e.getMessage(), e);
            return R.fail("创建JSAPI签名失败: " + e.getMessage());
        }
    }

    /**
     * 获取微信卡券签名
     *
     * @param appid 公众号appid
     * @param url   当前网页的URL
     * @return 卡券签名信息
     */
    @GetMapping("/card/signature")
    @SwitchoverCheck
    public R<Object> createCardSignature(@PathVariable String appid,
                                         @RequestParam String url) {
        try {
            // 这里可以添加卡券签名的逻辑
            log.info("创建卡券签名: appid={}, url={}", appid, url);
            return R.ok("卡券签名功能待实现");
        } catch (Exception e) {
            log.error("创建卡券签名失败: {}", e.getMessage(), e);
            return R.fail("创建卡券签名失败: " + e.getMessage());
        }
    }

    /**
     * 获取微信JS接口的临时票据
     *
     * @param appid 公众号appid
     * @param type  票据类型
     * @return 临时票据
     */
    @GetMapping("/ticket/{type}")
    @SwitchoverCheck
    public R<String> getTicket(@PathVariable String appid,
                               @PathVariable String type) {
        try {
            String ticket = wxMpService.getTicket(me.chanjar.weixin.common.enums.TicketType.valueOf(type.toUpperCase()));
            return R.ok(ticket);
        } catch (Exception e) {
            log.error("获取票据失败: {}", e.getMessage(), e);
            return R.fail("获取票据失败: " + e.getMessage());
        }
    }
}
