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

namespace Lada.Framework.Data.Mappings.EntityMappings.Trucks;

/// <summary>
/// Mapping configuration cho TruckMedia
/// </summary>
public class TruckMediaMap : IEntityMappingConfiguration<TruckMedia>
{
    public void Map(EntityTypeBuilder<TruckMedia> builder)
    {
        builder.ToTable("TruckMedias");

        // Primary Key
        builder.HasKey(x => x.Id);

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

        // Relationships
        builder.HasOne(x => x.Truck)
            .WithMany(x => x.TruckMedias)
            .HasForeignKey(x => x.TruckId)
            .OnDelete(DeleteBehavior.Cascade);

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

        // Indexes
        builder.HasIndex(x => x.TruckId)
            .HasDatabaseName("IX_TruckMedias_TruckId");

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