<?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
    {
        foreach (['permanent_employees', 'temporary_employees'] as $tableName) {
            if (! Schema::hasTable($tableName) || Schema::hasColumn($tableName, 'designation_id')) {
                continue;
            }

            Schema::table($tableName, function (Blueprint $table): void {
                $table->foreignId('designation_id')
                    ->nullable()
                    ->after('id')
                    ->constrained('designations')
                    ->nullOnDelete();
            });
        }
    }

    public function down(): void
    {
        foreach (['permanent_employees', 'temporary_employees'] as $tableName) {
            if (! Schema::hasTable($tableName) || ! Schema::hasColumn($tableName, 'designation_id')) {
                continue;
            }

            Schema::table($tableName, function (Blueprint $table): void {
                $table->dropConstrainedForeignId('designation_id');
            });
        }
    }
};
