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

namespace Indotalent.Features.Pipeline.Lead.Cqrs;

public class GetLeadByIdResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public string? Title { get; set; }
    public string? Description { get; set; }
    public string? CompanyName { get; set; }
    public string? CompanyDescription { get; set; }
    public string? CompanyAddressStreet { get; set; }
    public string? CompanyAddressCity { get; set; }
    public string? CompanyAddressState { get; set; }
    public string? CompanyAddressZipCode { get; set; }
    public string? CompanyAddressCountry { get; set; }
    public string? CompanyPhoneNumber { get; set; }
    public string? CompanyFaxNumber { get; set; }
    public string? CompanyEmail { get; set; }
    public string? CompanyWebsite { get; set; }
    public string? CompanyWhatsApp { get; set; }
    public string? CompanyLinkedIn { get; set; }
    public string? CompanyFacebook { get; set; }
    public string? CompanyInstagram { get; set; }
    public string? CompanyTwitter { get; set; }
    public DateTime? DateProspecting { get; set; }
    public DateTime? DateClosingEstimation { get; set; }
    public DateTime? DateClosingActual { get; set; }
    public decimal? AmountTargeted { get; set; }
    public decimal? AmountClosed { get; set; }
    public decimal? BudgetScore { get; set; }
    public decimal? AuthorityScore { get; set; }
    public decimal? NeedScore { get; set; }
    public decimal? TimelineScore { get; set; }
    public PipelineStage PipelineStage { get; set; }
    public ClosingStatus ClosingStatus { get; set; }
    public string? ClosingNote { get; set; }
    public string? CampaignId { get; set; }
    public string? SalesTeamId { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public record GetLeadByIdQuery(string Id) : IRequest<GetLeadByIdResponse?>;

public class GetLeadByIdHandler : IRequestHandler<GetLeadByIdQuery, GetLeadByIdResponse?>
{
    private readonly AppDbContext _context;

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

    public async Task<GetLeadByIdResponse?> Handle(GetLeadByIdQuery request, CancellationToken cancellationToken)
    {
        return await _context.Lead
            .AsNoTracking()
            .Where(x => x.Id == request.Id)
            .Select(x => new GetLeadByIdResponse
            {
                Id = x.Id,
                AutoNumber = x.AutoNumber,
                Title = x.Title,
                Description = x.Description,
                CompanyName = x.CompanyName,
                CompanyDescription = x.CompanyDescription,
                CompanyAddressStreet = x.CompanyAddressStreet,
                CompanyAddressCity = x.CompanyAddressCity,
                CompanyAddressState = x.CompanyAddressState,
                CompanyAddressZipCode = x.CompanyAddressZipCode,
                CompanyAddressCountry = x.CompanyAddressCountry,
                CompanyPhoneNumber = x.CompanyPhoneNumber,
                CompanyFaxNumber = x.CompanyFaxNumber,
                CompanyEmail = x.CompanyEmail,
                CompanyWebsite = x.CompanyWebsite,
                CompanyWhatsApp = x.CompanyWhatsApp,
                CompanyLinkedIn = x.CompanyLinkedIn,
                CompanyFacebook = x.CompanyFacebook,
                CompanyInstagram = x.CompanyInstagram,
                CompanyTwitter = x.CompanyTwitter,
                DateProspecting = x.DateProspecting,
                DateClosingEstimation = x.DateClosingEstimation,
                DateClosingActual = x.DateClosingActual,
                AmountTargeted = x.AmountTargeted,
                AmountClosed = x.AmountClosed,
                BudgetScore = x.BudgetScore,
                AuthorityScore = x.AuthorityScore,
                NeedScore = x.NeedScore,
                TimelineScore = x.TimelineScore,
                PipelineStage = x.PipelineStage,
                ClosingStatus = x.ClosingStatus,
                ClosingNote = x.ClosingNote,
                CampaignId = x.CampaignId,
                SalesTeamId = x.SalesTeamId,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy
            })
            .FirstOrDefaultAsync(cancellationToken);
    }
}