// <copyright file="MediaStreamInfoConfiguration.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

namespace Jellyfin.Database.Implementations.ModelConfiguration;

using System;
using Jellyfin.Database.Implementations.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

/// <summary>
/// People configuration.
/// </summary>
public class MediaStreamInfoConfiguration : IEntityTypeConfiguration<MediaStreamInfo>
{
    /// <inheritdoc/>
    public void Configure(EntityTypeBuilder<MediaStreamInfo> builder)
    {
        ArgumentNullException.ThrowIfNull(builder);
        builder.HasKey(e => new { e.ItemId, e.StreamIndex });
        builder.HasIndex(e => e.StreamIndex);
        builder.HasIndex(e => e.StreamType);
        builder.HasIndex(e => new { e.StreamIndex, e.StreamType });
        builder.HasIndex(e => new { e.StreamIndex, e.StreamType, e.Language });

        builder.HasOne(e => e.AudioDetails)
            .WithOne(a => a.Stream)
            .HasForeignKey<AudioStreamDetails>(a => new { a.ItemId, a.StreamIndex })
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasOne(e => e.VideoDetails)
            .WithOne(v => v.Stream)
            .HasForeignKey<VideoStreamDetails>(v => new { v.ItemId, v.StreamIndex })
            .OnDelete(DeleteBehavior.Cascade);
    }
}
