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

            $table->string('wa_id', 20)->unique();
            $table->boolean('is_competition')->default(false);

            $table->string('promoter', 255)->nullable();
            $table->string('promoter_address', 255)->nullable();
            $table->string('promoter_postal_code', 50)->nullable();
            $table->string('promoter_city', 255)->nullable();
            $table->string('promoter_nuts_code', 40)->nullable();
            $table->string('contact_name', 255)->nullable();
            $table->string('contact_phone', 255)->nullable();
            $table->string('contact_email', 255)->nullable();
            $table->unsignedBigInteger('promoter_country_id')->nullable();

            $table->string('original_language_code', 10)->nullable();

            $table->date('publication_date');
            $table->dateTime('submission_until')->nullable();
            $table->dateTime('information_request_until')->nullable();

            $table->string('procedure_type_code', 20)->nullable();
            $table->unsignedBigInteger('procedure_type_id');

            $table->string('location', 255)->nullable();
            $table->string('location_address', 255)->nullable();
            $table->string('location_postal_code', 50)->nullable();
            $table->string('location_city', 255)->nullable();
            $table->string('location_nuts_code', 40)->nullable();
            $table->unsignedBigInteger('location_country_id')->nullable();

            $table->string('participant', 255)->nullable();
            $table->longText('content')->nullable();
            $table->longText('aditional_content')->nullable();

            $table->string('xml_url', 255)->nullable();
            $table->string('html_url', 255)->nullable();

            $table->text('coordination')->nullable();
            $table->string('official_number', 30)->nullable();
            $table->text('description')->nullable();
            $table->text('note')->nullable();

            $table->nullableTimestamps();

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

            $table->foreign('promoter_country_id')->references('id')
                ->on('countries')->onDelete('SET NULL')->onUpdate('CASCADE');

            $table->foreign('location_country_id')->references('id')
                ->on('countries')->onDelete('SET NULL')->onUpdate('CASCADE');

            $table->foreign('procedure_type_id')->references('id')
                ->on('procedure_types')->onDelete('RESTRICT')->onUpdate('CASCADE');
        });

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

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

            // Languages
            $table->unique(['competition_id', 'language_id'], 'idx_competition_language');
            $table->foreign('competition_id')
                ->references('id')
                ->on('competitions')
                ->onDelete('CASCADE')
                ->onUpdate('CASCADE');
        });

        Schema::create('competition_cpvs', function (Blueprint $table) {
            $table->unsignedBigInteger('competition_id');
            $table->unsignedBigInteger('cpv_id');
            $table->integer('position')->nullable();
            $table->primary(['competition_id', 'cpv_id']);

            $table->foreign('competition_id')->references('id')
                ->on('competitions')->onDelete('CASCADE')->onUpdate('CASCADE');
            $table->foreign('cpv_id')->references('id')
                ->on('cpvs')->onDelete('CASCADE')->onUpdate('CASCADE');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('competition_cpvs');
        Schema::dropIfExists('competition_languages');
        Schema::dropIfExists('competitions');
    }
};
