<?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 voting_procedures table to document voting methods
     * and rules for leadership elections with version tracking.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('voting_procedures', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Foreign key to organizations
            $table->foreignId('organization_id')
                ->constrained('organizations')
                ->onDelete('cascade');

            // Procedure details
            $table->string('name', 191);
            $table->text('description');
            $table->string('voting_method', 191);
            $table->text('rules');

            // Version tracking for audit history
            $table->integer('version')->default(1);

            // Timestamps and soft deletes
            $table->timestamps();
            $table->softDeletes();

            // Indexes for performance
            $table->index('organization_id');
            $table->index('name');
            $table->index('version');
        });
    }

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