using Data.Data.Configurations;
using DeepDrftModels.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace DeepDrftData.Data.Configurations;

public class MixMetadataConfiguration : BaseEntityConfiguration<MixMetadata>
{
    public override void Configure(EntityTypeBuilder<MixMetadata> builder)
    {
        // Wires up Id PK + audit columns (CreatedAt, UpdatedAt, IsDeleted) and the IsDeleted index.
        base.Configure(builder);

        builder.ToTable("mix_metadata");

        // Map the base audit columns to the snake_case naming the rest of the schema uses.
        builder.Property(e => e.Id).HasColumnName("id");
        builder.Property(e => e.CreatedAt).HasColumnName("created_at");
        builder.Property(e => e.UpdatedAt).HasColumnName("updated_at");
        builder.Property(e => e.IsDeleted).HasColumnName("is_deleted");

        builder.Property(e => e.ReleaseId)
            .HasColumnName("release_id");

        builder.Property(e => e.WaveformEntryKey)
            .IsRequired()
            .HasMaxLength(500)   // Consistent with ImagePath on ReleaseEntity; entry keys can carry GUIDs.
            .HasColumnName("waveform_entry_key");

        // 1:1 to the parent release. The unique FK index is the DB-level enforcement of the
        // one-satellite-per-release cardinality. Cascade on delete: removing the release removes its
        // medium satellite (unlike Track's SetNull — a satellite has no meaning without its release).
        builder.HasOne(e => e.Release)
            .WithOne(r => r.MixMetadata)
            .HasForeignKey<MixMetadata>(e => e.ReleaseId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasIndex(e => e.ReleaseId)
            .IsUnique()
            .HasDatabaseName("IX_mix_metadata_release_id");

        // Names the is_deleted index explicitly. BaseEntityConfiguration.Configure already
        // calls HasIndex(e => e.IsDeleted); this adds HasDatabaseName so EF always uses
        // "IX_mix_metadata_is_deleted" regardless of auto-naming conventions.
        builder.HasIndex(e => e.IsDeleted).HasDatabaseName("IX_mix_metadata_is_deleted");
    }
}
