<?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, 2026 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('base_tenants');

    } // down()


    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('tenants', 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('domain')->nullable();   // unique();
            $Table->string('identifier', 30);
            $Table->boolean('internal')->default(0);
            $Table->foreignId('master_id')->nullable()->constrained(table: 'tenants', indexName: 'tenants_FK_master_id');
            $Table->string('path', 32);

            $Table->index('master_id', 'tenants_FKI_master_id');
            $Table->index('path',      'tenants_IDX_path');
            $Table->unique('path',     'tenants_UKI_path');
            $Table->unique('uuid',     'tenants_UKI_uuid');

            new CodeGenerator($Table, [
                'columns' => [
                    'uuid' => [
                        'guarded' => true,
                    ],
                    'active' => [
                        'sortable' => true,
                    ],
                    'identifier' => [
                        'sortable' => true,
                    ],
                    'master_id' => [
                        'relationName' => 'master-tenant',
                        'reverseRelationName' => 'sub-tenants',
                        'sortable' => true,
                    ],
                    'path' => [
                        'guarded' => true,
                    ],
                ],
                'exclude' => [
                    'livewire.index',
                    'livewire.modal.create',
                    'livewire.modal.edit',
                ],
                'extends' => [
                    'laravel.model' => 'Spatie\\Multitenancy\\Models\\Tenant as BaseTenant',
                ],
                'indexQueries' => [
                    'terminal' => '$Query->where(\'active\', \'=\', 1)',
                ],
                'routing' => [
                    'terminal' => 'readOnly',
                ],
                'traits' => [
                    'laravel.model' => [
//                        'BplanBase\\Globals\Traits\\HasActiveScope',
                        'BplanBase\\Globals\Traits\\HasNoInternalScope',
//                        'BplanBase\\Globals\Traits\\HasOrderedScope',
                        'BplanBase\\Globals\\Traits\ModelHasMaterializedPath',
                    ],
                ],

            ]);

        });

    } // up()

};
