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

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.ResultBean;
import com.seer.teach.mp.admin.controller.req.MpTemplateMessageReq;
import com.seer.teach.mp.admin.controller.resp.MpTemplateMessageResp;
import com.seer.teach.mp.admin.controller.resp.MpWxMpTemplateResp;
import com.seer.teach.mp.admin.convert.MpWxMpTemplateConvert;
import com.seer.teach.mp.entity.MpTemplateMessageEntity;
import com.seer.teach.mp.admin.service.AdminTemplateMessageService;
import com.seer.teach.mp.admin.utils.ClassFieldUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

/**
 * <p>
 * 公众号模板消息接口
 * </p>
 *
 * @author Wang
 * @since 2025-08-02 15:45:13
 */
@Slf4j
@RestController
@RequestMapping("/mp/template/message")
@Tag(name = "管理端 - 公众号模板消息接口")
public class AdminMpTemplateMessageController {

    @Resource
    private AdminTemplateMessageService adminTemplateMessageService;

    /**
     * 获取模板列表
     */
    @SaCheckPermission("mp:template:message:list")
    @Operation(summary = "获取模板列表")
    @GetMapping("/list")
    public ResultBean<List<MpWxMpTemplateResp>> list(@RequestParam("appId") String appId) throws WxErrorException {
        List<WxMpTemplate> allPrivateTemplate = adminTemplateMessageService.getAllPrivateTemplate(appId);
        return ResultBean.success(MpWxMpTemplateConvert.INSTANCE.convertList(allPrivateTemplate));
    }

    /**
     * 获取模板消息历史
     */
    @SaCheckPermission("mp:template:message:history")
    @Operation(summary = "获取模板消息历史")
    @GetMapping("/history")
    public ResultBean<List<MpTemplateMessageResp>> history(@RequestParam("businessId") String businessId){
        List<MpTemplateMessageEntity> history = adminTemplateMessageService.getTemplateMessageHistory(businessId);
        return ResultBean.success(MpWxMpTemplateConvert.INSTANCE.convertMpList(history));
    }

    /**
     * 发送模板消息给指定角色的用户
     */
    @SaCheckPermission("mp:template:message:send")
    @Operation(summary = "发送模板消息给指定角色的用户")
    @PostMapping("/send")
    public ResultBean<String> sendTemplateMessageToRoleUsers(@Valid @RequestBody MpTemplateMessageReq req) throws WxErrorException {
        Integer userId = StpUtil.getLoginIdAsInt();
        log.info("用户{}发送模板消息", userId);
        adminTemplateMessageService.sendTemplateMessage(req);
        return ResultBean.success("消息发送成功");
    }

    /**
     * 根据类名获取类的字段和字段类型信息
     *
     * @param className 类的全限定名
     * @return 字段名和字段类型的映射关系
     */
    @SaCheckPermission("mp:template:message:field-info")
    @Operation(summary = "根据类名获取类的字段和字段类型信息")
    @GetMapping("/field-info")
    public ResultBean<List<ClassFieldUtils.FieldInfo>> getFieldInfo(@RequestParam("className") String className) throws ClassNotFoundException {
        List<ClassFieldUtils.FieldInfo> orderFields = ClassFieldUtils.getFieldInfo(className, Schema.class);
        return ResultBean.success(orderFields);
    }
}