package com.njcn.msgpush.module.push.controller.admin.retry;

import com.njcn.msgpush.framework.common.pojo.CommonResult;
import com.njcn.msgpush.framework.common.pojo.PageResult;
import com.njcn.msgpush.module.push.controller.admin.retry.vo.MessageRetryQueueReqVO;
import com.njcn.msgpush.module.push.controller.admin.retry.vo.RetryStrategyConfigReqVO;
import com.njcn.msgpush.module.push.dal.dataobject.retry.MessageRetryQueueDO;
import com.njcn.msgpush.module.push.dal.dataobject.retry.RetryStrategyConfigDO;
import com.njcn.msgpush.module.push.service.retry.MessageRetryQueueService;
import com.njcn.msgpush.module.push.service.retry.RetryStrategyConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.njcn.msgpush.framework.common.pojo.CommonResult.success;

/**
 * @author caozehui
 * @data 2026-02-27
 */
@Tag(name = "管理后台 - 消息重试")
@Validated
@RestController
@RequestMapping("/push/retry")
public class MessageRetryQueueController {

    @Autowired
    private MessageRetryQueueService messageRetryQueueService;
    @Autowired
    private RetryStrategyConfigService retryStrategyConfigService;

    @PostMapping("/page")
    @Operation(summary = "分页获得消息重试队列")
    @PreAuthorize("@ss.hasPermission('push:retry:page')")
    public CommonResult<PageResult<MessageRetryQueueDO>> getRetryPage(@Validated @RequestBody MessageRetryQueueReqVO reqVO) {
        PageResult<MessageRetryQueueDO> result = messageRetryQueueService.getPage(reqVO);
        return success(result);
    }

    @PostMapping("/manual")
    @Operation(summary = "批量手动重试消息")
    @PreAuthorize("@ss.hasPermission('push:retry:manual')")
    @Parameter(name = "messageIds", description = "消息ID列表", required = true)
    public CommonResult<Void> manualRetry(@RequestBody List<String> messageIds) {
        messageRetryQueueService.manualRetry(messageIds);
        return success(null);
    }

    @PostMapping("/config/list")
    @Operation(summary = "获得消息重试配置列表")
    @PreAuthorize("@ss.hasPermission('push:retry:config:list')")
    public CommonResult<List<RetryStrategyConfigDO>> listRetryConfig() {
        return success(retryStrategyConfigService.listAll());
    }

    @PostMapping("/config/update")
    @Operation(summary = "更新消息重试配置")
    @PreAuthorize("@ss.hasPermission('push:retry:config:update')")
    public CommonResult<Boolean> updateRetryConfig(@Validated @RequestBody RetryStrategyConfigReqVO retryStrategyConfigVO) {
        return success(retryStrategyConfigService.updateStrategyConfig(retryStrategyConfigVO));
    }

    @PostMapping("/config/toggle")
    @Operation(summary = "启用/禁用重试策略配置")
    @PreAuthorize("@ss.hasPermission('push:retry:config:toggle')")
    @Parameter(name = "id", description = "id", required = true)
    public CommonResult<Boolean> toggleRetryConfig(@RequestParam("id") String id) {
        return success(retryStrategyConfigService.toggleEnableField(id));
    }
}
