package com.seer.teach.mp.app.controller;

import com.seer.teach.mp.app.controller.req.MpOpenCheckSignatureReq;
import com.seer.teach.mp.app.controller.req.MpOpenHandleMessageReq;
import com.seer.teach.mp.app.service.AppMpNotificationService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;

@Tag(name = "APP - 公众号回调")
@RestController
@RequestMapping("/callback")
@Validated
@Slf4j
public class AppMpNotificationController {

    @Resource
    private AppMpNotificationService appMpNotificationService;

    /**
     * 接收微信公众号的消息推送
     *
     * <a href="https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_standard_messages.html">文档</a>
     */
    @Operation(summary = "处理消息")
    @PostMapping(value = "/{appId}", produces = "application/xml; charset=UTF-8")
    public String handleMessage(@PathVariable("appId") String appId,
                                @RequestBody String content,
                                MpOpenHandleMessageReq reqVO) {
        log.info("[handleMessage][appId({}) 推送消息，参数({}) 内容({})]", appId, reqVO, content);
        return appMpNotificationService.handleMessage(appId, content, reqVO);
    }

    /**
     * 接收微信公众号的校验签名
     *
     * 对应 <a href="https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html">文档</a>
     */
    @Operation(summary = "校验签名") // 参见
    @GetMapping(value = "/{appId}", produces = "text/plain;charset=utf-8")
    public String checkSignature(@PathVariable("appId") String appId,
                                 MpOpenCheckSignatureReq reqVO) {
        log.info("[checkSignature][appId({}) 接收到来自微信服务器的认证消息({})]", appId, reqVO);
        return appMpNotificationService.checkSignature(appId, reqVO);
    }

}
