<?php
/**
 * Migration Class
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  Migrations
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 * @copyright   Copyright (C) 2025 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
 */


use BplanBase\CodeGenerator\Enums\SortContext;
use BplanBase\CodeGenerator\Loaders\CodeGenerator;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;


/**
 * Migration Class
 *
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
return new class extends Migration
{

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

    } // down()


    /**
     * Run the migrations.
     */
    public function up(): void
    {
        $tableName = 'countries';

        if (Schema::hasTable($tableName) === false) {
            Schema::create($tableName, function (Blueprint $Table) {
                $Table->id();

                $Table->boolean('active')->default(1);
                $Table->double('area')->nullable();
                $Table->json('capital_names')->nullable();
                $Table->string('comments', 2000)->nullable();
                $Table->string('fips10', 2)->nullable();
                $Table->string('ioc', 3)->nullable();
                $Table->string('iso3166_alpha_2', 2)->nullable();
                $Table->string('iso3166_alpha_3', 3)->nullable();
                $Table->string('iso3166_numeric', 3)->nullable();
                $Table->string('local_name', 150)->nullable();
                $Table->json('locales')->nullable()->comment('Locales in einem JSON-Array (["fr_BE", "nl_BE"]), auch wenn es nur einen einzigen Wert gibt (["de_DE"]).');
                $Table->json('long_names')->nullable();
                $Table->json('nationality_names')->nullable();
                $Table->string('nato_country_code_2', 2)->nullable();
                $Table->string('nato_country_code_3', 3)->nullable();
                $Table->json('short_names')->nullable();
                $Table->string('telephone_country_code', 5)->nullable();
                $Table->string('telephone_country_code_extended', 100)->nullable();
                $Table->string('tld', 3)->nullable();
                $Table->string('vehicle_registration_code', 3)->nullable();

                new CodeGenerator($Table, [
                    'columns' => [
                        'active' => [
                            'filter' => true,
                        ],
                        'capital_names' => [
                            'jsonType' => CodeGenerator::JSON_TYPE_LOCALE_OBJECT,
                            'sortable' => SortContext::JsonApi->value,
                        ],
                        'iso3166_alpha_2' => [
                            'sortable' => true,
                        ],
                        'iso3166_alpha_3' => [
                            'sortable' => SortContext::JsonApi->value,
                        ],
                        'iso3166_numeric' => [
                            'sortable' => SortContext::JsonApi->value,
                        ],
                        'locales' => [
                            'jsonType' => CodeGenerator::JSON_TYPE_ARRAY,
                            'sortable' => true,
                        ],
                        'long_names' => [
                            'jsonType' => CodeGenerator::JSON_TYPE_LOCALE_OBJECT,
                            'sortable' => SortContext::JsonApi->value,
                        ],
                        'nationality_names' => [
                            'jsonType' => CodeGenerator::JSON_TYPE_LOCALE_OBJECT,
                            'sortable' => SortContext::JsonApi->value,
                        ],
                        'short_names' => [
                            'gridField'   => 1,
                            'jsonType'    => CodeGenerator::JSON_TYPE_LOCALE_OBJECT,
                            'searchField' => true,
                            'sortable'    => true,
                        ],
                        'telephone_country_code' => [
                            'gridField'   => 2,
                            'searchField' => true,
                            'sortable'    => true,
                        ],
                        'telephone_country_code_extended' => [
                            'sortable' => SortContext::JsonApi->value,
                        ],
                    ],
                    'exclude' => [
                        'livewire.*',
                    ],
                    'indexQueries' => [
                        'terminal' => '$Query->where(\'active\', \'=\', 1)',
                    ],
                    'routing' => [
                        'terminal' => 'readOnly',
                    ],

                ]);

            });

        }
    } // up()

};
