<?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.
     */
    public function up(): void
    {
        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.
     */
    public function down(): void
    {
        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);
        });
    }
};
