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

namespace Indotalent.Infrastructure.Database.MsSQL.Configuration;

public class CampaignConfiguration : IEntityTypeConfiguration<Campaign>
{
    public void Configure(EntityTypeBuilder<Campaign> 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.SalesTeamId)
            .HasMaxLength(GlobalConsts.StringLengthId);

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

        builder.HasMany(e => e.BudgetList)
            .WithOne(e => e.Campaign)
            .HasForeignKey(e => e.CampaignId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasMany(e => e.ExpenseList)
            .WithOne(e => e.Campaign)
            .HasForeignKey(e => e.CampaignId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasMany(e => e.LeadList)
            .WithOne(e => e.Campaign)
            .HasForeignKey(e => e.CampaignId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasIndex(e => new { e.TenantId, e.AutoNumber }).IsUnique();
        builder.HasIndex(e => e.SalesTeamId);
        builder.HasIndex(e => e.CampaignDateStart);
        builder.HasIndex(e => e.CampaignDateFinish);
    }
}