<?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('user_profile_specialisations', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('backend_user_id')->nullable();
            $table->nullableTimestamps();

            $table->foreign('backend_user_id')->references('id')
                ->on('backend_users')->onDelete('SET NULL')->onUpdate('CASCADE');
        });

        Schema::create('user_profile_specialisation_languages', function (Blueprint $table) {
            $table->bigIncrements('translation_id');
            $table->unsignedBigInteger('user_profile_specialisation_id');
            $table->string('language_id', 10);
            $table->string('title', 150);

            // Languages
            $table->unique(['user_profile_specialisation_id', 'language_id'], 'idx_user_specialisation_language');
            $table->foreign('user_profile_specialisation_id', 'fk_user_specialisation_language')
                ->references('id')
                ->on('user_profile_specialisations')
                ->onDelete('CASCADE')
                ->onUpdate('CASCADE');
        });

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

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropForeign(['user_profile_specialisation_id']);
            $table->dropColumn('user_profile_specialisation_id');
        });
        Schema::dropIfExists('user_profile_specialisation_languages');
        Schema::dropIfExists('user_profile_specialisations');
    }
};
