using Baya.Domain.Entities.Reviews;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Baya.Infrastructure.Persistence.Configuration.ReviewsConfig;

/// <summary>
/// <c>review_tag_links</c> — the N:N join between a review and a master tag. The
/// <c>UNIQUE(review_id, review_tag_master_id)</c> forbids the same tag twice on one review; its leading column
/// is <c>review_id</c>, so it also serves "load a review's tags".
/// </summary>
internal sealed class ReviewTagLinkConfig : IEntityTypeConfiguration<ReviewTagLink>
{
    public void Configure(EntityTypeBuilder<ReviewTagLink> builder)
    {
        builder.ToTable("ReviewTagLinks", "reviews");

        builder.HasIndex(l => new { l.ReviewId, l.ReviewTagMasterId })
            .IsUnique()
            .HasDatabaseName("UX_ReviewTagLinks_Review_Tag");

        builder.HasOne(l => l.Review).WithMany(r => r.TagLinks).HasForeignKey(l => l.ReviewId).IsRequired();
        builder.HasOne(l => l.Tag).WithMany(t => t.Links).HasForeignKey(l => l.ReviewTagMasterId).IsRequired();
    }
}
