using ColaFlow.Modules.Identity.Domain.Aggregates.Tenants;
using ColaFlow.Modules.Identity.Domain.Aggregates.Users;
using ColaFlow.Modules.Identity.Domain.Repositories;
using MediatR;
using Microsoft.Extensions.Logging;

namespace ColaFlow.Modules.Identity.Application.Queries.GetPendingInvitations;

public class GetPendingInvitationsQueryHandler(
    IInvitationRepository invitationRepository,
    IUserRepository userRepository,
    ILogger<GetPendingInvitationsQueryHandler> logger)
    : IRequestHandler<GetPendingInvitationsQuery, List<InvitationDto>>
{
    public async Task<List<InvitationDto>> Handle(GetPendingInvitationsQuery request, CancellationToken cancellationToken)
    {
        var tenantId = TenantId.Create(request.TenantId);

        // Get all pending invitations for the tenant
        var invitations = await invitationRepository.GetPendingByTenantAsync(tenantId, cancellationToken);

        // Get all unique inviter user IDs
        var inviterIds = invitations.Select(i => (Guid)i.InvitedBy).Distinct().ToList();

        // Fetch all inviters in one query
        var inviters = await userRepository.GetByIdsAsync(inviterIds, cancellationToken);
        var inviterDict = inviters.ToDictionary(u => u.Id, u => u.FullName.Value);

        // Map to DTOs
        var dtos = invitations.Select(i => new InvitationDto(
            Id: i.Id,
            Email: i.Email,
            Role: i.Role.ToString(),
            InvitedByName: inviterDict.TryGetValue(i.InvitedBy, out var name) ? name : "Unknown",
            InvitedById: i.InvitedBy,
            CreatedAt: i.CreatedAt,
            ExpiresAt: i.ExpiresAt
        )).ToList();

        logger.LogInformation(
            "Retrieved {Count} pending invitations for tenant {TenantId}",
            dtos.Count,
            request.TenantId);

        return dtos;
    }
}
