<?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 leadership_histories table to store historical records
     * of past leaders and their terms (automatically created when assignments end).
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('leadership_histories', function (Blueprint $table) {
            // Primary key
            $table->id();

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

            // Foreign key to leadership positions
            $table->foreignId('leadership_position_id')
                ->constrained('leadership_positions')
                ->onDelete('cascade');

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

            // Term dates
            $table->date('term_start_date');
            $table->date('term_end_date');

            // Snapshot of position title at time of service
            $table->string('position_title', 191);

            // Additional notes
            $table->text('notes')->nullable();

            // Timestamps (no soft deletes - history is permanent)
            $table->timestamps();

            // Indexes for performance
            $table->index('organization_id');
            $table->index('leadership_position_id');
            $table->index('user_id');
            $table->index('term_start_date');
            $table->index('term_end_date');
        });
    }

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