using HoneyBox.Admin.Business.Attributes;
using HoneyBox.Admin.Business.Models;
using HoneyBox.Admin.Business.Models.Task;
using HoneyBox.Admin.Business.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace HoneyBox.Admin.Business.Controllers;

/// <summary>
/// 任务管理控制器（后台管理）
/// </summary>
[Route("api/admin/business/task")]
public class AdminTaskController : BusinessControllerBase
{
    private readonly IAdminTaskService _taskService;

    public AdminTaskController(IAdminTaskService taskService)
    {
        _taskService = taskService;
    }

    /// <summary>
    /// 获取任务列表（分页）
    /// </summary>
    /// <param name="request">分页请求</param>
    /// <returns>任务列表</returns>
    [HttpGet]
    [BusinessPermission("task:list")]
    public async Task<IActionResult> GetTasks([FromQuery] TaskListRequest request)
    {
        try
        {
            var result = await _taskService.GetTasksAsync(request);
            return Ok(result);
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 获取任务详情
    /// </summary>
    /// <param name="id">任务ID</param>
    /// <returns>任务详情</returns>
    [HttpGet("{id}")]
    [BusinessPermission("task:list")]
    public async Task<IActionResult> GetTaskById(int id)
    {
        try
        {
            var result = await _taskService.GetTaskByIdAsync(id);
            if (result == null)
            {
                return NotFoundError("任务不存在");
            }
            return Ok(result);
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 新增任务
    /// </summary>
    /// <param name="request">创建请求</param>
    /// <returns>新创建的任务ID</returns>
    [HttpPost]
    [BusinessPermission("task:add")]
    public async Task<IActionResult> CreateTask([FromBody] TaskCreateRequest request)
    {
        try
        {
            var id = await _taskService.CreateTaskAsync(request);
            return Ok(new { id }, "创建成功");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 更新任务
    /// </summary>
    /// <param name="id">任务ID</param>
    /// <param name="request">更新请求</param>
    /// <returns>操作结果</returns>
    [HttpPut("{id}")]
    [BusinessPermission("task:edit")]
    public async Task<IActionResult> UpdateTask(int id, [FromBody] TaskUpdateRequest request)
    {
        try
        {
            var result = await _taskService.UpdateTaskAsync(id, request);
            return result ? Ok("更新成功") : Error(BusinessErrorCodes.InternalError, "更新失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 删除任务（软删除）
    /// </summary>
    /// <param name="id">任务ID</param>
    /// <returns>操作结果</returns>
    [HttpDelete("{id}")]
    [BusinessPermission("task:delete")]
    public async Task<IActionResult> DeleteTask(int id)
    {
        try
        {
            var result = await _taskService.DeleteTaskAsync(id);
            return result ? Ok("删除成功") : Error(BusinessErrorCodes.InternalError, "删除失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }
}
