using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Indotalent.Features.Multitenant.TenantUser.Cqrs;

public class GetTenantUserByIdResponse
{
    public string? Id { get; set; }
    public string? TenantId { get; set; }
    public string? UserId { get; set; }
    public string? Summary { get; set; }
    public bool IsActive { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public record GetTenantUserByIdQuery(string Id) : IRequest<GetTenantUserByIdResponse?>;

public class GetTenantUserByIdHandler : IRequestHandler<GetTenantUserByIdQuery, GetTenantUserByIdResponse?>
{
    private readonly AppDbContext _context;

    public GetTenantUserByIdHandler(AppDbContext context) => _context = context;

    public async Task<GetTenantUserByIdResponse?> Handle(GetTenantUserByIdQuery request, CancellationToken cancellationToken)
    {
        return await _context.TenantUser
            .AsNoTracking()
            .Where(x => x.Id == request.Id)
            .Select(x => new GetTenantUserByIdResponse
            {
                Id = x.Id,
                TenantId = x.TenantId,
                UserId = x.UserId,
                Summary = x.Summary,
                IsActive = x.IsActive,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy,
            })
            .FirstOrDefaultAsync(cancellationToken);
    }
}