<?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 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
    {
        $tableName = 'languages';

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

                $Table->timestamps();
                $Table->softDeletes('deleted_at');

                $Table->string('code', 2);
                $Table->tinyInteger('context')->default(LanguageContext::None->value)->comment('Siehe Enums/LanguageContext: 0 = None, 1 = Terminal, 2 = Web, 3 = Both');
                $Table->json('labels');

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

            });

        }
    } // up()


};
