<?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('resignations', function (Blueprint $table) {
            if (! Schema::hasColumn('resignations', 'offboarding_id')) {
                $table->unsignedBigInteger('offboarding_id')->nullable()->after('user_id')->index();
            }
        });

        Schema::table('terminations', function (Blueprint $table) {
            if (! Schema::hasColumn('terminations', 'offboarding_id')) {
                $table->unsignedBigInteger('offboarding_id')->nullable()->after('user_id')->index();
            }
        });

        Schema::table('deposits', function (Blueprint $table) {
            if (! Schema::hasColumn('deposits', 'deposit_category_id')) {
                $table->unsignedBigInteger('deposit_category_id')->nullable()->after('account_id')->index();
            }
        });

        Schema::table('expenses', function (Blueprint $table) {
            if (! Schema::hasColumn('expenses', 'vendor_id')) {
                $table->unsignedBigInteger('vendor_id')->nullable()->after('account_id')->index();
            }
        });
    }

    public function down(): void
    {
        Schema::table('resignations', function (Blueprint $table) {
            if (Schema::hasColumn('resignations', 'offboarding_id')) {
                $table->dropColumn('offboarding_id');
            }
        });

        Schema::table('terminations', function (Blueprint $table) {
            if (Schema::hasColumn('terminations', 'offboarding_id')) {
                $table->dropColumn('offboarding_id');
            }
        });

        Schema::table('deposits', function (Blueprint $table) {
            if (Schema::hasColumn('deposits', 'deposit_category_id')) {
                $table->dropColumn('deposit_category_id');
            }
        });

        Schema::table('expenses', function (Blueprint $table) {
            if (Schema::hasColumn('expenses', 'vendor_id')) {
                $table->dropColumn('vendor_id');
            }
        });
    }
};
