<?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\Enums\SortContext;
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('printers');

    } // down()


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

        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->json('config_settings')->nullable();
                $Table->string('host', 255);
                $Table->string('identifier', 100)->nullable()->unique();
                $Table->string('label', 100);
                $Table->foreignId('location_id')->nullable()->constrained(table: 'locations', indexName: 'printers_FK_location_id')->nullOnDelete();
                $Table->string('name', 100);
                $Table->foreignId('printer_type_id')->constrained(table: 'printer_types', indexName: 'printers_FK_printer_type_id');
                $Table->foreignId('tenant_id')->constrained(table: 'tenants', indexName: 'printers_FK_tenant_id');

                $Table->index('location_id',     'printers_FKI_location_id');
                $Table->index('printer_type_id', 'printers_FKI_printer_type_id');
                $Table->index('tenant_id',       'printers_FKI_tenant_id');

                new CodeGenerator($Table, [
                    'columns' => [
                        'id' => [
                            'gridField' => 1,
                        ],
                        'config_settings' => [
                            'jsonType' => CodeGenerator::JSON_TYPE_OBJECT,
                        ],
                        'host' => [
                            'searchField' => true,
                        ],
                        'label' => [
                            'gridField'   => 4,
                            'searchField' => true,
                            'sortable'    => SortContext::Grid->value,
                        ],
                        'location_id' => [
                            'gridField'           => 5,
                            'relationName'        => 'physical-location',
                            'reverseRelationName' => 'physical-printers',
                        ],
                        'name' => [
                            'gridField' => 3,
                            'sortable'  => true,
                        ],
                        'printer_type_id' => [
                            'gridField' => 2,
                        ],
                    ],
                    'exclude' => [
                        'laravel.permission-seeder',
                    ],
                    'routing' => [
                        'terminal' => 'readOnly',
                    ],
                ]);

            });
        }
    } // up()

};
