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 PremiseMediaMap : IEntityMappingConfiguration<PremiseMedia>
    {
        public void Map(EntityTypeBuilder<PremiseMedia> builder)
        {
            builder.ToTable("PremiseMedias");

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

            // Properties
            builder.Property(x => x.PremiseId).IsRequired();
            builder.Property(x => x.MediaId).IsRequired();
            builder.Property(x => x.MediaType).IsRequired();
            builder.Property(x => x.DisplayOrder).IsRequired();

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

            builder.HasOne(x => x.Media)
                .WithMany()
                .HasForeignKey(x => x.MediaId)
                .OnDelete(DeleteBehavior.Restrict);

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

            builder.HasIndex(x => x.MediaId)
                .HasDatabaseName("IX_PremiseMedias_MediaId");
        }
    }
}
