using Indotalent.Data.Entities;
using Indotalent.Shared.Consts;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Indotalent.Infrastructure.Database.MsSQL.Configuration;

public class LeadConfiguration : IEntityTypeConfiguration<Lead>
{
    public void Configure(EntityTypeBuilder<Lead> builder)
    {
        builder.Property(e => e.AutoNumber)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.Title)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.Description)
            .HasMaxLength(GlobalConsts.StringLengthMedium);

        builder.Property(e => e.CompanyName)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CompanyDescription)
            .HasMaxLength(GlobalConsts.StringLengthMedium);

        builder.Property(e => e.CompanyAddressStreet)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CompanyAddressCity)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CompanyAddressState)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CompanyAddressZipCode)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CompanyAddressCountry)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CompanyPhoneNumber)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CompanyFaxNumber)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CompanyEmail)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CompanyWebsite)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CompanyWhatsApp)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CompanyLinkedIn)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CompanyFacebook)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CompanyInstagram)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CompanyTwitter)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.ClosingNote)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.CampaignId)
            .HasMaxLength(GlobalConsts.StringLengthId);

        builder.Property(e => e.SalesTeamId)
            .HasMaxLength(GlobalConsts.StringLengthId);

        builder.HasOne(e => e.Campaign)
            .WithMany(e => e.LeadList)
            .HasForeignKey(e => e.CampaignId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasOne(e => e.SalesTeam)
            .WithMany()
            .HasForeignKey(e => e.SalesTeamId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasMany(e => e.LeadContacts)
            .WithOne(e => e.Lead)
            .HasForeignKey(e => e.LeadId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasMany(e => e.LeadActivities)
            .WithOne(e => e.Lead)
            .HasForeignKey(e => e.LeadId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasIndex(e => e.AutoNumber).IsUnique();
        builder.HasIndex(e => e.CampaignId);
        builder.HasIndex(e => e.SalesTeamId);
        builder.HasIndex(e => e.DateProspecting);
    }
}