<?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 election_cycles table to define election periods for
     * leadership positions with duration and applicable positions.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('election_cycles', function (Blueprint $table) {
            // Primary key
            $table->id();

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

            // Cycle details
            $table->string('name', 191);

            // Duration in years (supports decimal like 0.5 for 6 months)
            $table->decimal('duration_years', 8, 1);

            // Cycle dates
            $table->date('start_date');
            $table->date('end_date');

            // Cycle status
            $table->enum('status', ['active', 'completed', 'cancelled'])
                ->default('active');

            // JSON array of applicable leadership position IDs
            $table->json('applicable_positions');

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

            // Indexes for performance
            $table->index('organization_id');
            $table->index('status');
            $table->index('start_date');
            $table->index('end_date');
        });
    }

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