using Lada.Framework.Data.Domains.Premises;
using Lada.Framework.Data.Mappings;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Lada.Framework.Data.Mappings.EntityMappings.Premises
{
    public class PremiseContactMap : IEntityMappingConfiguration<PremiseContact>
    {
        public void Map(EntityTypeBuilder<PremiseContact> builder)
        {
            builder.ToTable("PremiseContacts");

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

            // Properties
            builder.Property(x => x.PremiseId).IsRequired();
            builder.Property(x => x.FullName).HasMaxLength(200).IsRequired();
            builder.Property(x => x.PhoneNumber).HasMaxLength(20).IsRequired();
            builder.Property(x => x.Role).IsRequired();
            builder.Property(x => x.Note).HasMaxLength(500);

            // Relationships
            builder.HasOne(x => x.Premise)
                .WithMany(x => x.Contacts)
                .HasForeignKey(x => x.PremiseId)
                .OnDelete(DeleteBehavior.Cascade);

            // Indexes
            builder.HasIndex(x => x.PremiseId)
                .HasDatabaseName("IX_PremiseContacts_PremiseId");
        }
    }
}
