using Microsoft.AspNetCore.Mvc;
using QYZH.InteractiveMagazine.IService;
using QYZH.InteractiveMagazine.Models.Common;
using QYZH.InteractiveMagazine.Models.Dto;
using QYZH.InteractiveMagazine.Models.Dto.Compensation;

namespace QYZH.InteractiveMagazine.WebApi.Controllers;

/// <summary>
/// 补偿任务管理控制器
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class CompensationManageController : BaseController
{
    private readonly ICompensationManageService _compensationManageService;
    private readonly ILogger<CompensationManageController> _logger;

    public CompensationManageController(ICompensationManageService compensationManageService, ILogger<CompensationManageController> logger)
    {
        _compensationManageService = compensationManageService;
        _logger = logger;
    }

    /// <summary>
    /// 分页查询补偿任务列表
    /// </summary>
    [HttpPost("list")]
    public async Task<BaseResponse<PageListModel<CompensationManageOutput>>> GetList([FromBody] CompensationTaskQueryInput input)
    {
        try
        {
            var result = await _compensationManageService.GetListAsync(input);
            return Success(result);
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "查询补偿任务列表业务异常: {Message}", ex.Message);
            return BaseResponse<PageListModel<CompensationManageOutput>>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "查询补偿任务列表系统异常");
            return BaseResponse<PageListModel<CompensationManageOutput>>.Fail("查询补偿任务列表失败，请稍后重试");
        }
    }

    /// <summary>
    /// 获取补偿任务详情
    /// </summary>
    [HttpGet("{id}/detail")]
    public async Task<BaseResponse<CompensationManageDetailOutput>> GetDetail(long id)
    {
        try
        {
            var result = await _compensationManageService.GetDetailAsync(id);
            return Success(result);
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "获取补偿任务详情业务异常: {Message}", ex.Message);
            return BaseResponse<CompensationManageDetailOutput>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取补偿任务详情系统异常，TaskId: {TaskId}", id);
            return BaseResponse<CompensationManageDetailOutput>.Fail("获取补偿任务详情失败，请稍后重试");
        }
    }

    /// <summary>
    /// 手动重试补偿任务
    /// </summary>
    [HttpPost("{id}/retry")]
    public async Task<BaseResponse<object>> Retry(long id, [FromBody] CompensationRetryInput input)
    {
        try
        {
            var operatorId = GetCurrentUserId() ?? 0;
            var operatorName = GetCurrentUserName() ?? "Unknown";
            var ipAddress = HttpContext.Connection.RemoteIpAddress?.ToString();

            await _compensationManageService.RetryAsync(id, operatorId, operatorName, input, ipAddress);
            return Success(new object(), "补偿任务已重置为待处理状态");
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "手动重试补偿任务业务异常: {Message}", ex.Message);
            return BaseResponse<object>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "手动重试补偿任务系统异常，TaskId: {TaskId}", id);
            return BaseResponse<object>.Fail("手动重试失败，请稍后重试");
        }
    }

    /// <summary>
    /// 标记补偿任务为已解决
    /// </summary>
    [HttpPost("{id}/resolve")]
    public async Task<BaseResponse<object>> Resolve(long id, [FromBody] CompensationResolveInput input)
    {
        try
        {
            var operatorId = GetCurrentUserId() ?? 0;
            var operatorName = GetCurrentUserName() ?? "Unknown";
            var ipAddress = HttpContext.Connection.RemoteIpAddress?.ToString();

            await _compensationManageService.ResolveAsync(id, operatorId, operatorName, input, ipAddress);
            return Success(new object(), "补偿任务已标记为已解决");
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "标记补偿任务已解决业务异常: {Message}", ex.Message);
            return BaseResponse<object>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "标记补偿任务已解决系统异常，TaskId: {TaskId}", id);
            return BaseResponse<object>.Fail("标记已解决失败，请稍后重试");
        }
    }
}
