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

namespace HoneyBox.Admin.Business.Controllers;

/// <summary>
/// 奖品等级管理控制器
/// </summary>
[ApiController]
[Route("api/admin/business/prize-levels")]
[Authorize]
public class PrizeLevelController : BusinessControllerBase
{
    private readonly IPrizeLevelService _prizeLevelService;

    public PrizeLevelController(IPrizeLevelService prizeLevelService)
    {
        _prizeLevelService = prizeLevelService;
    }

    /// <summary>
    /// 获取奖品等级列表
    /// </summary>
    [HttpGet]
    [BusinessPermission("goods:prize-level")]
    public async Task<IActionResult> GetPrizeLevels([FromQuery] PrizeLevelListRequest request)
    {
        var result = await _prizeLevelService.GetPrizeLevelsAsync(request);
        return Ok(result);
    }

    /// <summary>
    /// 获取奖品等级详情
    /// </summary>
    [HttpGet("{id}")]
    [BusinessPermission("goods:prize-level")]
    public async Task<IActionResult> GetPrizeLevel(int id)
    {
        var result = await _prizeLevelService.GetPrizeLevelAsync(id);
        if (result == null)
        {
            return Error(BusinessErrorCodes.NotFound, "奖品等级不存在");
        }
        return Ok(result);
    }

    /// <summary>
    /// 创建奖品等级
    /// </summary>
    [HttpPost]
    [BusinessPermission("goods:prize-level")]
    public async Task<IActionResult> CreatePrizeLevel([FromBody] PrizeLevelCreateRequest request)
    {
        if (string.IsNullOrWhiteSpace(request.Title))
        {
            return Error(BusinessErrorCodes.ValidationFailed, "请输入等级名称");
        }

        var id = await _prizeLevelService.CreatePrizeLevelAsync(request);
        return Ok(new { id });
    }

    /// <summary>
    /// 更新奖品等级
    /// </summary>
    [HttpPut("{id}")]
    [BusinessPermission("goods:prize-level")]
    public async Task<IActionResult> UpdatePrizeLevel(int id, [FromBody] PrizeLevelUpdateRequest request)
    {
        if (string.IsNullOrWhiteSpace(request.Title))
        {
            return Error(BusinessErrorCodes.ValidationFailed, "请输入等级名称");
        }

        var result = await _prizeLevelService.UpdatePrizeLevelAsync(id, request);
        if (!result)
        {
            return Error(BusinessErrorCodes.NotFound, "奖品等级不存在");
        }
        return Ok("更新成功");
    }

    /// <summary>
    /// 删除奖品等级
    /// </summary>
    [HttpDelete("{id}")]
    [BusinessPermission("goods:prize-level")]
    public async Task<IActionResult> DeletePrizeLevel(int id)
    {
        try
        {
            var result = await _prizeLevelService.DeletePrizeLevelAsync(id);
            if (!result)
            {
                return Error(BusinessErrorCodes.NotFound, "奖品等级不存在");
            }
            return Ok("删除成功");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 获取所有奖品等级选项（用于下拉选择）
    /// </summary>
    [HttpGet("options")]
    [BusinessPermission("goods:list")]
    public async Task<IActionResult> GetPrizeLevelOptions()
    {
        var result = await _prizeLevelService.GetPrizeLevelOptionsAsync();
        return Ok(result);
    }

    /// <summary>
    /// 根据盒子类型获取奖品等级选项
    /// </summary>
    /// <param name="request">请求参数</param>
    /// <returns>奖品等级选项列表</returns>
    [HttpGet("options-by-type")]
    [BusinessPermission("goods:list")]
    public async Task<IActionResult> GetPrizeLevelOptionsByType([FromQuery] PrizeLevelOptionsByTypeRequest request)
    {
        var result = await _prizeLevelService.GetPrizeLevelOptionsByTypeAsync(request.GoodsType, request.IsChildPrize);
        return Ok(result);
    }

    /// <summary>
    /// 获取概率统计
    /// </summary>
    [HttpGet("stats")]
    [BusinessPermission("goods:prize-level")]
    public async Task<IActionResult> GetPrizeLevelStats()
    {
        var totalPro = await _prizeLevelService.GetTotalProbabilityAsync();
        return Ok(new { totalProbability = totalPro });
    }
}
