<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('video_slides', function (Blueprint $table) {
            // NULL = song-wide (legacy / shared). Non-null = owned by that audio track.
            // On track delete, slides become song-wide rather than disappear.
            $table->foreignId('audio_track_id')
                ->nullable()
                ->after('video_id')
                ->constrained('video_audio_tracks')
                ->nullOnDelete();

            $table->index(['video_id', 'audio_track_id']);
        });
    }

    public function down(): void
    {
        Schema::table('video_slides', function (Blueprint $table) {
            $table->dropIndex(['video_id', 'audio_track_id']);
            $table->dropConstrainedForeignId('audio_track_id');
        });
    }
};
