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

namespace Indotalent.Features.Pipeline.LeadContact.Cqrs;

public class GetLeadContactListResponse
{
    public string? Id { get; set; }
    public string? LeadId { get; set; }
    public string? AutoNumber { get; set; }
    public string? FullName { get; set; }
    public string? MobileNumber { get; set; }
    public string? Email { get; set; }
    public string? AvatarName { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public record GetLeadContactListQuery() : IRequest<List<GetLeadContactListResponse>>;

public class GetLeadContactListHandler : IRequestHandler<GetLeadContactListQuery, List<GetLeadContactListResponse>>
{
    private readonly AppDbContext _context;

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

    public async Task<List<GetLeadContactListResponse>> Handle(GetLeadContactListQuery request, CancellationToken cancellationToken)
    {
        return await _context.LeadContact
            .AsNoTracking()
            .OrderBy(x => x.FullName)
            .Select(x => new GetLeadContactListResponse
            {
                Id = x.Id,
                LeadId = x.LeadId,
                AutoNumber = x.AutoNumber,
                FullName = x.FullName,
                MobileNumber = x.MobileNumber,
                Email = x.Email,
                AvatarName = x.AvatarName,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy
            })
            .ToListAsync(cancellationToken);
    }
}