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

namespace Extrudex.Infrastructure.Data.Configurations;

public class MaterialFinishConfiguration : BaseEntityConfiguration<MaterialFinish>
{
    public override void Configure(EntityTypeBuilder<MaterialFinish> 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 finish name is unique per base material
        builder.HasIndex(e => new { e.MaterialBaseId, e.Name })
            .IsUnique()
            .HasDatabaseName("ix_material_finishes_material_base_id_name");

        // Relationship configured from MaterialBase side; navigation-only here
        builder.HasOne(e => e.MaterialBase)
            .WithMany(e => e.Finishes)
            .HasForeignKey(e => e.MaterialBaseId)
            .HasConstraintName("fk_material_finishes_material_base")
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasMany(e => e.Spools)
            .WithOne(e => e.MaterialFinish)
            .HasForeignKey(e => e.MaterialFinishId)
            .HasConstraintName("fk_spools_material_finish");
    }
}