using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using TeleWave.Domain.Library;

namespace TeleWave.Infrastructure.Persistence.Configurations;

public class ShowConfiguration : IEntityTypeConfiguration<Show>
{
    public void Configure(EntityTypeBuilder<Show> builder)
    {
        builder.Property(x => x.Name).IsRequired().HasMaxLength(256);
        builder.Property(x => x.Description).HasMaxLength(2048);
        builder.Property(x => x.MetadataProvider).HasMaxLength(16);
        builder.Property(x => x.MetadataExternalId).HasMaxLength(64);
        builder.Property(x => x.OriginalName).HasMaxLength(256);
        builder.Property(x => x.FranchiseExternalId).HasMaxLength(64);
        builder.Property(x => x.FranchiseName).HasMaxLength(256);

        builder
            .HasMany(x => x.Episodes)
            .WithOne()
            .HasForeignKey(e => e.ShowId)
            .OnDelete(DeleteBehavior.Cascade);
        builder.Navigation(x => x.Episodes).UsePropertyAccessMode(PropertyAccessMode.Field);

        builder
            .HasMany(x => x.Genres)
            .WithOne()
            .HasForeignKey(g => g.ShowId)
            .OnDelete(DeleteBehavior.Cascade);
        builder.Navigation(x => x.Genres).UsePropertyAccessMode(PropertyAccessMode.Field);
    }
}

public class ShowEpisodeConfiguration : IEntityTypeConfiguration<ShowEpisode>
{
    public void Configure(EntityTypeBuilder<ShowEpisode> builder)
    {
        builder.HasIndex(x => new { x.ShowId, x.Position });
        builder.HasIndex(x => x.MediaAssetId);
        builder.Property(x => x.Title).HasMaxLength(512);
        builder.Property(x => x.Overview).HasMaxLength(4096);
    }
}
