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

namespace Indotalent.Features.Multitenant.Tenant.Cqrs;

public class GetTenantListResponse
{
    public string? Id { get; set; }
    public string? Name { get; set; }
    public string? PhoneNumber { get; set; }
    public string? EmailAddress { get; set; }
    public string? City { get; set; }
    public bool IsActive { get; set; }
}

public record GetTenantListQuery() : IRequest<List<GetTenantListResponse>>;

public class GetTenantListHandler : IRequestHandler<GetTenantListQuery, List<GetTenantListResponse>>
{
    private readonly AppDbContext _context;

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

    public async Task<List<GetTenantListResponse>> Handle(GetTenantListQuery request, CancellationToken cancellationToken)
    {
        return await _context.Tenant
            .AsNoTracking()
            .OrderBy(x => x.Name)
            .Select(x => new GetTenantListResponse
            {
                Id = x.Id,
                Name = x.Name,
                PhoneNumber = x.PhoneNumber,
                EmailAddress = x.EmailAddress,
                City = x.City,
                IsActive = x.IsActive
            })
            .ToListAsync(cancellationToken);
    }
}