package com.gxwebsoft.hjm.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.hjm.dto.BatchTemplateMessageRequest;
import com.gxwebsoft.hjm.dto.SubscribeMessageRequest;
import com.gxwebsoft.hjm.dto.TemplateMessageRequest;
import com.gxwebsoft.hjm.service.WxNotificationService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 微信公众号订阅通知控制器
 *
 * @author 科技小王子
 * @since 2025-06-15
 */
@Slf4j
@Tag(name = "微信公众号订阅通知")
@RestController
@RequestMapping("/api/hjm/wx-subscription")
public class SendSubscriptionMessages extends BaseController {

    @Resource
    private WxNotificationService wxNotificationService;

    /**
     * 发送模板消息
     */
    @Operation(summary = "发送模板消息")
    @OperationLog("发送模板消息")
    @PostMapping("/send-template")
    public ApiResult<?> sendTemplateMessage(@RequestBody TemplateMessageRequest request) {
        try {
            User loginUser = getLoginUser();
            Integer tenantId = loginUser != null ? loginUser.getTenantId() : 0;

            // 发送模板消息
            boolean success = wxNotificationService.sendTemplateMessage(tenantId, request);

            if (success) {
                return success("模板消息发送成功");
            } else {
                return fail("模板消息发送失败");
            }

        } catch (Exception e) {
            log.error("发送模板消息失败", e);
            return fail("发送失败：" + e.getMessage());
        }
    }





    /**
     * 发送订阅消息（小程序订阅消息）
     */
    @Operation(summary = "发送订阅消息")
    @OperationLog("发送订阅消息")
    @PostMapping("/send-subscribe")
    public ApiResult<?> sendSubscribeMessage(@RequestBody SubscribeMessageRequest request) {
        try {
            User loginUser = getLoginUser();
            Integer tenantId = loginUser != null ? loginUser.getTenantId() : 0;

            // 发送订阅消息
            boolean success = wxNotificationService.sendSubscribeMessage(tenantId, request);

            if (success) {
                return success("订阅消息发送成功");
            } else {
                return fail("订阅消息发送失败");
            }

        } catch (Exception e) {
            log.error("发送订阅消息失败", e);
            return fail("发送失败：" + e.getMessage());
        }
    }



    /**
     * 批量发送模板消息
     */
    @Operation(summary = "批量发送模板消息")
    @OperationLog("批量发送模板消息")
    @PostMapping("/batch-send-template")
    public ApiResult<?> batchSendTemplateMessage(@RequestBody BatchTemplateMessageRequest request) {
        try {
            User loginUser = getLoginUser();
            Integer tenantId = loginUser != null ? loginUser.getTenantId() : 0;

            WxNotificationService.BatchSendResult result = wxNotificationService.batchSendTemplateMessage(tenantId, request.getMessages());

            return success("批量发送完成，" + result.toString());

        } catch (Exception e) {
            log.error("批量发送模板消息失败", e);
            return fail("批量发送失败：" + e.getMessage());
        }
    }

    /**
     * 获取模板列表
     */
    @Operation(summary = "获取模板列表")
    @GetMapping("/templates")
    public ApiResult<?> getTemplateList() {
        try {
            User loginUser = getLoginUser();
            Integer tenantId = loginUser != null ? loginUser.getTenantId() : 0;

            String response = wxNotificationService.getTemplateList(tenantId);
            if (response != null) {
                JSONObject result = JSONObject.parseObject(response);
                return success("获取成功", result);
            } else {
                return fail("获取失败");
            }

        } catch (Exception e) {
            log.error("获取模板列表失败", e);
            return fail("获取失败：" + e.getMessage());
        }
    }
}
