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

namespace HoneyBox.Admin.Business.Controllers;

/// <summary>
/// 权益等级管理控制器
/// </summary>
[Route("api/admin/business/[controller]")]
public class QyLevelController : BusinessControllerBase
{
    private readonly IQyLevelService _qyLevelService;

    public QyLevelController(IQyLevelService qyLevelService)
    {
        _qyLevelService = qyLevelService;
    }

    #region 权益等级接口

    /// <summary>
    /// 获取权益等级列表（分页）
    /// </summary>
    /// <param name="request">分页请求</param>
    /// <returns>权益等级列表</returns>
    [HttpGet]
    [BusinessPermission("qylevel:list")]
    public async Task<IActionResult> GetQyLevels([FromQuery] QyLevelListRequest request)
    {
        try
        {
            var result = await _qyLevelService.GetQyLevelsAsync(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("qylevel:list")]
    public async Task<IActionResult> GetQyLevelById(int id)
    {
        try
        {
            var result = await _qyLevelService.GetQyLevelByIdAsync(id);
            if (result == null)
            {
                return NotFoundError("权益等级不存在");
            }
            return Ok(result);
        }
        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("qylevel:edit")]
    public async Task<IActionResult> UpdateQyLevel(int id, [FromBody] QyLevelUpdateRequest request)
    {
        try
        {
            var result = await _qyLevelService.UpdateQyLevelAsync(id, request);
            return result ? Ok("更新成功") : Error(BusinessErrorCodes.InternalError, "更新失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 新增权益等级
    /// </summary>
    /// <param name="request">创建请求</param>
    /// <returns>新创建的权益等级ID</returns>
    [HttpPost]
    [BusinessPermission("qylevel:add")]
    public async Task<IActionResult> CreateQyLevel([FromBody] QyLevelCreateRequest request)
    {
        try
        {
            var id = await _qyLevelService.CreateQyLevelAsync(request);
            return Ok(new { id }, "创建成功");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 删除权益等级（软删除）
    /// </summary>
    /// <param name="id">权益等级ID</param>
    /// <returns>操作结果</returns>
    [HttpDelete("{id}")]
    [BusinessPermission("qylevel:delete")]
    public async Task<IActionResult> DeleteQyLevel(int id)
    {
        try
        {
            var result = await _qyLevelService.DeleteQyLevelAsync(id);
            return result ? Ok("删除成功") : Error(BusinessErrorCodes.InternalError, "删除失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    #endregion

    #region 权益等级奖品接口

    /// <summary>
    /// 获取权益等级奖品列表（分页）
    /// </summary>
    /// <param name="id">权益等级ID</param>
    /// <param name="request">分页请求</param>
    /// <returns>奖品列表</returns>
    [HttpGet("{id}/prizes")]
    [BusinessPermission("qylevel:list")]
    public async Task<IActionResult> GetQyLevelPrizes(int id, [FromQuery] QyLevelPrizeListRequest request)
    {
        try
        {
            var result = await _qyLevelService.GetQyLevelPrizesAsync(id, request);
            return Ok(result);
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 新增权益等级奖品
    /// </summary>
    /// <param name="id">权益等级ID</param>
    /// <param name="request">创建请求</param>
    /// <returns>新创建的奖品ID</returns>
    [HttpPost("{id}/prizes")]
    [BusinessPermission("qylevel:add")]
    public async Task<IActionResult> CreateQyLevelPrize(int id, [FromBody] QyLevelPrizeCreateRequest request)
    {
        try
        {
            var prizeId = await _qyLevelService.CreateQyLevelPrizeAsync(id, request);
            return Ok(new { id = prizeId }, "创建成功");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 更新权益等级奖品
    /// </summary>
    /// <param name="prizeId">奖品ID</param>
    /// <param name="request">更新请求</param>
    /// <returns>操作结果</returns>
    [HttpPut("prizes/{prizeId}")]
    [BusinessPermission("qylevel:edit")]
    public async Task<IActionResult> UpdateQyLevelPrize(int prizeId, [FromBody] QyLevelPrizeUpdateRequest request)
    {
        try
        {
            var result = await _qyLevelService.UpdateQyLevelPrizeAsync(prizeId, request);
            return result ? Ok("更新成功") : Error(BusinessErrorCodes.InternalError, "更新失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 删除权益等级奖品（软删除）
    /// </summary>
    /// <param name="prizeId">奖品ID</param>
    /// <returns>操作结果</returns>
    [HttpDelete("prizes/{prizeId}")]
    [BusinessPermission("qylevel:delete")]
    public async Task<IActionResult> DeleteQyLevelPrize(int prizeId)
    {
        try
        {
            var result = await _qyLevelService.DeleteQyLevelPrizeAsync(prizeId);
            return result ? Ok("删除成功") : Error(BusinessErrorCodes.InternalError, "删除失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    #endregion
}
