<?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\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('modules');

    } // down()


    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('modules', 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('base_namespace', 255);
            $Table->string('identifier', 30);
            $Table->boolean('internal')->default(0);
            $Table->string('label', 30);
            $Table->string('package_name', 50);
            $Table->string('route_prefix', 30);
            $Table->string('vendor_name', 50);
            $Table->string('vendor_package', 100);

            $Table->unique('identifier',   'modules_UKI_identifier');
            $Table->unique('route_prefix', 'modules_UKI_route_prefix');
            $Table->unique('uuid',         'modules_UKI_uuid');

            new CodeGenerator($Table, [
                'columns' => [
                    'identifier' => [
                        'guarded'  => true,
                        'sortable' => true,
                    ],
                    'label' => [
                        'searchField' => true,
                    ],
                    'vendor_name' => [
                        'searchField' => true,
                    ],
                ],
                'exclude' => [
                    'livewire.*',
                ],
                'indexQueries' => [
                    'terminal' => '$Query->where(\'active\', \'=\', 1)',
                ],
                'routing' => [
                    'terminal' => 'readOnly',
                ],

            ]);

        });
    } // up()

};
