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

namespace HoneyBox.Admin.Business.Controllers;

/// <summary>
/// 悬浮球配置控制器
/// </summary>
[Route("api/admin/business/[controller]")]
public class FloatBallController : BusinessControllerBase
{
    private readonly IFloatBallService _floatBallService;

    public FloatBallController(IFloatBallService floatBallService)
    {
        _floatBallService = floatBallService;
    }

    /// <summary>
    /// 获取悬浮球列表（分页）
    /// </summary>
    /// <param name="request">分页请求</param>
    /// <returns>悬浮球列表</returns>
    [HttpGet]
    [BusinessPermission("floatball:list")]
    public async Task<IActionResult> GetFloatBalls([FromQuery] FloatBallListRequest request)
    {
        try
        {
            var result = await _floatBallService.GetFloatBallsAsync(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("floatball:list")]
    public async Task<IActionResult> GetFloatBallById(int id)
    {
        try
        {
            var result = await _floatBallService.GetFloatBallByIdAsync(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("floatball:add")]
    public async Task<IActionResult> CreateFloatBall([FromBody] FloatBallCreateRequest request)
    {
        try
        {
            var id = await _floatBallService.CreateFloatBallAsync(request);
            return Ok(new { id }, "创建成功");
        }
        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("floatball:edit")]
    public async Task<IActionResult> UpdateFloatBall(int id, [FromBody] FloatBallUpdateRequest request)
    {
        try
        {
            var result = await _floatBallService.UpdateFloatBallAsync(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("floatball:delete")]
    public async Task<IActionResult> DeleteFloatBall(int id)
    {
        try
        {
            var result = await _floatBallService.DeleteFloatBallAsync(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("floatball:edit")]
    public async Task<IActionResult> UpdateStatus(int id, [FromBody] FloatBallStatusRequest request)
    {
        try
        {
            var result = await _floatBallService.UpdateStatusAsync(id, request);
            return result ? Ok("状态更新成功") : Error(BusinessErrorCodes.InternalError, "状态更新失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }
}
