<?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('states', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('country_id');
            $table->unsignedBigInteger('backend_user_id')->nullable();
            $table->geometry('location', 'point')->nullable();
            $table->smallInteger('location_zoom')->nullable();
            $table->string('code', 20)->nullable();

            $table->nullableTimestamps();

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

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

            // Languages
            $table->unique(['state_id', 'language_id'], 'idx_country_language');
            $table->foreign('state_id')
                ->references('id')
                ->on('states')
                ->onDelete('CASCADE')
                ->onUpdate('CASCADE');
        });
    }

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