using Lada.Framework.Data.Domains.Crm;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Lada.Framework.Data.Mappings.EntityMappings.Crm
{
    public class VatInfoMap : IEntityMappingConfiguration<VatInfo>
    {
        public void Map(EntityTypeBuilder<VatInfo> builder)
        {
            builder.ToTable("VatInvoice");

            builder.HasKey(x => x.Id);

            builder.Property(x => x.CustomerId)
                .IsRequired();

            builder.Property(x => x.CompanyName)
                .IsRequired()
                .HasMaxLength(200);

            builder.Property(x => x.TaxCode)
                .HasMaxLength(20);

            builder.Property(x => x.InvoiceEmail)
                .IsRequired()
                .HasMaxLength(100);

            builder.Property(x => x.Address)
                .IsRequired()
                .HasMaxLength(500);

            builder.Property(x => x.ContactName)
                .IsRequired()
                .HasMaxLength(100);

            builder.Property(x => x.ContactPhone)
                .IsRequired()
                .HasMaxLength(20);

            builder.Property(x => x.Note)
                .HasMaxLength(1000);


            // Relationships
            builder.HasOne(x => x.Customer)
                .WithMany(c => c.VatInfos)
                .HasForeignKey(x => x.CustomerId)
                .OnDelete(DeleteBehavior.Cascade);

            // Indexes
            builder.HasIndex(x => x.CustomerId);
            builder.HasIndex(x => x.TaxCode);
            builder.HasIndex(x => x.InvoiceEmail);
            builder.HasIndex(x => new { x.CustomerId, x.IsDefault }).IsUnique();
        }
    }
}