<?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
    {
        // Quitar la FK singular antes de modificar la tabla
        Schema::table('promociones', function (Blueprint $table) {
            $table->dropForeign(['plantel_id']);
            $table->dropColumn('plantel_id');
        });

        Schema::create('plantel_promocion', function (Blueprint $table) {
            $table->unsignedBigInteger('plantel_id');
            $table->foreign('plantel_id')->references('id')->on('plantels')->cascadeOnDelete();
            $table->unsignedBigInteger('promocion_id');
            $table->foreign('promocion_id')->references('id')->on('promociones')->cascadeOnDelete();
            $table->primary(['plantel_id', 'promocion_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('plantel_promocion');

        Schema::table('promociones', function (Blueprint $table) {
            $table->foreignId('plantel_id')->constrained('plantels')->cascadeOnDelete();
        });
    }
};
