<?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 staff_histories table to track employment records
     * for staff members including current and former employees.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('staff_histories', function (Blueprint $table) {
            // Primary key
            $table->id();

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

            // Foreign key to users (nullable - may not have system account)
            $table->foreignId('user_id')
                ->nullable()
                ->constrained('users')
                ->onDelete('set null');

            // Staff details
            $table->string('staff_name', 191);
            $table->string('position', 191);

            // Employment dates
            $table->date('employment_start_date');
            $table->date('employment_end_date')->nullable();

            // Employment status
            $table->enum('employment_status', ['current', 'former']);

            // Reason for leaving (for former employees)
            $table->text('termination_reason')->nullable();

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

            // Indexes for performance
            $table->index('organization_id');
            $table->index('user_id');
            $table->index('employment_status');
            $table->index('employment_start_date');
            $table->index('staff_name');
        });
    }

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