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

namespace HoneyBox.Admin.Business.Controllers;

/// <summary>
/// 钻石商品管理控制器
/// </summary>
[Route("api/admin/business/[controller]")]
public class DiamondController : BusinessControllerBase
{
    private readonly IDiamondService _diamondService;

    public DiamondController(IDiamondService diamondService)
    {
        _diamondService = diamondService;
    }

    /// <summary>
    /// 获取钻石商品列表
    /// </summary>
    /// <param name="request">查询请求</param>
    /// <returns>分页钻石商品列表</returns>
    [HttpGet]
    [BusinessPermission("diamond:list")]
    public async Task<IActionResult> GetDiamondProducts([FromQuery] DiamondProductListRequest request)
    {
        try
        {
            var result = await _diamondService.GetDiamondProductsAsync(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("diamond:list")]
    public async Task<IActionResult> GetDiamondProductById(int id)
    {
        try
        {
            var result = await _diamondService.GetDiamondProductByIdAsync(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("diamond:add")]
    public async Task<IActionResult> CreateDiamondProduct([FromBody] DiamondProductCreateRequest request)
    {
        try
        {
            var productId = await _diamondService.CreateDiamondProductAsync(request);
            return Ok(new { id = productId }, "创建成功");
        }
        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("diamond:edit")]
    public async Task<IActionResult> UpdateDiamondProduct(int id, [FromBody] DiamondProductUpdateRequest request)
    {
        try
        {
            var result = await _diamondService.UpdateDiamondProductAsync(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("diamond:delete")]
    public async Task<IActionResult> DeleteDiamondProduct(int id)
    {
        try
        {
            var result = await _diamondService.DeleteDiamondProductAsync(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("diamond:edit")]
    public async Task<IActionResult> UpdateDiamondProductStatus(int id, [FromBody] DiamondProductStatusRequest request)
    {
        try
        {
            var result = await _diamondService.UpdateDiamondProductStatusAsync(id, request.Status);
            var message = request.Status == 1 ? "启用成功" : "禁用成功";
            return result ? Ok(message) : Error(BusinessErrorCodes.InternalError, "操作失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 获取最大排序值
    /// </summary>
    /// <returns>最大排序值</returns>
    [HttpGet("max-sort")]
    [BusinessPermission("diamond:list")]
    public async Task<IActionResult> GetMaxSortOrder()
    {
        try
        {
            var maxSort = await _diamondService.GetMaxSortOrderAsync();
            return Ok(new DiamondMaxSortResponse { MaxSort = maxSort });
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }
}
