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

namespace HoneyBox.Admin.Business.Controllers;

/// <summary>
/// 指定中奖配置管理控制器
/// </summary>
[Route("api/admin/goods/{goodsId}/designated-prizes")]
public class DesignatedPrizeController : BusinessControllerBase
{
    private readonly IDesignatedPrizeService _designatedPrizeService;

    public DesignatedPrizeController(IDesignatedPrizeService designatedPrizeService)
    {
        _designatedPrizeService = designatedPrizeService;
    }

    /// <summary>
    /// 获取商品的指定中奖配置列表
    /// </summary>
    /// <param name="goodsId">商品ID</param>
    /// <returns>配置列表</returns>
    [HttpGet]
    [BusinessPermission("goods:view")]
    public async Task<IActionResult> GetList(int goodsId)
    {
        try
        {
            var result = await _designatedPrizeService.GetByGoodsIdAsync(goodsId);
            return Ok(result);
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 创建指定中奖配置
    /// </summary>
    /// <param name="goodsId">商品ID</param>
    /// <param name="request">创建请求</param>
    /// <returns>创建的配置</returns>
    [HttpPost]
    [BusinessPermission("goods:edit")]
    public async Task<IActionResult> Create(int goodsId, [FromBody] CreateDesignatedPrizeRequest request)
    {
        try
        {
            var result = await _designatedPrizeService.CreateAsync(goodsId, request);
            return Ok(result, "创建成功");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 更新指定中奖配置
    /// </summary>
    /// <param name="goodsId">商品ID</param>
    /// <param name="id">配置ID</param>
    /// <param name="request">更新请求</param>
    /// <returns>更新后的配置</returns>
    [HttpPut("{id}")]
    [BusinessPermission("goods:edit")]
    public async Task<IActionResult> Update(int goodsId, int id, [FromBody] UpdateDesignatedPrizeRequest request)
    {
        try
        {
            var result = await _designatedPrizeService.UpdateAsync(id, request);
            return Ok(result, "更新成功");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 删除指定中奖配置
    /// </summary>
    /// <param name="goodsId">商品ID</param>
    /// <param name="id">配置ID</param>
    /// <returns>操作结果</returns>
    [HttpDelete("{id}")]
    [BusinessPermission("goods:edit")]
    public async Task<IActionResult> Delete(int goodsId, int id)
    {
        try
        {
            var result = await _designatedPrizeService.DeleteAsync(id);
            return result ? Ok("删除成功") : Error(BusinessErrorCodes.InternalError, "删除失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }
}
