using Extrudex.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Extrudex.Infrastructure.Data.Configurations;

public class MaterialModifierConfiguration : BaseEntityConfiguration<MaterialModifier>
{
    public override void Configure(EntityTypeBuilder<MaterialModifier> builder)
    {
        base.Configure(builder);

        builder.Property(e => e.Name)
            .HasColumnName("name")
            .IsRequired()
            .HasMaxLength(100);

        builder.Property(e => e.MaterialBaseId)
            .HasColumnName("material_base_id")
            .IsRequired();

        // Unique index on (material_base_id, name) — each modifier name is unique per base material
        builder.HasIndex(e => new { e.MaterialBaseId, e.Name })
            .IsUnique()
            .HasDatabaseName("ix_material_modifiers_material_base_id_name");

        builder.HasOne(e => e.MaterialBase)
            .WithMany(e => e.Modifiers)
            .HasForeignKey(e => e.MaterialBaseId)
            .HasConstraintName("fk_material_modifiers_material_base")
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasMany(e => e.Spools)
            .WithOne(e => e.MaterialModifier!)
            .HasForeignKey(e => e.MaterialModifierId)
            .HasConstraintName("fk_spools_material_modifier");
    }
}