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

namespace JSMR.Infrastructure.Data.Configuration;

public sealed class VoiceWorkTagConfiguration : IEntityTypeConfiguration<VoiceWorkTag>
{
    public void Configure(EntityTypeBuilder<VoiceWorkTag> builder)
    {
        builder.ToTable("VoiceWorkTags");
        builder.HasKey(x => new { x.VoiceWorkId, x.TagId });

        builder.HasOne(x => x.VoiceWork)
            .WithMany(v => v.Tags)
            .HasForeignKey(x => x.VoiceWorkId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasOne(x => x.Tag)
            .WithMany()
            .HasForeignKey(x => x.TagId)
            .OnDelete(DeleteBehavior.Cascade);

        // Helpful indexes
        builder.HasIndex(x => x.TagId);
        builder.HasIndex(x => new { x.VoiceWorkId, x.Position });
        builder.HasIndex(x => x.IsValid);
    }
}