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

    } // down()


    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('user_records', function (Blueprint $Table) {
            $Table->foreignId('id')->constrained(table: 'users', indexName: 'user_records_PK');
            $Table->uuid('uuid');

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

            $Table->string('email', 255);
            $Table->string('first_name', 50)->nullable();
            $Table->string('last_name', 50)->nullable();

            $Table->unique('id',   'user_records_UKI_id');
            $Table->unique('uuid', 'user_records_UKI_uuid');

            new CodeGenerator($Table, [
                'appends' => [
                    /*
                    **  Append Fields */
                    'full_name' => [
                        'return'      => 'trim($this->first_name.\' \'.$this->last_name)',
                        'return-type' => 'string',
                        'searchField' => true,
                    ],
                    'full_name_reverted' => [
                        'return' => 'trim($this->last_name.\', \'.$this->first_name, \',\')',
                        'return-type' => 'string',
                    ],
                ],
                'columns' => [
                    'id' => [
                        'relationName' => 'user',
                        'reverseRelationType' => 'HasOne',
                    ],
                    'email' => [
                        'ruleType' => 'email',
                        'searchField' => true,
                        'sortable' => true,
                    ],
                    'first_name' => [
                        'searchField' => true,
                    ],
                    'last_name' => [
                        'searchField' => true,
                        'sortable'    => true,
                    ],
                ],
                'exclude' => [
                    'laravel.permission-seeder',
                ],
                'routing' => [
                    'terminal' => 'readOnly',
                ],
//                'traits' => [
//                    'laravel.model' => [
//                        'BplanBase\\Globals\Traits\\HasOrderedScope',
//                    ],
//                ],
            ]);

        });
    } // up()

};
