using Microsoft.AspNetCore.Authorization;

namespace WebApi.Controllers;

[Authorize]
[ApiController]
[Route("api/[controller]/[action]")]
public class MessageRecordController(IMessageRecordService messageService, ILogger<MessageRecordController> logger) : ControllerBase
{
    /// <summary>
    /// 获取消息列表
    /// </summary>
    [HttpGet]
    public async Task<PagedResponse<MessageRecord>> GetList([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 20)
    {
        try
        {
            var (list, total) = await messageService.GetPagedListAsync(pageIndex, pageSize);
            return PagedResponse<MessageRecord>.Done(list.ToArray(), (int)total);
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "获取消息列表失败");
            return PagedResponse<MessageRecord>.Fail($"获取消息列表失败: {ex.Message}");
        }
    }

    /// <summary>
    /// 获取未读消息数量
    /// </summary>
    [HttpGet]
    public async Task<BaseResponse<long>> GetUnreadCount()
    {
        try
        {
            var count = await messageService.GetUnreadCountAsync();
            return count.Ok();
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "获取未读消息数量失败");
            return $"获取未读消息数量失败: {ex.Message}".Fail<long>();
        }
    }

    /// <summary>
    /// 标记已读
    /// </summary>
    [HttpPost]
    public async Task<BaseResponse<bool>> MarkAsRead([FromQuery] long id)
    {
        try
        {
            var result = await messageService.MarkAsReadAsync(id);
            return result.Ok();
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "标记消息已读失败，ID: {Id}", id);
            return $"标记消息已读失败: {ex.Message}".Fail<bool>();
        }
    }

    /// <summary>
    /// 全部标记已读
    /// </summary>
    [HttpPost]
    public async Task<BaseResponse<bool>> MarkAllAsRead()
    {
        try
        {
            var result = await messageService.MarkAllAsReadAsync();
            return result.Ok();
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "全部标记已读失败");
            return $"全部标记已读失败: {ex.Message}".Fail<bool>();
        }
    }

    /// <summary>
    /// 删除消息
    /// </summary>
    [HttpPost]
    public async Task<BaseResponse<bool>> Delete([FromQuery] long id)
    {
        try
        {
            var result = await messageService.DeleteAsync(id);
            return result.Ok();
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "删除消息失败，ID: {Id}", id);
            return $"删除消息失败: {ex.Message}".Fail<bool>();
        }
    }

    /// <summary>
    /// 新增消息 (测试用)
    /// </summary>
    [HttpPost]
    public async Task<BaseResponse<bool>> Add([FromBody] MessageRecord message)
    {
        try
        {
            var result = await messageService.AddAsync(message);
            return result.Ok();
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "新增消息失败");
            return $"新增消息失败: {ex.Message}".Fail<bool>();
        }
    }
}
