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

namespace HoneyBox.Admin.Business.Controllers;

/// <summary>
/// 奖励管理控制器
/// </summary>
[Route("api/admin/business/[controller]")]
public class RewardController : BusinessControllerBase
{
    private readonly IRewardService _rewardService;

    public RewardController(IRewardService rewardService)
    {
        _rewardService = rewardService;
    }

    /// <summary>
    /// 获取奖励列表（分页）
    /// </summary>
    /// <param name="request">分页请求</param>
    /// <returns>奖励列表</returns>
    [HttpGet]
    [BusinessPermission("reward:list")]
    public async Task<IActionResult> GetRewards([FromQuery] RewardListRequest request)
    {
        try
        {
            var result = await _rewardService.GetRewardsAsync(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("reward:list")]
    public async Task<IActionResult> GetRewardById(int id)
    {
        try
        {
            var result = await _rewardService.GetRewardByIdAsync(id);
            if (result == null)
            {
                return NotFoundError("奖励不存在");
            }
            return Ok(result);
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 根据业务奖励ID获取奖励列表
    /// </summary>
    /// <param name="rewardId">业务奖励ID</param>
    /// <returns>奖励列表</returns>
    [HttpGet("by-reward-id")]
    [BusinessPermission("reward:list")]
    public async Task<IActionResult> GetRewardsByRewardId([FromQuery] string rewardId)
    {
        try
        {
            var result = await _rewardService.GetRewardsByRewardIdAsync(rewardId);
            return Ok(result);
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 新增奖励
    /// </summary>
    /// <param name="request">创建请求</param>
    /// <returns>新创建的奖励ID</returns>
    [HttpPost]
    [BusinessPermission("reward:add")]
    public async Task<IActionResult> CreateReward([FromBody] RewardCreateRequest request)
    {
        try
        {
            var id = await _rewardService.CreateRewardAsync(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("reward:edit")]
    public async Task<IActionResult> UpdateReward(int id, [FromBody] RewardUpdateRequest request)
    {
        try
        {
            var result = await _rewardService.UpdateRewardAsync(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("reward:delete")]
    public async Task<IActionResult> DeleteReward(int id)
    {
        try
        {
            var result = await _rewardService.DeleteRewardAsync(id);
            return result ? Ok("删除成功") : Error(BusinessErrorCodes.InternalError, "删除失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 批量更新奖励
    /// </summary>
    /// <param name="request">批量更新请求</param>
    /// <returns>操作结果</returns>
    [HttpPost("batch")]
    [BusinessPermission("reward:edit")]
    public async Task<IActionResult> BatchUpdateRewards([FromBody] RewardBatchRequest request)
    {
        try
        {
            var result = await _rewardService.BatchUpdateRewardsAsync(request);
            return result ? Ok("批量更新成功") : Error(BusinessErrorCodes.InternalError, "批量更新失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }
}
