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

namespace Indotalent.Features.Pipeline.LeadActivity.Cqrs;

public class GetLeadActivityByIdResponse
{
    public string? Id { get; set; }
    public string? LeadId { get; set; }
    public string? AutoNumber { get; set; }
    public string? Summary { get; set; }
    public string? Description { get; set; }
    public DateTime? FromDate { get; set; }
    public DateTime? ToDate { get; set; }
    public Data.Enums.LeadActivityType Type { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public record GetLeadActivityByIdQuery(string Id) : IRequest<GetLeadActivityByIdResponse?>;

public class GetLeadActivityByIdHandler : IRequestHandler<GetLeadActivityByIdQuery, GetLeadActivityByIdResponse?>
{
    private readonly AppDbContext _context;

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

    public async Task<GetLeadActivityByIdResponse?> Handle(GetLeadActivityByIdQuery request, CancellationToken cancellationToken)
    {
        return await _context.LeadActivity
            .AsNoTracking()
            .Where(x => x.Id == request.Id)
            .Select(x => new GetLeadActivityByIdResponse
            {
                Id = x.Id,
                LeadId = x.LeadId,
                AutoNumber = x.AutoNumber,
                Summary = x.Summary,
                Description = x.Description,
                FromDate = x.FromDate,
                ToDate = x.ToDate,
                Type = x.Type,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy
            })
            .FirstOrDefaultAsync(cancellationToken);
    }
}