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

namespace HoneyBox.Admin.Business.Controllers;

/// <summary>
/// 单页管理控制器
/// </summary>
[Route("api/admin/business/[controller]")]
public class DanyeController : BusinessControllerBase
{
    private readonly IDanyeService _danyeService;

    public DanyeController(IDanyeService danyeService)
    {
        _danyeService = danyeService;
    }

    /// <summary>
    /// 获取单页列表
    /// </summary>
    /// <returns>单页列表</returns>
    [HttpGet]
    [BusinessPermission("danye:list")]
    public async Task<IActionResult> GetDanyeList()
    {
        try
        {
            var result = await _danyeService.GetDanyeListAsync();
            return Ok(result);
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 获取单页详情
    /// </summary>
    /// <param name="id">单页ID</param>
    /// <returns>单页详情</returns>
    [HttpGet("{id}")]
    [BusinessPermission("danye:list")]
    public async Task<IActionResult> GetDanyeById(int id)
    {
        try
        {
            var result = await _danyeService.GetDanyeByIdAsync(id);
            if (result == null)
            {
                return NotFoundError("单页不存在");
            }
            return Ok(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}")]
    [BusinessPermission("danye:edit")]
    public async Task<IActionResult> UpdateDanye(int id, [FromBody] DanyeUpdateRequest request)
    {
        try
        {
            var result = await _danyeService.UpdateDanyeAsync(id, request);
            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}/image-optimizer")]
    [BusinessPermission("danye:edit")]
    public async Task<IActionResult> ToggleImageOptimizer(int id, [FromBody] ImageOptimizerRequest request)
    {
        try
        {
            var result = await _danyeService.ToggleImageOptimizerAsync(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>
    [HttpPut("{id}/clear-content")]
    [BusinessPermission("danye:edit")]
    public async Task<IActionResult> ClearContent(int id)
    {
        try
        {
            var result = await _danyeService.ClearContentAsync(id);
            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("danye:delete")]
    public async Task<IActionResult> DeleteDanye(int id)
    {
        try
        {
            var result = await _danyeService.DeleteDanyeAsync(id);
            return result ? Ok("删除成功") : Error(BusinessErrorCodes.InternalError, "删除失败");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }
}
