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

namespace Indotalent.Features.Pipeline.Lead.Cqrs;

public class UpdateLeadRequest
{
    public string? Id { 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 class UpdateLeadResponse
{
    public string? Id { get; set; }
    public bool Success { get; set; }
}

public record UpdateLeadCommand(UpdateLeadRequest Data) : IRequest<UpdateLeadResponse>;

public class UpdateLeadHandler : IRequestHandler<UpdateLeadCommand, UpdateLeadResponse>
{
    private readonly AppDbContext _context;

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

    public async Task<UpdateLeadResponse> Handle(UpdateLeadCommand request, CancellationToken cancellationToken)
    {
        var entity = await _context.Lead
            .FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);

        if (entity == null) return new UpdateLeadResponse { Id = request.Data.Id, Success = false };

        entity.Title = request.Data.Title;
        entity.Description = request.Data.Description;
        entity.CompanyName = request.Data.CompanyName;
        entity.CompanyDescription = request.Data.CompanyDescription;
        entity.CompanyAddressStreet = request.Data.CompanyAddressStreet;
        entity.CompanyAddressCity = request.Data.CompanyAddressCity;
        entity.CompanyAddressState = request.Data.CompanyAddressState;
        entity.CompanyAddressZipCode = request.Data.CompanyAddressZipCode;
        entity.CompanyAddressCountry = request.Data.CompanyAddressCountry;
        entity.CompanyPhoneNumber = request.Data.CompanyPhoneNumber;
        entity.CompanyFaxNumber = request.Data.CompanyFaxNumber;
        entity.CompanyEmail = request.Data.CompanyEmail;
        entity.CompanyWebsite = request.Data.CompanyWebsite;
        entity.CompanyWhatsApp = request.Data.CompanyWhatsApp;
        entity.CompanyLinkedIn = request.Data.CompanyLinkedIn;
        entity.CompanyFacebook = request.Data.CompanyFacebook;
        entity.CompanyInstagram = request.Data.CompanyInstagram;
        entity.CompanyTwitter = request.Data.CompanyTwitter;
        entity.DateProspecting = request.Data.DateProspecting;
        entity.DateClosingEstimation = request.Data.DateClosingEstimation;
        entity.DateClosingActual = request.Data.DateClosingActual;
        entity.AmountTargeted = request.Data.AmountTargeted;
        entity.AmountClosed = request.Data.AmountClosed;
        entity.BudgetScore = request.Data.BudgetScore;
        entity.AuthorityScore = request.Data.AuthorityScore;
        entity.NeedScore = request.Data.NeedScore;
        entity.TimelineScore = request.Data.TimelineScore;
        entity.PipelineStage = request.Data.PipelineStage;
        entity.ClosingStatus = request.Data.ClosingStatus;
        entity.ClosingNote = request.Data.ClosingNote;
        entity.CampaignId = request.Data.CampaignId;
        entity.SalesTeamId = request.Data.SalesTeamId;

        await _context.SaveChangesAsync(cancellationToken);

        return new UpdateLeadResponse { Id = entity.Id, Success = true };
    }
}