<?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\Loaders\CodeGenerator;
use BplanBase\Globals\Registries\LocationTypeRegistry;
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('location_types');

    } // down()


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

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

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

                $Table->enum('identifier', LocationTypeRegistry::names());
                $Table->boolean('is_internal')->default(0);
                $Table->json('sub_types')->nullable();

                $Table->unique('identifier', 'location_types_UKI_identifier')->comment('Die Werte dieses Feldes werden beim Setup aus der LocationTypeRegistry gespeist.');

                $Table->comment('Technische Daten. Es dürfen weder neue Datensätze hinzugefügt noch vorhandene Datensätze verändert oder entfernt werden.');

                new CodeGenerator($Table, [
                    'columns' => [
                        'identifier' => [
                            'guarded' => true,
                            'rules' => [
                                'Rule::in(LocationTypeRegistry::names())',
                            ],
                            'sortable' => true,
                        ],
                        'sub_types' => [
                            'jsonType' => CodeGenerator::JSON_TYPE_ARRAY,
                        ],
                    ],
                    'exclude' => [
                        'laravel.permission-seeder',
                        'livewire.*',
                    ],
                    'routing' => [
                        'terminal' => 'readOnly',
                    ],
                    'uses' => [
                        'jsonapi.request' => [
                            'Illuminate\\Validation\\Rule',
                        ],
                        'laravel.form-request.store' => [
                            'Illuminate\\Validation\\Rule',
                        ],
                        'laravel.form-request.update' => [
                            'Illuminate\\Validation\\Rule',
                        ],
                    ],
                ]);

            });

        }
    } // up()


};
