<?php

use App\Models\Pouzivatel\PouzivatelStavEnum;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

/**
 * Class ZmenaStlpcaBlokovanyNaStavPouzivateliaTable
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('pouzivatelia', function (Blueprint $table) {
            $table->string('blokovany')
                ->change()
                ->default(PouzivatelStavEnum::AKTIVNY());
            $table->renameColumn('blokovany', 'stav');
        });

        DB::update(
            'update pouzivatelia set stav = ? where stav = ?',
            [
                PouzivatelStavEnum::BLOKOVANY(),
                'true',
            ]
        );

        DB::update(
            'update pouzivatelia set stav = ? where stav = ?',
            [
                PouzivatelStavEnum::AKTIVNY(),
                'false',
            ]
        );
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('pouzivatelia', function (Blueprint $table) {
            $table->renameColumn('stav', 'blokovany');
        });

        DB::update(
            'update pouzivatelia set blokovany = ? where blokovany = ?',
            [
                'false',
                PouzivatelStavEnum::AKTIVNY(),
            ]
        );

        DB::update(
            'update pouzivatelia set blokovany = ? where blokovany = ?',
            [
                'true',
                PouzivatelStavEnum::BLOKOVANY(),
            ]
        );

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