<?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::table('employees', function (Blueprint $table) {
            $table->string('blood_type', 8)->nullable()->after('marital_status');
            $table->string('religion', 80)->nullable()->after('blood_type');
            $table->string('tax_number', 80)->nullable()->after('social_insurance_number');
            $table->string('swift_code', 32)->nullable()->after('iban');
            $table->foreignId('branch_id')->nullable()->after('department_id')->constrained('hr_branches')->nullOnDelete();
            $table->foreignId('section_id')->nullable()->after('branch_id')->constrained('hr_sections')->nullOnDelete();
            $table->foreignId('team_id')->nullable()->after('section_id')->constrained('hr_teams')->nullOnDelete();
            $table->foreignId('job_grade_id')->nullable()->after('position_id')->constrained('hr_job_grades')->nullOnDelete();
            $table->foreignId('work_location_id')->nullable()->after('job_grade_id')->constrained('hr_work_locations')->nullOnDelete();
            $table->foreignId('cost_center_id')->nullable()->after('work_location_id')->constrained('hr_cost_centers')->nullOnDelete();
        });

        Schema::table('employee_addresses', function (Blueprint $table) {
            if (! Schema::hasColumn('employee_addresses', 'postal_code')) {
                $table->string('postal_code', 32)->nullable()->after('city');
            }
        });

        Schema::create('employee_employment_histories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->string('type', 40);
            $table->date('effective_date');
            $table->string('previous_value')->nullable();
            $table->string('new_value')->nullable();
            $table->string('reason')->nullable();
            $table->text('notes')->nullable();
            $table->json('meta')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->index(['tenant_id', 'employee_id', 'type']);
            $table->index(['employee_id', 'effective_date']);
        });
    }

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

        Schema::table('employee_addresses', function (Blueprint $table) {
            if (Schema::hasColumn('employee_addresses', 'postal_code')) {
                $table->dropColumn('postal_code');
            }
        });

        Schema::table('employees', function (Blueprint $table) {
            $table->dropConstrainedForeignId('cost_center_id');
            $table->dropConstrainedForeignId('work_location_id');
            $table->dropConstrainedForeignId('job_grade_id');
            $table->dropConstrainedForeignId('team_id');
            $table->dropConstrainedForeignId('section_id');
            $table->dropConstrainedForeignId('branch_id');
            $table->dropColumn(['blood_type', 'religion', 'tax_number', 'swift_code']);
        });
    }
};
