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

namespace HoneyBox.Admin.Business.Controllers;

/// <summary>
/// 商品管理控制器
/// </summary>
[Route("api/admin/business/[controller]")]
public class GoodsController : BusinessControllerBase
{
    private readonly IGoodsService _goodsService;

    public GoodsController(IGoodsService goodsService)
    {
        _goodsService = goodsService;
    }

    #region 商品管理

    /// <summary>
    /// 获取商品列表
    /// </summary>
    /// <param name="request">查询请求</param>
    /// <returns>分页商品列表</returns>
    [HttpGet]
    [BusinessPermission("goods:list")]
    public async Task<IActionResult> GetGoodsList([FromQuery] GoodsListRequest request)
    {
        try
        {
            var result = await _goodsService.GetGoodsListAsync(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("goods:view")]
    public async Task<IActionResult> GetGoodsDetail(int id)
    {
        try
        {
            var result = await _goodsService.GetGoodsDetailAsync(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("goods:add")]
    public async Task<IActionResult> CreateGoods([FromBody] GoodsCreateRequest request)
    {
        try
        {
            var operatorId = (int)(GetCurrentUserId() ?? 0);
            var goodsId = await _goodsService.CreateGoodsAsync(request, operatorId);
            return Ok(new { id = goodsId }, "创建成功");
        }
        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("goods:edit")]
    public async Task<IActionResult> UpdateGoods(int id, [FromBody] GoodsUpdateRequest request)
    {
        try
        {
            var operatorId = (int)(GetCurrentUserId() ?? 0);
            var result = await _goodsService.UpdateGoodsAsync(id, request, operatorId);
            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("goods:delete")]
    public async Task<IActionResult> DeleteGoods(int id)
    {
        try
        {
            var operatorId = (int)(GetCurrentUserId() ?? 0);
            var result = await _goodsService.DeleteGoodsAsync(id, operatorId);
            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("goods:status")]
    public async Task<IActionResult> SetGoodsStatus(int id, [FromBody] GoodsStatusRequest request)
    {
        try
        {
            var operatorId = (int)(GetCurrentUserId() ?? 0);
            var result = await _goodsService.SetGoodsStatusAsync(id, request.Status, operatorId);
            var message = request.Status == 1 ? "上架成功" : "下架成功";
            return result ? Ok(message) : Error(BusinessErrorCodes.InternalError, "操作失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    #endregion

    #region 奖品管理

    /// <summary>
    /// 获取商品奖品列表
    /// </summary>
    /// <param name="id">商品ID</param>
    /// <returns>奖品列表</returns>
    [HttpGet("{id}/prizes")]
    [BusinessPermission("goods:view")]
    public async Task<IActionResult> GetPrizes(int id)
    {
        try
        {
            var result = await _goodsService.GetPrizesAsync(id);
            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("goods:edit")]
    public async Task<IActionResult> AddPrize(int id, [FromBody] PrizeCreateRequest request)
    {
        try
        {
            var prizeId = await _goodsService.AddPrizeAsync(id, request);
            return Ok(new { id = prizeId }, "添加成功");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    #endregion

    #region 盒子扩展设置

    /// <summary>
    /// 获取盒子扩展设置
    /// </summary>
    /// <param name="id">盒子ID</param>
    /// <returns>扩展设置（如无独立配置则返回类型默认配置）</returns>
    [HttpGet("{id}/extend")]
    [BusinessPermission("goods:view")]
    public async Task<IActionResult> GetGoodsExtend(int id)
    {
        try
        {
            var result = await _goodsService.GetGoodsExtendAsync(id);
            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}/extend")]
    [BusinessPermission("goods:edit")]
    public async Task<IActionResult> UpdateGoodsExtend(int id, [FromBody] GoodsExtendUpdateRequest request)
    {
        try
        {
            var result = await _goodsService.UpdateGoodsExtendAsync(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}/extend")]
    [BusinessPermission("goods:edit")]
    public async Task<IActionResult> DeleteGoodsExtend(int id)
    {
        try
        {
            var result = await _goodsService.DeleteGoodsExtendAsync(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>
    /// <returns>新盒子ID</returns>
    [HttpPost("{id}/copy")]
    [BusinessPermission("goods:add")]
    public async Task<IActionResult> CopyGoods(int id)
    {
        try
        {
            var operatorId = (int)(GetCurrentUserId() ?? 0);
            var newGoodsId = await _goodsService.CopyGoodsAsync(id, operatorId);
            return Ok(new { id = newGoodsId }, "复制成功");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 清空盒子抽奖记录
    /// </summary>
    /// <param name="id">盒子ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("{id}/clear-lottery")]
    [BusinessPermission("goods:edit")]
    public async Task<IActionResult> ClearGoodsLottery(int id)
    {
        try
        {
            var operatorId = (int)(GetCurrentUserId() ?? 0);
            var result = await _goodsService.ClearGoodsLotteryAsync(id, operatorId);
            return result ? Ok("清空成功") : Error(BusinessErrorCodes.InternalError, "清空失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    #endregion
}
