using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MilitaryTrainingManagement.Services.Interfaces;

namespace MilitaryTrainingManagement.Controllers;

/// <summary>
/// 审计日志控制器
/// </summary>
[Authorize]
public class AuditLogsController : BaseApiController
{
    private readonly IAuditService _auditService;

    public AuditLogsController(IAuditService auditService)
    {
        _auditService = auditService;
    }

    /// <summary>
    /// 获取审计日志列表（分页）
    /// </summary>
    [HttpGet]
    public async Task<IActionResult> GetAll([FromQuery] AuditLogQueryParameters parameters)
    {
        var logs = await _auditService.GetLogsAsync(parameters);
        var totalCount = await _auditService.GetLogCountAsync(parameters);
        
        var pageSize = parameters.PageSize;
        var pageNumber = parameters.PageNumber;
        var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);

        return Ok(new
        {
            items = logs,
            totalCount,
            pageNumber,
            pageSize,
            totalPages
        });
    }

    /// <summary>
    /// 根据ID获取审计日志详情
    /// </summary>
    [HttpGet("{id}")]
    public async Task<IActionResult> GetById(int id)
    {
        var log = await _auditService.GetLogByIdAsync(id);
        if (log == null)
        {
            return NotFound(new { message = "审计日志不存在" });
        }
        return Ok(log);
    }
}
