package shop.alien.store.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.store.dto.WxJsSdkConfigRequest;
import shop.alien.store.dto.WxJsSdkConfigVo;
import shop.alien.store.service.WxJsSdkService;

/**
 * 微信公众号 JS-SDK（H5 分享等）签名接口
 */
@Slf4j
@Api(tags = {"微信公众号-JS-SDK"})
@CrossOrigin
@RestController
@RequestMapping("/wx")
@RequiredArgsConstructor
public class WxJsSdkController {

    private final WxJsSdkService wxJsSdkService;

    @ApiOperation(value = "获取微信 JS-SDK 签名参数（前端 wx.config 使用）",
            notes = "url 放在请求体，避免 GET query 过长导致 413")
    @PostMapping("/getWxConfig")
    public R<WxJsSdkConfigVo> getWxConfig(@RequestBody WxJsSdkConfigRequest request) {
        if (request == null || !StringUtils.hasText(request.getUrl())) {
            return R.fail("url 不能为空");
        }
        String url = request.getUrl().trim();
        log.info("WxJsSdkController.getWxConfig url={}", url);
        return R.data(wxJsSdkService.buildJsSdkConfig(url));
    }
}
