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

namespace Indotalent.Features.Setting.Company.Cqrs;

public class GetCompanyListResponse
{
    public string? Id { get; set; }
    public string? Name { get; set; }
    public string? Email { get; set; }
    public string? Phone { get; set; }
    public string? City { get; set; }
    public bool IsDefault { get; set; }
}

public record GetCompanyListQuery() : IRequest<List<GetCompanyListResponse>>;

public class GetCompanyListHandler : IRequestHandler<GetCompanyListQuery, List<GetCompanyListResponse>>
{
    private readonly AppDbContext _context;

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

    public async Task<List<GetCompanyListResponse>> Handle(GetCompanyListQuery request, CancellationToken cancellationToken)
    {
        return await _context.Company
            .AsNoTracking()
            .OrderBy(x => x.Name)
            .Select(x => new GetCompanyListResponse
            {
                Id = x.Id,
                Name = x.Name,
                Email = x.Email,
                Phone = x.Phone,
                City = x.City,
                IsDefault = x.IsDefault
            })
            .ToListAsync(cancellationToken);
    }
}