<?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('job_offers', function (Blueprint $table) {
            $table->dropForeign(['account_id']);
            $table->dropColumn('account_id');
        });

        Schema::table('projects', function (Blueprint $table) {
            $table->dropForeign(['account_id']);
            $table->dropColumn('account_id');
        });

        Schema::table('offices', function (Blueprint $table) {
            $table->dropForeign(['account_id']);
            $table->dropColumn('account_id');
        });

        Schema::table('result_entities', function (Blueprint $table) {
            $table->dropForeign(['account_id']);
            $table->dropColumn('account_id');
        });

        Schema::table('coordination_group_members', function (Blueprint $table) {
            $table->dropForeign(['account_id']);
            $table->dropColumn('account_id');
        });

        Schema::table('executing_party_group_companies', function (Blueprint $table) {
            $table->dropForeign(['account_id']);
            $table->dropColumn('account_id');
        });
    }

    public function down(): void
    {
        Schema::table('job_offers', function (Blueprint $table) {
            $table->unsignedBigInteger('account_id')->nullable()->after('id');
            $table->foreign('account_id')->references('id')->on('accounts')->onDelete('CASCADE')->onUpdate('CASCADE');
        });

        Schema::table('projects', function (Blueprint $table) {
            $table->unsignedBigInteger('account_id')->nullable()->after('id');
            $table->foreign('account_id')->references('id')->on('accounts')->onDelete('CASCADE')->onUpdate('CASCADE');
        });

        Schema::table('offices', function (Blueprint $table) {
            $table->unsignedBigInteger('account_id')->nullable()->after('id');
            $table->foreign('account_id')->references('id')->on('accounts')->onDelete('CASCADE')->onUpdate('CASCADE');
        });

        Schema::table('result_entities', function (Blueprint $table) {
            $table->unsignedBigInteger('account_id')->nullable()->after('id');
            $table->foreign('account_id')->references('id')->on('accounts')->onDelete('SET NULL')->onUpdate('CASCADE');
        });

        Schema::table('coordination_group_members', function (Blueprint $table) {
            $table->unsignedBigInteger('account_id')->nullable()->after('coordination_group_id');
            $table->foreign('account_id')->references('id')->on('accounts')->onDelete('SET NULL')->onUpdate('CASCADE');
        });

        Schema::table('executing_party_group_companies', function (Blueprint $table) {
            $table->unsignedBigInteger('account_id')->nullable()->after('company_profile_id');
            $table->foreign('account_id')->references('id')->on('accounts')->onDelete('SET NULL')->onUpdate('CASCADE');
        });
    }
};
