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

namespace CartWise.Infrastructure.Data.Configurations;

public class ProductIdentifierConfiguration : IEntityTypeConfiguration<ProductIdentifier>
{
    public void Configure(EntityTypeBuilder<ProductIdentifier> builder)
    {
        builder.HasKey(i => i.ProductIdentifierId);

        builder.Property(i => i.Value)
            .HasMaxLength(64)
            .IsRequired();

        builder.Property(i => i.NormalizedValue)
            .HasMaxLength(64)
            .IsRequired();

        builder.HasIndex(i => new { i.IdentifierType, i.NormalizedValue })
            .IsUnique();

        builder.HasOne<Product>()
            .WithMany(p => p.Identifiers)
            .HasForeignKey(i => i.ProductId)
            .OnDelete(DeleteBehavior.Cascade);
    }
}
