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

namespace HoneyBox.Admin.Business.Controllers;

/// <summary>
/// 福利屋入口控制器
/// </summary>
[Route("api/admin/business/[controller]")]
public class WelfareHouseController : BusinessControllerBase
{
    private readonly IWelfareHouseService _welfareHouseService;

    public WelfareHouseController(IWelfareHouseService welfareHouseService)
    {
        _welfareHouseService = welfareHouseService;
    }

    /// <summary>
    /// 获取福利屋入口列表（分页）
    /// </summary>
    /// <param name="request">分页请求</param>
    /// <returns>福利屋入口列表</returns>
    [HttpGet]
    [BusinessPermission("welfarehouse:list")]
    public async Task<IActionResult> GetWelfareHouses([FromQuery] WelfareHouseListRequest request)
    {
        try
        {
            var result = await _welfareHouseService.GetWelfareHousesAsync(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("welfarehouse:list")]
    public async Task<IActionResult> GetWelfareHouseById(int id)
    {
        try
        {
            var result = await _welfareHouseService.GetWelfareHouseByIdAsync(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("welfarehouse:add")]
    public async Task<IActionResult> CreateWelfareHouse([FromBody] WelfareHouseCreateRequest request)
    {
        try
        {
            var id = await _welfareHouseService.CreateWelfareHouseAsync(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("welfarehouse:edit")]
    public async Task<IActionResult> UpdateWelfareHouse(int id, [FromBody] WelfareHouseUpdateRequest request)
    {
        try
        {
            var result = await _welfareHouseService.UpdateWelfareHouseAsync(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("welfarehouse:delete")]
    public async Task<IActionResult> DeleteWelfareHouse(int id)
    {
        try
        {
            var result = await _welfareHouseService.DeleteWelfareHouseAsync(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("welfarehouse:edit")]
    public async Task<IActionResult> UpdateStatus(int id, [FromBody] WelfareHouseStatusRequest request)
    {
        try
        {
            var result = await _welfareHouseService.UpdateStatusAsync(id, request);
            return result ? Ok("状态更新成功") : Error(BusinessErrorCodes.InternalError, "状态更新失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }
}
