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

namespace HoneyBox.Admin.Business.Controllers;

/// <summary>
/// 中奖公告管理控制器
/// </summary>
[Route("api/admin/announcements")]
public class AnnouncementController : BusinessControllerBase
{
    private readonly IAnnouncementService _announcementService;

    public AnnouncementController(IAnnouncementService announcementService)
    {
        _announcementService = announcementService;
    }

    /// <summary>
    /// 获取公告列表（分页）
    /// </summary>
    /// <param name="page">页码</param>
    /// <param name="pageSize">每页数量</param>
    /// <param name="keyword">搜索关键词（可选，搜索用户名或奖品名称）</param>
    /// <returns>分页公告列表</returns>
    [HttpGet]
    [BusinessPermission("announcement:list")]
    public async Task<IActionResult> GetList([FromQuery] int page = 1, [FromQuery] int pageSize = 20, [FromQuery] string? keyword = null)
    {
        try
        {
            var result = await _announcementService.GetListAsync(page, pageSize, keyword);
            return Ok(result);
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 根据ID获取公告详情
    /// </summary>
    /// <param name="id">公告ID</param>
    /// <returns>公告详情</returns>
    [HttpGet("{id}")]
    [BusinessPermission("announcement:list")]
    public async Task<IActionResult> GetById(int id)
    {
        try
        {
            var result = await _announcementService.GetByIdAsync(id);
            return Ok(result);
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 创建公告
    /// </summary>
    /// <param name="request">创建请求</param>
    /// <returns>创建的公告</returns>
    [HttpPost]
    [BusinessPermission("announcement:add")]
    public async Task<IActionResult> Create([FromBody] CreateAnnouncementRequest request)
    {
        try
        {
            var result = await _announcementService.CreateAsync(request);
            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("announcement:edit")]
    public async Task<IActionResult> Update(int id, [FromBody] UpdateAnnouncementRequest request)
    {
        try
        {
            var result = await _announcementService.UpdateAsync(id, request);
            return Ok(result, "更新成功");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }

    /// <summary>
    /// 删除公告
    /// </summary>
    /// <param name="id">公告ID</param>
    /// <returns>操作结果</returns>
    [HttpDelete("{id}")]
    [BusinessPermission("announcement:delete")]
    public async Task<IActionResult> Delete(int id)
    {
        try
        {
            var result = await _announcementService.DeleteAsync(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>
    [HttpPatch("{id}/status")]
    [BusinessPermission("announcement:edit")]
    public async Task<IActionResult> ToggleStatus(int id)
    {
        try
        {
            var result = await _announcementService.ToggleStatusAsync(id);
            return Ok(result, "状态切换成功");
        }
        catch (BusinessException ex)
        {
            return Error(ex.Code, ex.Message);
        }
    }
}
