using ColaFlow.Modules.ProjectManagement.Domain.Repositories;
using MediatR;

namespace ColaFlow.Modules.ProjectManagement.Application.Queries.AuditLogs.GetAuditLogById;

/// <summary>
/// Handler for GetAuditLogByIdQuery
/// Retrieves a single audit log entry by its unique identifier
/// </summary>
public class GetAuditLogByIdQueryHandler : IRequestHandler<GetAuditLogByIdQuery, AuditLogDto?>
{
    private readonly IAuditLogRepository _auditLogRepository;

    public GetAuditLogByIdQueryHandler(IAuditLogRepository auditLogRepository)
    {
        _auditLogRepository = auditLogRepository;
    }

    public async Task<AuditLogDto?> Handle(GetAuditLogByIdQuery request, CancellationToken cancellationToken)
    {
        var auditLog = await _auditLogRepository.GetByIdAsync(request.AuditLogId, cancellationToken);

        if (auditLog == null)
            return null;

        return new AuditLogDto(
            auditLog.Id,
            auditLog.EntityType,
            auditLog.EntityId,
            auditLog.Action,
            auditLog.UserId?.Value,
            auditLog.Timestamp,
            auditLog.OldValues,
            auditLog.NewValues
        );
    }
}
