using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Infrastructure.Database;
using MediatR;
using Indotalent.Data.Enums;
using Indotalent.Data.Entities;
using Microsoft.EntityFrameworkCore;

namespace Indotalent.Features.Pipeline.Lead.Cqrs;

public class CreateLeadRequest
{
    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; } = DateTime.Today;
    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; } = PipelineStage.Prospecting;
    public ClosingStatus ClosingStatus { get; set; } = ClosingStatus.ClosedWon;
    public string? ClosingNote { get; set; }
    public string? CampaignId { get; set; }
    public string? SalesTeamId { get; set; }
}

public class CreateLeadResponse
{
    public string? Id { get; set; }
    public string? Code { get; set; }
}

public record CreateLeadCommand(CreateLeadRequest Data) : IRequest<CreateLeadResponse>;

public class CreateLeadHandler : IRequestHandler<CreateLeadCommand, CreateLeadResponse>
{
    private readonly AppDbContext _context;

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

    public async Task<CreateLeadResponse> Handle(CreateLeadCommand request, CancellationToken cancellationToken)
    {
        var entityName = nameof(Data.Entities.Lead);

        var autoNo = await _context.GenerateAutoNumberAsync(
            entityName: entityName,
            prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
            ct: cancellationToken
        );

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

        _context.Lead.Add(entity);
        await _context.SaveChangesAsync(cancellationToken);

        return new CreateLeadResponse
        {
            Id = entity.Id,
            Code = entity.AutoNumber
        };
    }
}