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

namespace QYZH.InteractiveMagazine.WebApi.Controllers;

/// <summary>
/// 勋章管理控制器
/// </summary>
[Route("api/[controller]")]
[ApiController]
[ApiExplorerSettings(GroupName = nameof(ApiVersionEnum.Platform))]
public class MedalController : BaseController
{
    private readonly IMedalService _medalService;
    private readonly ILogger<MedalController> _logger;

    public MedalController(IMedalService medalService, ILogger<MedalController> logger)
    {
        _medalService = medalService;
        _logger = logger;
    }

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

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

    /// <summary>
    /// 删除勋章
    /// </summary>
    /// <param name="id">勋章ID</param>
    /// <returns>操作结果</returns>
    [HttpDelete("{id}")]
    public async Task<BaseResponse<object>> DeleteAsync(long id)
    {
        try
        {
            await _medalService.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<MedalOutput>> GetByIdAsync(long id)
    {
        try
        {
            var result = await _medalService.GetByIdAsync(id);
            return Success(result);
        }
        catch (BusinessException ex)
        {
            _logger.LogWarning(ex, "获取勋章业务异常: {Message}", ex.Message);
            return BaseResponse<MedalOutput>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取勋章系统异常，ID：{Id}", id);
            return BaseResponse<MedalOutput>.Fail("获取勋章信息失败，请稍后重试");
        }
    }

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

    /// <summary>
    /// 更新勋章启用/禁用状态
    /// </summary>
    /// <param name="id">勋章ID</param>
    /// <param name="status">状态: 0=禁用, 1=启用</param>
    /// <returns>操作结果</returns>
    [HttpPut("{id}/status")]
    public async Task<BaseResponse<bool>> UpdateStatusAsync(long id)
    {
        try
        {
            var res = await _medalService.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("更新勋章状态失败，请稍后重试");
        }
    }

    /// <summary>
    /// 获取规则配置中可用的目标表列表
    /// </summary>
    [HttpGet("rule-config/tables")]
    public async Task<BaseResponse<List<MedalRuleTableOptionOutput>>> GetAvailableTables()
    {
        try
        {
            var result = await _medalService.GetAvailableTablesAsync();
            return Success(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取可用目标表列表系统异常");
            return BaseResponse<List<MedalRuleTableOptionOutput>>.Fail("获取可用目标表列表失败，请稍后重试");
        }
    }

    /// <summary>
    /// 获取指定表的可用字段列表
    /// </summary>
    /// <param name="tableName">表名</param>
    [HttpGet("rule-config/tables/{tableName}/fields")]
    public async Task<BaseResponse<List<MedalRuleFieldOptionOutput>>> GetTableFields(string tableName)
    {
        try
        {
            var result = await _medalService.GetTableFieldsAsync(tableName);
            return Success(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取可用字段列表系统异常，表名：{TableName}", tableName);
            return BaseResponse<List<MedalRuleFieldOptionOutput>>.Fail("获取可用字段列表失败，请稍后重试");
        }
    }
}
