using HoneyBox.Admin.Business.Attributes;
using HoneyBox.Admin.Business.Models;
using HoneyBox.Admin.Business.Models.Config;
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/goods-types")]
public class GoodsTypesController : BusinessControllerBase
{
    private readonly IGoodsService _goodsService;
    private readonly IAdminConfigService _configService;

    public GoodsTypesController(IGoodsService goodsService, IAdminConfigService configService)
    {
        _goodsService = goodsService;
        _configService = configService;
    }

    /// <summary>
    /// 获取盒子类型列表
    /// </summary>
    /// <returns>盒子类型列表及货币名称配置</returns>
    [HttpGet]
    [BusinessPermission("goods:view")]
    public async Task<IActionResult> GetGoodsTypes()
    {
        try
        {
            var types = await _goodsService.GetGoodsTypesAsync();
            
            // 获取货币名称配置
            var appSetting = await _configService.GetConfigAsync<AppSetting>(ConfigKeys.AppSetting);
            
            var result = new GoodsTypesResponse
            {
                List = types,
                CurrencyNames = new CurrencyNamesDto
                {
                    BalanceName = appSetting?.BalanceName ?? "余额",
                    Currency1Name = appSetting?.Currency1Name ?? "吧唧币",
                    Currency2Name = appSetting?.Currency2Name ?? "哈尼券"
                }
            };
            
            return Ok(result);
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 创建盒子类型
    /// </summary>
    /// <param name="request">创建请求</param>
    /// <returns>新类型ID</returns>
    [HttpPost]
    [BusinessPermission("goods:type:create")]
    public async Task<IActionResult> CreateGoodsType([FromBody] GoodsTypeCreateRequest request)
    {
        try
        {
            var typeId = await _goodsService.CreateGoodsTypeAsync(request);
            return Ok(new { id = typeId, message = "创建成功" });
        }
        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:type:update")]
    public async Task<IActionResult> UpdateGoodsType(int id, [FromBody] GoodsTypeUpdateRequest request)
    {
        try
        {
            var result = await _goodsService.UpdateGoodsTypeAsync(id, request);
            return Ok(new { success = result, message = result ? "更新成功" : "更新失败" });
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 删除盒子类型
    /// </summary>
    /// <param name="id">类型ID</param>
    /// <returns>操作结果</returns>
    [HttpDelete("{id}")]
    [BusinessPermission("goods:type:delete")]
    public async Task<IActionResult> DeleteGoodsType(int id)
    {
        try
        {
            var result = await _goodsService.DeleteGoodsTypeAsync(id);
            return Ok(new { success = result, message = 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}/status")]
    [BusinessPermission("goods:type:update")]
    public async Task<IActionResult> SetGoodsTypeStatus(int id, [FromBody] GoodsTypeStatusRequest request)
    {
        try
        {
            var result = await _goodsService.SetGoodsTypeStatusAsync(id, request);
            return Ok(new { success = result, message = result ? "操作成功" : "操作失败" });
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }
}
