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

    } // down()


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

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

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

                $Table->string('callback_url', 255);
                $Table->text('error_message')->nullable();
                $Table->timestamp('finished_at')->nullable();
                $Table->json('payload');
                $Table->foreignId('printer_id')->nullable()->constrained(table: 'printers', indexName: 'print_jobs_FK_printer_id')->nullOnDelete();
                $Table->timestamp('sent_at')->nullable();
                $Table->enum('status', array_column(PrintJobStatus::cases(), 'value'))->default(PrintJobStatus::Pending->value);
                $Table->foreignId('tenant_id')->constrained(table: 'tenants', indexName: 'print_jobs_FK_tenant_id');

                $Table->index('printer_id', 'print_jobs_FKI_printer_id');
                $Table->index('status',     'print_jobs_IDX_status');
                $Table->index('tenant_id',  'print_jobs_FKI_tenant_id');

                new CodeGenerator($Table, [
                    'columns' => [
                        'id' => [
                            'gridField' => 1,
                        ],
                        'created_at' => [
                            'gridField' => 4,
                            'sortable'  => true,
                        ],
                        'finished_at' => [
                            'gridField' => 6,
                            'sortable'  => true,
                        ],
                        'payload' => [
                            'jsonType' => CodeGenerator::JSON_TYPE_ARRAY,
                        ],
                        'printer_id' => [
                            'gridField'   => 2,
                            'searchField' => true,
                            // 'sortable'    => true,
                        ],
                        'sent_at' => [
                            'gridField' => 5,
                            'sortable'  => true,
                        ],
                        'status' => [
                            'cast'        => PrintJobStatus::class,
                            'gridField'   => 3,
                            'searchField' => true,
                            'sortable'    => true,
                        ],
                    ],
                    'exclude' => [
                        'laravel.action.update',
                        'laravel.action.delete',
                        'laravel.permission-seeder',
                        'livewire.modal.*',
                    ],
                    'routing' => [
                        'data'     => 'readOnly',
                        'terminal' => 'readOnly',
                    ],
                    'traits' => [
                        'laravel.model' => [
                            'BplanBase\\Globals\\Traits\\HasOrderedScope',
                        ],
                    ],
                    'uses' => [
                        'laravel.model' => [
                            'BplanBase\\Globals\\Enums\\PrintJobStatus',
                            'Illuminate\\Database\\Eloquent\\Builder',
                        ],
                    ],
                ]);

            });
        }
    } // up()

};
