<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * Creates the pivot table for many-to-many relationship between
     * election_cycles and voting_procedures.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('election_cycle_voting_procedure', function (Blueprint $table) {
            // Foreign key to election cycles
            $table->foreignId('election_cycle_id')
                ->constrained('election_cycles')
                ->onDelete('cascade');

            // Foreign key to voting procedures
            $table->foreignId('voting_procedure_id')
                ->constrained('voting_procedures')
                ->onDelete('cascade');

            // Composite primary key
            $table->primary(['election_cycle_id', 'voting_procedure_id']);

            // Timestamps for tracking when relationships were created
            $table->timestamps();

            // Indexes (primary key already creates indexes)
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down(): void
    {
        Schema::dropIfExists('election_cycle_voting_procedure');
    }
};
