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

namespace Indotalent.Features.Pipeline.LeadActivity.Cqrs;

public class GetLeadActivityListResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public string? Summary { get; set; }
    public DateTime? FromDate { get; set; }
    public Data.Enums.LeadActivityType Type { get; set; }
}

public record GetLeadActivityListQuery() : IRequest<List<GetLeadActivityListResponse>>;

public class GetLeadActivityListHandler : IRequestHandler<GetLeadActivityListQuery, List<GetLeadActivityListResponse>>
{
    private readonly AppDbContext _context;

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

    public async Task<List<GetLeadActivityListResponse>> Handle(GetLeadActivityListQuery request, CancellationToken cancellationToken)
    {
        return await _context.LeadActivity
            .AsNoTracking()
            .OrderByDescending(x => x.FromDate)
            .Select(x => new GetLeadActivityListResponse
            {
                Id = x.Id,
                AutoNumber = x.AutoNumber,
                Summary = x.Summary,
                FromDate = x.FromDate,
                Type = x.Type
            })
            .ToListAsync(cancellationToken);
    }
}