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

namespace JSMR.Infrastructure.Data.Configuration;

public sealed class VoiceWorkConfiguration : IEntityTypeConfiguration<VoiceWork>
{
    public void Configure(EntityTypeBuilder<VoiceWork> builder)
    {
        builder.ToTable("VoiceWorks");
        builder.HasKey(x => x.VoiceWorkId);

        builder.Property(x => x.ProductId)
            .IsRequired()
            .HasMaxLength(16);

        builder.Property(x => x.OriginalProductId)
            .HasMaxLength(16);

        builder.Property(x => x.ProductName)
            .IsRequired()
            .HasMaxLength(256);

        builder.Property(x => x.Description)
            .HasMaxLength(512);

        // Relationships
        builder.HasOne(x => x.Circle)
            .WithMany(c => c.VoiceWorks)
            .HasForeignKey(x => x.CircleId)
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasOne(x => x.Series)
            .WithMany(s => s.VoiceWorks)
            .HasForeignKey(x => x.SeriesId)
            .OnDelete(DeleteBehavior.SetNull);

        // Indexes for common filters/sorts
        builder.HasIndex(x => x.ProductId).IsUnique();
        builder.HasIndex(x => x.CircleId);
        builder.HasIndex(x => x.SalesDate);
        builder.HasIndex(x => x.ExpectedDate);
        builder.HasIndex(x => x.Favorite);
        builder.HasIndex(x => x.IsValid);
        builder.HasIndex(x => x.SubtitleLanguage);
        builder.HasIndex(x => x.AIGeneration);
        builder.HasIndex(x => x.Downloads);
        builder.HasIndex(x => x.WishlistCount);
    }
}
