<?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 BplanBase\Globals\Enums\UserAccessLevel;
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('users');

    } // down()


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

        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->enum('access_level', array_column(UserAccessLevel::cases(), 'name'))->default('Staff');
                $Table->foreignId('current_team_id')->nullable();
                $Table->string('email');
                $Table->timestamp('email_verified_at')->nullable();
                $Table->boolean('internal')->default(0);
                $Table->string('name');
                $Table->string('password');
                $Table->string('profile_photo_path', 2048)->nullable();
                $Table->boolean('restricted')->default(0);
                $Table->foreignId('tenant_id')->constrained(table: 'tenants', indexName: 'users_FK_tenant_id');

                $Table->rememberToken();

                $Table->unique('email',    'users_UKI_email');
                $Table->index('tenant_id', 'users_FKI_tenant_id');
                $Table->unique('uuid',     'users_UKI_uuid');

                new CodeGenerator($Table, [
                    'appends' => [
                        'full_name' => [
                            'return' => '$this->name',
                            'return-type' => 'string',
                        ],
                        'full_name_reverted' => [
                            'return' => '$this->name',
                            'return-type' => 'string',
                        ],
                        /*
                        **  Es wird keine Methode erzeugt, weil die entsprechende Methode bereits im Trait "HasProfilePhoto"
                        **  definiert ist. */
                        'profile_photo_url' => CodeGenerator::APPENDS_JSONAPI_SCHEMA + CodeGenerator::APPENDS_LARAVEL_MODEL_ARRAY,
                    ],
                    'columns' => [
                        'active' => [
                            'sortable' => true,
                        ],
                        'email' => [
                            'ruleType' => 'email',
                            'sortable' => true,
                        ],
                        'internal' => [
                            'sortable' => true,
                        ],
                        'name' => [
                            'sortable' => true,
                        ],
                        'password' => [
                            'cast' => 'hashed',
                            'hidden' => true,
                        ],
                        'remember_token' => [
                            'guarded' => true,
                            'hidden' => true,
                        ],
                        'tenant_id' => [
                            'guarded' => true,
                            'sortable' => true,
                            // 'relationName' => 'user-tenant',            // User-Model Relationenmethode, Standard: tenant()
                            // 'reverseRelationName' => 'tenant-users',    // Tenant-Model Relationenmethode, Standard: users()
                        ],
                    ],
                    'exclude' => [
                        // 'jsonapi.policy',
                        // 'jsonapi.request',
                        // 'jsonapi.schema',
                        // 'laravel.action.create',
                        // 'laravel.action.delete',
                        // 'laravel.action.update',
                        // 'laravel.controller',
                        // 'laravel.model',
                        // 'laravel.observer',
                        // 'laravel.repository',
                        // 'laravel.service',
                        'livewire.index',
                        'livewire.modal.create',
                        'livewire.modal.edit',

                    ],
                    'extends' => [
                        'laravel.model' => 'Illuminate\\Foundation\\Auth\\User as Authenticatable',
                    ],
                    'indexQueries' => [
                        'terminal' => '$Query->where(\'active\', \'=\', 1)',
                    ],
                    'routing' => [
                        'terminal' => 'readOnly',
                    ],
                    'traits' => [
                        'laravel.model' => [
    //                        'BplanBase\\Globals\\Traits\\HasActiveScope',
                            'BplanBase\\Globals\\Traits\\HasNoInternalScope',
                            'BplanBase\\Globals\\Traits\\HasOrderedScope',
                            'Illuminate\\Contracts\\Auth\\MustVerifyEmail',
                            'Illuminate\\Notifications\\Notifiable',
                            'Laravel\\Sanctum\\HasApiTokens',
                            'Laravel\\Jetstream\\HasProfilePhoto',
                            'Laravel\\Fortify\\TwoFactorAuthenticatable',
                            'Laravel\\Sanctum\\HasApiTokens',
                        ],
                    ],
                    'uses' => [
                        'laravel.model' => [
                            // 'BplanBase\\Globals\\Models\\User as BaseUser',
                            'BplanBase\\Globals\\Observers\\UserObserver',
                            'BplanBase\\Globals\\Enums\\UserAccessLevel',
                            'BplanBase\\Globals\\Scopes\\TenantScope',
                            'Illuminate\\Notifications\\Notification',
                            'Illuminate\\Support\\Str',
                        ],
                    ],
                ]);

            });

        }
    } // up()


};
