<?php
/**
 * Migration Class
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  Migrations
 * @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      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
return new class extends Migration
{


    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('terminal_connections');

    } // down()


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

        if (Schema::hasTable($tableName) === false) {
            Schema::create($tableName, function (Blueprint $Table) {
                $Table->id();
                $Table->uuid('uuid')->unique();

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

                $Table->timestamp('connected_at');
                $Table->timestamp('disconnected_at')->nullable();
                $Table->foreignId('location_id')->unique()->constrained(table: 'locations', indexName: 'terminal_connections_FK_location_id')->cascadeOnDelete();
                $Table->foreignId('tenant_id')->constrained(table: 'tenants', indexName: 'terminal_connections_FK_tenant_id');

                $Table->index('location_id', 'terminal_connections_FKI_location_id');
                $Table->index('tenant_id',   'terminal_connections_FKI_tenant_id');

                new CodeGenerator($Table, [
                    'columns' => [
                        'id' => [
                            'gridField' => 1,
                        ],
                        'connected_at' => [
                            'gridField' => 3,
                            'sortable'  => true,
                        ],
                        'disconnected_at' => [
                            'gridField' => 4,
                            'sortable'  => true,
                        ],
                        'location_id' => [
                            'gridField'   => 2,
                            // 'searchField' => true,
                        ],
                    ],
                    'exclude' => [
                        'laravel.action.*',
                        'livewire.modal.*',
                    ],
                    'routing' => [
                        'data'     => 'readOnly',
                        'terminal' => 'readOnly',
                    ],
                ]);

            });
        }
    } // up()

};
