package com.sundot.airport.exam.controller;

import com.sundot.airport.common.annotation.Log;
import com.sundot.airport.common.core.controller.BaseController;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.enums.BusinessType;
import com.sundot.airport.exam.dto.NotifyTaskDTO;
import com.sundot.airport.exam.service.IDailyTaskExpireService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <b>功能名：</b>DailyTaskExpireController<br>
 * <b>说明：</b> 每日任务逾期处理Controller <br>
 * <b>著作权：</b> Copyright (C) 2025 SUNDOT CORPORATION<br>
 * <b>修改履历：
 *
 * @author Simon Lin
 */
@RestController
@RequestMapping("/exam/daily/expire")
public class DailyTaskExpireController extends BaseController {

    @Autowired
    private IDailyTaskExpireService dailyTaskExpireService;

    /**
     * 手动触发逾期任务处理（管理员功能）
     */
    @PreAuthorize("@ss.hasPermi('exam:daily:expire:process') or @ss.hasRole('test') or @ss.hasRole('zhijianke')")
    @Log(title = "手动处理逾期任务", businessType = BusinessType.UPDATE)
    @PostMapping("/process")
    public AjaxResult processExpiredTasks() {
        try {
            logger.info("管理员手动触发逾期任务处理");

            int expiredCount = dailyTaskExpireService.processExpiredTasks();

            return success("逾期任务处理完成，共处理 " + expiredCount + " 个任务");
        } catch (Exception e) {
            logger.error("手动处理逾期任务失败", e);
            return error("处理失败：" + e.getMessage());
        }
    }

    /**
     * 提醒用户完成逾期任务（单个）
     */
    @PreAuthorize("@ss.hasPermi('exam:daily:expire:notify') or @ss.hasRole('test') or @ss.hasRole('zhijianke')")
    @Log(title = "提醒用户完成逾期任务", businessType = BusinessType.OTHER)
    @PostMapping("/notify")
    public AjaxResult notifyExpiredTask(@Valid @RequestBody NotifyTaskDTO dto) {
        try {
            logger.info("提醒用户完成逾期任务 - taskId: {}, notifyType: {}", dto.getTaskId(), dto.getNotifyType());

            if (dto.getTaskId() == null || dto.getTaskId().isEmpty()) {
                return error("任务ID不能为空");
            }

            boolean result = dailyTaskExpireService.notifyExpiredTask(dto);

            if (result) {
                return success("提醒发送成功");
            } else {
                return error("提醒发送失败");
            }
        } catch (Exception e) {
            logger.error("提醒用户完成逾期任务失败", e);
            return error("提醒发送失败：" + e.getMessage());
        }
    }

    /**
     * 批量提醒用户完成逾期任务
     */
    @PreAuthorize("@ss.hasPermi('exam:daily:expire:notify') or @ss.hasRole('test') or @ss.hasRole('zhijianke')")
    @Log(title = "批量提醒用户完成逾期任务", businessType = BusinessType.OTHER)
    @PostMapping("/batch-notify")
    public AjaxResult batchNotifyExpiredTasks(@Valid @RequestBody NotifyTaskDTO dto) {
        try {
            logger.info("批量提醒用户完成逾期任务 - taskIds: {}, notifyType: {}", dto.getTaskIds(), dto.getNotifyType());

            if (dto.getTaskIds() == null || dto.getTaskIds().isEmpty()) {
                return error("任务ID列表不能为空");
            }

            int successCount = dailyTaskExpireService.batchNotifyExpiredTasks(dto);

            return success("批量提醒完成，成功发送 " + successCount + " 条提醒");
        } catch (Exception e) {
            logger.error("批量提醒用户完成逾期任务失败", e);
            return error("批量提醒发送失败：" + e.getMessage());
        }
    }
}
