<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('account_company_specialisations', function (Blueprint $table) {
            $table->unsignedBigInteger('account_id');
            $table->unsignedBigInteger('company_specialisation_id');
            $table->unsignedInteger('position')->nullable();

            $table->foreign('account_id', 'account_specialisations_fk')->references('id')
                ->on('accounts')->onDelete('CASCADE')->onUpdate('CASCADE');

            $table->foreign('company_specialisation_id', 'account_company_specialisations_fk')->references('id')
                ->on('company_specialisations')->onDelete('CASCADE')->onUpdate('CASCADE');
        });

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

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('account_company_specialisations');

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