package com.ruoyi.app.order;

import com.ruoyi.app.pay.LinePayOfflineService;
import com.ruoyi.app.pay.dto.LinePayOfflinePayRequest;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.system.utils.Auth;
import com.ruoyi.system.utils.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/system/orderShOprate/linePay/offline")
public class PosOrderLinePayOfflineController {
    private static final Logger log = LoggerFactory.getLogger(PosOrderLinePayOfflineController.class);
    private final LinePayOfflineService offlineService;

    public PosOrderLinePayOfflineController(LinePayOfflineService offlineService) {
        this.offlineService = offlineService;
    }

    @Anonymous
    @Auth
    @PostMapping("/pay")
    public AjaxResult pay(@RequestHeader String token,
                          @RequestBody(required = false) LinePayOfflinePayRequest request) {
        if (request == null || request.getDdId() == null || request.getDdId().trim().isEmpty()) {
            throw new ServiceException(MessageUtils.message("line.pay.order.required"));
        }
        if (request.getOneTimeKey() == null
                || !request.getOneTimeKey().matches("\\d{18}")) {
            throw new ServiceException(MessageUtils.message("line.pay.offline.mycode.invalid"));
        }
        Long merchantId = Long.valueOf(new JwtUtil().getusid(token));
        // 入口留痕：后续 ServiceException 全局处理器只记 i18n 消息，靠这行关联商家/订单/付款码
        log.info("LINE Pay offline pay requested, merchantId={}, ddId={}, myCode={}",
                merchantId, request.getDdId(), maskMyCode(request.getOneTimeKey()));
        return AjaxResult.success(offlineService.pay(merchantId, request.getDdId().trim(),
                request.getOneTimeKey()));
    }

    @Anonymous
    @Auth
    @GetMapping("/status")
    public AjaxResult status(@RequestHeader String token, @RequestParam String ddId) {
        Long merchantId = Long.valueOf(new JwtUtil().getusid(token));
        // 前端处理中每 5 秒轮询一次，debug 级避免日志刷屏
        log.debug("LINE Pay offline status query, merchantId={}, ddId={}", merchantId, ddId);
        return AjaxResult.success(offlineService.status(merchantId, ddId));
    }

    /** 付款码掩码：仅保留后 4 位；完整 oneTimeKey 属一次性凭证，禁止写入日志。 */
    private static String maskMyCode(String oneTimeKey) {
        if (oneTimeKey == null || oneTimeKey.length() < 4) {
            return "****";
        }
        return "****" + oneTimeKey.substring(oneTimeKey.length() - 4);
    }
}
