<?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('archives', function (Blueprint $table) {
            $table->bigIncrements('id');

            $table->date('archive_date');
            $table->unsignedBigInteger('document_id')->nullable();
            $table->unsignedBigInteger('image_id')->nullable();

            $table->unsignedBigInteger('backend_user_id')->nullable();
            $table->nullableTimestamps();

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

        Schema::create('archive_languages', function (Blueprint $table) {
            $table->bigIncrements('translation_id');
            $table->unsignedBigInteger('archive_id');
            $table->string('language_id', 10);

            $table->string('title', 255);
            $table->string('subtitle', 255)->nullable();
            $table->string('label', 255)->nullable();

            $table->unique(['archive_id', 'language_id'], 'idx_archive_language');
            $table->foreign('archive_id')
                ->references('id')
                ->on('archives')
                ->onDelete('CASCADE')
                ->onUpdate('CASCADE');
        });
    }

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