using ColaFlow.Modules.ProjectManagement.Application.Queries.AuditLogs;
using ColaFlow.Modules.ProjectManagement.Application.Queries.AuditLogs.GetAuditLogById;
using ColaFlow.Modules.ProjectManagement.Application.Queries.AuditLogs.GetAuditLogsByEntity;
using ColaFlow.Modules.ProjectManagement.Application.Queries.AuditLogs.GetRecentAuditLogs;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ColaFlow.API.Controllers;

/// <summary>
/// Audit Logs API Controller
/// Provides read-only access to audit history for entities
/// </summary>
[ApiController]
[Route("api/v1/[controller]")]
[Authorize]
public class AuditLogsController(IMediator mediator) : ControllerBase
{
    private readonly IMediator _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));

    /// <summary>
    /// Get a specific audit log by ID
    /// </summary>
    /// <param name="id">Audit log ID</param>
    /// <param name="cancellationToken">Cancellation token</param>
    /// <returns>Audit log details</returns>
    [HttpGet("{id:guid}")]
    [ProducesResponseType(typeof(AuditLogDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> GetById(Guid id, CancellationToken cancellationToken = default)
    {
        var query = new GetAuditLogByIdQuery(id);
        var result = await _mediator.Send(query, cancellationToken);

        if (result == null)
            return NotFound();

        return Ok(result);
    }

    /// <summary>
    /// Get audit history for a specific entity
    /// </summary>
    /// <param name="entityType">Entity type (e.g., "Project", "Epic", "Story", "WorkTask")</param>
    /// <param name="entityId">Entity ID</param>
    /// <param name="cancellationToken">Cancellation token</param>
    /// <returns>List of audit logs for the entity</returns>
    [HttpGet("entity/{entityType}/{entityId:guid}")]
    [ProducesResponseType(typeof(IReadOnlyList<AuditLogDto>), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetByEntity(
        string entityType,
        Guid entityId,
        CancellationToken cancellationToken = default)
    {
        var query = new GetAuditLogsByEntityQuery(entityType, entityId);
        var result = await _mediator.Send(query, cancellationToken);
        return Ok(result);
    }

    /// <summary>
    /// Get recent audit logs across all entities
    /// </summary>
    /// <param name="count">Number of recent logs to retrieve (default: 100, max: 1000)</param>
    /// <param name="cancellationToken">Cancellation token</param>
    /// <returns>List of recent audit logs</returns>
    [HttpGet("recent")]
    [ProducesResponseType(typeof(IReadOnlyList<AuditLogDto>), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetRecent(
        [FromQuery] int count = 100,
        CancellationToken cancellationToken = default)
    {
        // Enforce max limit
        if (count > 1000)
            count = 1000;

        var query = new GetRecentAuditLogsQuery(count);
        var result = await _mediator.Send(query, cancellationToken);
        return Ok(result);
    }
}
