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

namespace HoneyBox.Admin.Business.Controllers;

/// <summary>
/// 广告管理控制器
/// </summary>
[Route("api/admin/business/[controller]")]
public class AdvertController : BusinessControllerBase
{
    private readonly IAdvertService _advertService;

    public AdvertController(IAdvertService advertService)
    {
        _advertService = advertService;
    }

    #region 广告管理

    /// <summary>
    /// 获取广告列表
    /// </summary>
    /// <param name="request">查询请求</param>
    /// <returns>分页广告列表</returns>
    [HttpGet]
    [BusinessPermission("advert:list")]
    public async Task<IActionResult> GetAdverts([FromQuery] AdvertListRequest request)
    {
        try
        {
            var result = await _advertService.GetAdvertsAsync(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("advert:list")]
    public async Task<IActionResult> GetAdvertById(int id)
    {
        try
        {
            var result = await _advertService.GetAdvertByIdAsync(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("advert:add")]
    public async Task<IActionResult> CreateAdvert([FromBody] AdvertCreateRequest request)
    {
        try
        {
            var advertId = await _advertService.CreateAdvertAsync(request);
            return Ok(new { id = advertId }, "创建成功");
        }
        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("advert:edit")]
    public async Task<IActionResult> UpdateAdvert(int id, [FromBody] AdvertUpdateRequest request)
    {
        try
        {
            var result = await _advertService.UpdateAdvertAsync(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("advert:delete")]
    public async Task<IActionResult> DeleteAdvert(int id)
    {
        try
        {
            var result = await _advertService.DeleteAdvertAsync(id);
            return result ? Ok("删除成功") : Error(BusinessErrorCodes.InternalError, "删除失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    #endregion

    #region 广告类型管理

    /// <summary>
    /// 获取广告类型列表
    /// </summary>
    /// <returns>广告类型列表</returns>
    [HttpGet("types")]
    [BusinessPermission("advert:list")]
    public async Task<IActionResult> GetAdvertTypes()
    {
        try
        {
            var result = await _advertService.GetAdvertTypesAsync();
            return Ok(result);
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 创建广告类型
    /// </summary>
    /// <param name="request">创建请求</param>
    /// <returns>新类型ID</returns>
    [HttpPost("types")]
    [BusinessPermission("advert:add")]
    public async Task<IActionResult> CreateAdvertType([FromBody] AdvertTypeCreateRequest request)
    {
        try
        {
            var typeId = await _advertService.CreateAdvertTypeAsync(request);
            return Ok(new { id = typeId }, "创建成功");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 更新广告类型
    /// </summary>
    /// <param name="id">类型ID</param>
    /// <param name="request">更新请求</param>
    /// <returns>操作结果</returns>
    [HttpPut("types/{id}")]
    [BusinessPermission("advert:edit")]
    public async Task<IActionResult> UpdateAdvertType(int id, [FromBody] AdvertTypeUpdateRequest request)
    {
        try
        {
            var result = await _advertService.UpdateAdvertTypeAsync(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("types/{id}")]
    [BusinessPermission("advert:delete")]
    public async Task<IActionResult> DeleteAdvertType(int id)
    {
        try
        {
            var result = await _advertService.DeleteAdvertTypeAsync(id);
            return result ? Ok("删除成功") : Error(BusinessErrorCodes.InternalError, "删除失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    #endregion
}
