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


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


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


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

    } // down()


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

        if (Schema::hasTable($tableName) === false) {
            Schema::create($tableName, function (Blueprint $Table) {
                $Table->id();
                $Table->uuid('uuid');
                $Table->boolean('active')->default(1);

                $Table->timestamp('created_at');
                $Table->softDeletes('deleted_at');
                $Table->timestamp('updated_at');

                $Table->string('address_addition', 255)->nullable();
                $Table->string('city', 50)->nullable();
                /*
                **  Support for spatial types depends on your database driver. Please refer to your
                **  database's documentation. If your application is utilizing a PostgreSQL database,
                **  you must install the PostGIS extension before the geometry method may be used. */
                $Table->geometry('coordinates', subtype: 'point', srid: 0)->nullable();
                $Table->enum('direction', array_column(AccessPointDirection::cases(), 'value'))->default(AccessPointDirection::Moot->value);
                $Table->string('email', 255)->nullable();
                $Table->string('house_number', 5)->nullable();
                $Table->string('identifier', 30)->nullable()->comment('Ein eindeutiger Bezeichner pro LocationType. Aufbau bei AccessPoints: AP#### ("AP" gefolgt von 5 Ziffern). Bei Kamera-/Schrankensystemen entspricht die Zahl dem verwendeten Port.');
                $Table->boolean('internal')->default(0);
                $Table->boolean('is_bookable')->default(1);
                $Table->boolean('is_connected')->default(0)->comment('Verbindungsstatus für den LocationType "Kiosk".');
                $Table->json('labels');
                $Table->foreignId('location_type_id')->nullable()->constrained(table: 'location_types', indexName: 'locations_FK_location_type_id');
                $Table->foreignId('master_id')->nullable()->constrained(table: 'locations', indexName: 'locations_FK_master_id');
                // $Table->string('path', 32);
                $Table->string('path', 32)->nullable();
                $Table->string('phone_number', 30)->nullable();
                $Table->string('street', 50)->nullable();
                $Table->string('sub_type', 30)->nullable();
                $Table->foreignId('tenant_id')->constrained(table: 'tenants', indexName: 'locations_FK_tenant_id');
                $Table->boolean('top_level')->default(0);
                $Table->string('zip_code', 10)->nullable();

                $Table->unique('identifier',          'locations_UKI_identifier');
                $Table->index('location_type_id',     'locations_FKI_location_type_id');
                $Table->index('master_id',            'locations_FKI_master_id');
                $Table->index('path',                 'locations_IDX_path');
                $Table->unique(['path', 'tenant_id'], 'locations_UKI_path_tenant_id');
                $Table->index('tenant_id',            'locations_FKI_tenant_id');

                new CodeGenerator($Table, [
                    'additionalGridFields' => [
                        'sub_locations' => 8,
                    ],
                    'appends' => [
                        'label' => [
                            'type' => 'locale',
                        ],
                    ],
                    'columns' => [
                        'id' => [
                            'gridField' => 1,
                        ],
                        'city' => [
                            'gridField' => 3,
                            'sortable' => SortContext::JsonApi->value,
                        ],
                        'direction' => [
                            'cast' => AccessPointDirection::class,
                        ],
                        'email' => [
                            'ruleType' => 'email',
                            'sortable' => SortContext::JsonApi->value,
                        ],
                        'house_number' => [
                            'gridField' => 6,
                        ],
                        'identifier' => [
                            'guarded' => true,
                        ],
                        'internal' => [
                            'sortable' => SortContext::JsonApi->value,
                        ],
                        'is_connected' => [
                            'guarded' => true,
                        ],
                        'labels' => [
                            'gridField'   => 2,
                            'jsonType'    => CodeGenerator::JSON_TYPE_LOCALE_OBJECT,
                            'searchField' => true,
                            'sortable'    => true,
                        ],
                        'location_type_id' => [
                            // todo     Ohne die explizite Angabe des eines Namens würde "locations"/"locations" verwendet werden.
                            'reverseRelationName' => 'locations',
                        ],
                        'master_id' => [
                            // todo     Ohne die explizite Angabe des eines Namens würde "locations"/"locations" verwendet werden.
                            'reverseRelationName' => 'sub-locations',
                        ],
                        'path' => [
                            'guarded' => SortContext::JsonApi->value,
                        ],
                        'phone_number' => [
                            'sortable' => SortContext::JsonApi->value,
                        ],
                        'street' => [
                            'gridField' => 5,
                        ],
                        'sub_type' => [
                            'filter' => true,
                        ],
                        'top_level' => [
                            'sortable' => SortContext::JsonApi->value,
                        ],
                        'zip_code' => [
                            'gridField' => 4,
                        ],
                    ],
                    'routing' => [
                        'terminal' => 'readOnly',
                    ],
                    'traits' => [
                        'laravel.model' => [
                            'BplanBase\\Globals\\Traits\ModelHasMaterializedPath',
                        ],
                    ],

                ]);

            });
        }
    } // up()

};
