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

    } // down()


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

        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->foreignId('role_id')->constrained(table: 'roles', indexName: 'role_user_FK_role_id');
                $Table->foreignId('user_id')->constrained(table: 'users', indexName: 'role_user_FK_user_id');

                $Table->index('role_id', 'role_user_FKI_role_id');
                $Table->index('user_id', 'role_user_FKI_user_id');
                $Table->unique('uuid',   'role_user_UKI_uuid');

                new CodeGenerator($Table, [
                    'exclude' => [
                        'laravel.action.update',
                        'livewire.*',
                    ],
                    'pivot' => [
                        'relations' => [
                            'role_id' => 'roles',
                            'user_id' => 'users',
                        ],
                    ],
                    'routing' => [
                        'terminal' => 'readOnly',
                    ],

                ]);

            });

        }
    } // up()

};
