<?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('song_slides', function (Blueprint $table) {
            $table->dropUnique(['song_group_id', 'order']);
            $table->dropIndex(['song_group_id']);
            $table->dropForeign(['song_group_id']);
            $table->dropColumn('song_group_id');
        });

        Schema::table('song_slides', function (Blueprint $table) {
            $table->foreignId('label_id')->nullable()->constrained('labels')->restrictOnDelete();
        });

        Schema::dropIfExists('song_arrangement_groups');
        Schema::dropIfExists('song_groups');

        Schema::create('song_arrangement_labels', function (Blueprint $table) {
            $table->id();
            $table->foreignId('song_arrangement_id')->constrained()->cascadeOnDelete();
            $table->foreignId('label_id')->constrained('labels')->restrictOnDelete();
            $table->unsignedInteger('order');
            $table->timestamps();
            $table->index(['song_arrangement_id', 'order']);
        });
    }

    public function down(): void
    {
        throw new \RuntimeException('Destructive migration: rollback not supported. Restore from backup.');
    }
};
