<?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_positions table to define the organizational
     * leadership hierarchy with positions like BOD, Chairman, CEO, C-Suite, etc.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('leadership_positions', function (Blueprint $table) {
            // Primary key
            $table->id();

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

            // Position details
            $table->string('title', 191);

            // Hierarchy level: Board of Directors, Chairman, CEO, C-Suite, etc.
            $table->enum('hierarchy_level', [
                'bod',
                'chairman',
                'ceo',
                'c_suite',
                'middle_management',
                'first_line_management',
                'frontline'
            ]);

            // Self-referencing foreign key for reporting structure
            $table->unsignedBigInteger('parent_position_id')->nullable();

            // Position description
            $table->text('description')->nullable();

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

            // Foreign key constraint for parent position
            $table->foreign('parent_position_id')
                ->references('id')
                ->on('leadership_positions')
                ->onDelete('set null');

            // Indexes for performance
            $table->index('organization_id');
            $table->index('hierarchy_level');
            $table->index('parent_position_id');
        });
    }

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