package com.ygtx.web.controller.ice;

import com.ygtx.common.core.domain.AjaxResult;
import com.ygtx.common.core.domain.ice.IcePendingTaskRequest;
import com.ygtx.common.core.domain.ice.IcePendingTaskResponse;
import com.ygtx.common.core.domain.ice.IcePendingTaskStatusRequest;
import com.ygtx.common.utils.StringUtils;
import com.ygtx.web.service.IcePendingTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ICE待办任务控制器
 *
 * @author ygt
 */
@RestController
@RequestMapping("/ice/pending-task")
public class IcePendingTaskController
{
    private static final Logger log = LoggerFactory.getLogger(IcePendingTaskController.class);

    @Autowired
    private IcePendingTaskService icePendingTaskService;

    /**
     * 推送待办消息到ICE
     *
     * @param request 待办消息请求对象
     * @return 操作结果
     */
    @PostMapping("/send")
    public AjaxResult sendPendingTask(@RequestBody IcePendingTaskRequest request)
    {
        try
        {
            log.info("接收推送待办消息请求，标题：{}，用户工号：{}", request.getTitle(), request.getCode());
            
            IcePendingTaskResponse response = icePendingTaskService.sendPendingTask(request);
            
            if (response.getSuccess())
            {
                log.info("成功推送待办消息到ICE，标题：{}，用户工号：{}", request.getTitle(), request.getCode());
                return AjaxResult.success(response);
            }
            else
            {
                log.error("推送待办消息到ICE失败，标题：{}，用户工号：{}，错误信息：{}", 
                        request.getTitle(), request.getCode(), response.getMsg());
                return AjaxResult.error("推送失败：" + response.getMsg());
            }
        }
        catch (Exception e)
        {
            log.error("推送待办消息到ICE异常", e);
            return AjaxResult.error("推送异常：" + e.getMessage());
        }
    }

    /**
     * 批量推送待办消息到ICE
     *
     * @param requestList 待办消息请求对象列表
     * @return 操作结果
     */
    @PostMapping("/batch-send")
    public AjaxResult batchSendPendingTask(@RequestBody List<IcePendingTaskRequest> requestList)
    {
        try
        {
            log.info("接收批量推送待办消息请求，数量：{}", requestList.size());
            
            int successCount = icePendingTaskService.batchSendPendingTask(requestList);
            int totalCount = requestList.size();
            int failCount = totalCount - successCount;
            
            log.info("批量推送待办消息完成，成功：{}，失败：{}", successCount, failCount);
            
            return AjaxResult.success(String.format("批量推送完成，成功：%d，失败：%d", successCount, failCount));
        }
        catch (Exception e)
        {
            log.error("批量推送待办消息异常", e);
            return AjaxResult.error("批量推送异常：" + e.getMessage());
        }
    }

    /**
     * 更新待办任务状态
     *
     * @param request 状态变更请求对象
     * @return 操作结果
     */
    @PutMapping("/status")
    public AjaxResult updatePendingTaskStatus(@RequestBody IcePendingTaskStatusRequest request)
    {
        try
        {
            log.info("接收更新待办任务状态请求，任务ID：{}，系统ID：{}，目标状态：{}", 
                    request.getTaskId(), request.getSystemId(), request.getIsCompleted());
            
            if (StringUtils.isEmpty(request.getTaskId()))
            {
                return AjaxResult.error("任务ID不能为空");
            }
            
            if (StringUtils.isEmpty(request.getIsCompleted()))
            {
                return AjaxResult.error("完成状态不能为空");
            }
            
            if (StringUtils.isEmpty(request.getSystemId()))
            {
                return AjaxResult.error("系统ID不能为空");
            }
            
            IcePendingTaskResponse response = icePendingTaskService.updatePendingTaskStatus(request);
            
            if (response.getSuccess())
            {
                log.info("成功更新待办任务状态，任务ID：{}，系统ID：{}，新状态：{}", 
                        request.getTaskId(), request.getSystemId(), request.getIsCompleted());
                return AjaxResult.success(response);
            }
            else
            {
                log.error("更新待办任务状态失败，任务ID：{}，系统ID：{}，错误信息：{}", 
                        request.getTaskId(), request.getSystemId(), response.getMsg());
                return AjaxResult.error("更新失败：" + response.getMsg());
            }
        }
        catch (Exception e)
        {
            log.error("更新待办任务状态异常", e);
            return AjaxResult.error("更新异常：" + e.getMessage());
        }
    }

    /**
     * 根据任务ID取消待办任务
     *
     * @param taskId 任务ID
     * @param systemId 系统ID
     * @return 操作结果
     */
    @DeleteMapping("/cancel/{taskId}")
    public AjaxResult cancelPendingTask(@PathVariable("taskId") String taskId, 
                                      @RequestParam("systemId") String systemId)
    {
        try
        {
            log.info("接收取消待办任务请求，任务ID：{}，系统ID：{}", taskId, systemId);
            
            if (StringUtils.isEmpty(taskId))
            {
                return AjaxResult.error("任务ID不能为空");
            }
            
            if (StringUtils.isEmpty(systemId))
            {
                return AjaxResult.error("系统ID不能为空");
            }
            
            IcePendingTaskResponse response = icePendingTaskService.cancelPendingTask(taskId, systemId);
            
            if (response.getSuccess())
            {
                log.info("成功取消待办任务，任务ID：{}，系统ID：{}", taskId, systemId);
                return AjaxResult.success(response);
            }
            else
            {
                log.error("取消待办任务失败，任务ID：{}，系统ID：{}，错误信息：{}", 
                        taskId, systemId, response.getMsg());
                return AjaxResult.error("取消失败：" + response.getMsg());
            }
        }
        catch (Exception e)
        {
            log.error("取消待办任务异常", e);
            return AjaxResult.error("取消异常：" + e.getMessage());
        }
    }

    /**
     * 查询待办任务状态
     *
     * @param taskId 任务ID
     * @param systemId 系统ID
     * @return 操作结果
     */
    @GetMapping("/status/{taskId}")
    public AjaxResult getPendingTaskStatus(@PathVariable("taskId") String taskId,
                                         @RequestParam("systemId") String systemId)
    {
        try
        {
            log.info("查询待办任务状态，任务ID：{}，系统ID：{}", taskId, systemId);
            
            // 注意：当前接口仅支持推送和更新状态，不支持查询状态
            // 这里返回一个提示信息
            return AjaxResult.error("当前版本不支持查询待办任务状态，请使用推送或更新状态功能");
        }
        catch (Exception e)
        {
            log.error("查询待办任务状态异常", e);
            return AjaxResult.error("查询异常：" + e.getMessage());
        }
    }
}