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

namespace QYZH.InteractiveMagazine.WebApi.Controllers;

/// <summary>
/// 签到配置管理控制器
/// </summary>
[Route("api/[controller]")]
[ApiController]
[ApiExplorerSettings(GroupName = nameof(ApiVersionEnum.Platform))]
public class CheckInConfigController : BaseController
{
    private readonly ICheckInConfigService _checkInConfigService;
    private readonly ILogger<CheckInConfigController> _logger;

    public CheckInConfigController(ICheckInConfigService checkInConfigService, ILogger<CheckInConfigController> logger)
    {
        _checkInConfigService = checkInConfigService;
        _logger = logger;
    }

    /// <summary>
    /// 创建签到配置
    /// </summary>
    /// <param name="input">签到配置信息</param>
    /// <returns>创建的签到配置信息</returns>
    [HttpPost]
    public async Task<BaseResponse<CheckInConfigOutput>> CreateAsync([FromBody] CheckInConfigInput input)
    {
        try
        {
            var result = await _checkInConfigService.CreateAsync(input);
            return Success(result, "创建签到配置成功");
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "创建签到配置业务异常: {Message}", ex.Message);
            return BaseResponse<CheckInConfigOutput>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "创建签到配置系统异常，参数：{Input}", input);
            return BaseResponse<CheckInConfigOutput>.Fail("创建签到配置失败，请稍后重试");
        }
    }

    /// <summary>
    /// 更新签到配置
    /// </summary>
    /// <param name="id">签到配置ID</param>
    /// <param name="input">签到配置信息</param>
    /// <returns>更新后的签到配置信息</returns>
    [HttpPut("{id}")]
    public async Task<BaseResponse<CheckInConfigOutput>> UpdateAsync(long id, [FromBody] CheckInConfigInput input)
    {
        try
        {
            var result = await _checkInConfigService.UpdateAsync(id, input);
            return Success(result, "更新签到配置成功");
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "更新签到配置业务异常: {Message}", ex.Message);
            return BaseResponse<CheckInConfigOutput>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "更新签到配置系统异常，ID：{Id}，参数：{Input}", id, input);
            return BaseResponse<CheckInConfigOutput>.Fail("更新签到配置失败，请稍后重试");
        }
    }

    /// <summary>
    /// 删除签到配置（软删除）
    /// </summary>
    /// <param name="id">签到配置ID</param>
    /// <returns>操作结果</returns>
    [HttpDelete("{id}")]
    public async Task<BaseResponse<object>> DeleteAsync(long id)
    {
        try
        {
            await _checkInConfigService.DeleteAsync(id);
            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, "删除签到配置系统异常，ID：{Id}", id);
            return BaseResponse<object>.Fail("删除签到配置失败，请稍后重试");
        }
    }

    /// <summary>
    /// 根据ID获取签到配置
    /// </summary>
    /// <param name="id">签到配置ID</param>
    /// <returns>签到配置信息</returns>
    [HttpGet("{id}")]
    public async Task<BaseResponse<CheckInConfigOutput>> GetByIdAsync(long id)
    {
        try
        {
            var result = await _checkInConfigService.GetByIdAsync(id);
            return Success(result);
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "获取签到配置业务异常: {Message}", ex.Message);
            return BaseResponse<CheckInConfigOutput>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取签到配置系统异常，ID：{Id}", id);
            return BaseResponse<CheckInConfigOutput>.Fail("获取签到配置信息失败，请稍后重试");
        }
    }

    /// <summary>
    /// 分页查询签到配置列表
    /// </summary>
    /// <param name="input">查询条件</param>
    /// <returns>分页结果</returns>
    [HttpPost("list")]
    public async Task<BaseResponse<PageListModel<CheckInConfigOutput>>> GetListAsync([FromBody] CheckInConfigQueryInput input)
    {
        try
        {
            var result = await _checkInConfigService.GetListAsync(input);
            return Success(result);
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "查询签到配置列表业务异常: {Message}", ex.Message);
            return BaseResponse<PageListModel<CheckInConfigOutput>>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "查询签到配置列表系统异常，参数：{Input}", input);
            return BaseResponse<PageListModel<CheckInConfigOutput>>.Fail("查询签到配置列表失败，请稍后重试");
        }
    }

    /// <summary>
    /// 更新签到配置启用/禁用状态
    /// </summary>
    /// <param name="id">签到配置ID</param>
    /// <returns>操作结果</returns>
    [HttpPut("{id}/status")]
    public async Task<BaseResponse<bool>> UpdateStatusAsync(long id)
    {
        try
        {
            var res = await _checkInConfigService.UpdateStatusAsync(id);
            return Success(res, "更新签到配置状态成功");
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "更新签到配置状态业务异常: {Message}", ex.Message);
            return BaseResponse<bool>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "更新签到配置状态系统异常，ID：{Id}", id);
            return BaseResponse<bool>.Fail("更新签到配置状态失败，请稍后重试");
        }
    }
}
