<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Seka\Portal\Modul\Vzdelavanie\Enums\ModelStatusEnum;
use Illuminate\Database\Migrations\Migration;

/**
 * Class UpdateVzdelavanieKniznicaUcebniceTable
 */
return new class extends Migration
{
    /**
     * @return void
     */
    public function up(): void
    {
        Schema::table('vzdelavanie_kniznica_ucebnice', function (Blueprint $table) {
            $table->renameColumn('verejne', 'stav');
            $table->string('stav')
                ->change()
                ->default(ModelStatusEnum::NEPUBLIKOVANE->name);
        });

        DB::update(
            'update vzdelavanie_kniznica_ucebnice set stav = ? where stav = ?',
            [
                ModelStatusEnum::NEPUBLIKOVANE->name,
                false,
            ]
        );

        DB::update(
            'update vzdelavanie_kniznica_ucebnice set stav = ? where stav = ?',
            [
                ModelStatusEnum::PUBLIKOVANE->name,
                true,
            ]
        );
    }

    /**
     * @return void
     */
    public function down(): void
    {
        Schema::table('vzdelavanie_kniznica_ucebnice', function (Blueprint $table) {
            $table->renameColumn('stav', 'verejne');
        });

        DB::update(
            'update vzdelavanie_kniznica_ucebnice set verejne = ? where verejne = ?',
            [
                false,
                ModelStatusEnum::NEPUBLIKOVANE->name,
            ]
        );

        DB::update(
            'update vzdelavanie_kniznica_ucebnice set verejne = ? where verejne = ?',
            [
                true,
                ModelStatusEnum::PUBLIKOVANE->name,
            ]
        );

        Schema::table('vzdelavanie_kniznica_ucebnice', function (Blueprint $table) {
            $table->boolean('verejne')
                ->change()
                ->default(false);
        });
    }
};
