using Asp.Versioning;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using IamService.API.Application.Common;
using IamService.API.Application.Queries.Audit;

namespace IamService.API.Controllers;

[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/audit")]
[Authorize(AuthenticationSchemes = "Bearer")]
[Authorize(Policy = "RequireAuditor")]
[SwaggerTag("Audit log management - requires Auditor role")]
public class AuditController : ControllerBase
{
    private readonly IMediator _mediator;
    public AuditController(IMediator mediator) => _mediator = mediator;

    [HttpGet("logs")]
    [SwaggerOperation(Summary = "Get audit logs", OperationId = "GetAuditLogs")]
    public async Task<IActionResult> GetLogs(
        [FromQuery] DateTime? fromDate,
        [FromQuery] DateTime? toDate,
        [FromQuery] int? eventTypeId,
        [FromQuery] Guid? actorId,
        [FromQuery] string? resourceType,
        [FromQuery] int skip = 0,
        [FromQuery] int take = 50,
        CancellationToken ct = default)
    {
        var result = await _mediator.Send(new GetAuditLogsQuery(fromDate, toDate, eventTypeId, actorId, resourceType, skip, take), ct);
        return Ok(ApiResponse<AuditLogsResult>.Ok(result));
    }
}
