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

namespace HoneyBox.Admin.Business.Controllers;

/// <summary>
/// 签到配置管理控制器
/// </summary>
[Route("api/admin/business/[controller]")]
public class SignConfigController : BusinessControllerBase
{
    private readonly ISignConfigService _signConfigService;

    public SignConfigController(ISignConfigService signConfigService)
    {
        _signConfigService = signConfigService;
    }

    /// <summary>
    /// 获取签到配置列表（分页）
    /// </summary>
    /// <param name="request">分页请求</param>
    /// <returns>签到配置列表</returns>
    [HttpGet]
    [BusinessPermission("signconfig:list")]
    public async Task<IActionResult> GetSignConfigs([FromQuery] SignConfigListRequest request)
    {
        try
        {
            var result = await _signConfigService.GetSignConfigsAsync(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("signconfig:list")]
    public async Task<IActionResult> GetSignConfigById(int id)
    {
        try
        {
            var result = await _signConfigService.GetSignConfigByIdAsync(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("signconfig:add")]
    public async Task<IActionResult> CreateSignConfig([FromBody] SignConfigCreateRequest request)
    {
        try
        {
            var id = await _signConfigService.CreateSignConfigAsync(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("signconfig:edit")]
    public async Task<IActionResult> UpdateSignConfig(int id, [FromBody] SignConfigUpdateRequest request)
    {
        try
        {
            var result = await _signConfigService.UpdateSignConfigAsync(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("signconfig:delete")]
    public async Task<IActionResult> DeleteSignConfig(int id)
    {
        try
        {
            var result = await _signConfigService.DeleteSignConfigAsync(id);
            return result ? Ok("删除成功") : Error(BusinessErrorCodes.InternalError, "删除失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 更新签到配置状态
    /// </summary>
    /// <param name="id">签到配置ID</param>
    /// <param name="request">状态更新请求</param>
    /// <returns>操作结果</returns>
    [HttpPut("{id}/status")]
    [BusinessPermission("signconfig:edit")]
    public async Task<IActionResult> UpdateSignConfigStatus(int id, [FromBody] SignConfigStatusRequest request)
    {
        try
        {
            var result = await _signConfigService.UpdateSignConfigStatusAsync(id, request.Status);
            return result ? Ok("状态更新成功") : Error(BusinessErrorCodes.InternalError, "状态更新失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 更新签到配置排序
    /// </summary>
    /// <param name="id">签到配置ID</param>
    /// <param name="request">排序更新请求</param>
    /// <returns>操作结果</returns>
    [HttpPut("{id}/sort")]
    [BusinessPermission("signconfig:edit")]
    public async Task<IActionResult> UpdateSignConfigSort(int id, [FromBody] SignConfigSortRequest request)
    {
        try
        {
            var result = await _signConfigService.UpdateSignConfigSortAsync(id, request.Sort);
            return result ? Ok("排序更新成功") : Error(BusinessErrorCodes.InternalError, "排序更新失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 更新签到配置奖励
    /// </summary>
    /// <param name="id">签到配置ID</param>
    /// <param name="request">奖励更新请求</param>
    /// <returns>操作结果</returns>
    [HttpPut("{id}/reward")]
    [BusinessPermission("signconfig:edit")]
    public async Task<IActionResult> UpdateSignConfigReward(int id, [FromBody] SignConfigRewardRequest request)
    {
        try
        {
            var result = await _signConfigService.UpdateSignConfigRewardAsync(id, request);
            return result ? Ok("奖励更新成功") : Error(BusinessErrorCodes.InternalError, "奖励更新失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }
}
