<?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\LanguageContext;
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('languages');

    } // down()


    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('languages', function (Blueprint $Table) {
            $Table->id();
            $Table->boolean('active')->default(1);

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

            $Table->string('code', 2);
            $Table->tinyInteger('context')->comment('Siehe Enums/LanguageContext: 1 = Backend, 2 = Kiosk, 3 = Both');
            $Table->json('labels')->nullable();
            $Table->foreignId('tenant_id')->constrained(table: 'tenants', indexName: 'languages_FK_tenant_id');

            $Table->index('tenant_id', 'languages_FKI_tenant_id');

            new CodeGenerator($Table, [
                'columns' => [
                    'code' => [
                        'sortable' => true,
                    ],
                    'context' => [
                        'rules' => [
                            'required',
                            'Rule::enum(LanguageContext::class)',
                        ]
                    ],
                    'labels' => [
                        'jsonType' => CodeGenerator::JSON_TYPE_LOCALE_OBJECT,
                        'sortable' => true,
                    ],
                ],
                'exclude' => [
                    'laravel.permission-seeder',
                ],
                'indexQueries' => [
                    'terminal' => '$Query->where(\'active\', \'=\', 1)',
                ],
                'routing' => [
                    'terminal' => [
                        'actions' => [
                            'show',
                        ],
                        'method' => 'only',
                    ],
                ],
                'uses' => [
                    'jsonapi.request' => [
                        'BplanBase\\Globals\\Enums\\LanguageContext',
                        'Illuminate\\Validation\\Rule',
                    ],
                    'laravel.form-request.store' => [
                        'BplanBase\\Globals\\Enums\\LanguageContext',
                        'Illuminate\\Validation\\Rule',
                    ],
                    'laravel.form-request.update' => [
                        'BplanBase\\Globals\\Enums\\LanguageContext',
                        'Illuminate\\Validation\\Rule',
                    ],
                ],
            ]);

        });
    } // up()


};
