<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('employee_transfers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained()->onDelete('cascade');
            $table->foreignId('from_company_id')->constrained('companies')->onDelete('restrict');
            $table->foreignId('to_company_id')->constrained('companies')->onDelete('restrict');
            $table->foreignId('from_department_id')->nullable()->constrained('departments')->onDelete('set null');
            $table->foreignId('to_department_id')->nullable()->constrained('departments')->onDelete('set null');
            $table->string('from_position');
            $table->string('to_position');
            $table->decimal('from_salary', 15, 2)->nullable();
            $table->decimal('to_salary', 15, 2)->nullable();
            $table->date('transfer_date');
            $table->date('effective_date');
            $table->enum('transfer_type', ['permanent', 'temporary', 'secondment'])->default('permanent');
            $table->date('return_date')->nullable();
            $table->text('reason')->nullable();
            $table->enum('status', ['pending', 'approved', 'rejected', 'completed', 'cancelled'])->default('pending');
            $table->foreignId('requested_by')->constrained('users');
            $table->foreignId('approved_by')->nullable()->constrained('users');
            $table->timestamp('approved_at')->nullable();
            $table->text('approval_notes')->nullable();
            $table->timestamps();
            
            $table->index(['employee_id', 'status']);
            $table->index('transfer_date');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('employee_transfers');
    }
};
