<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Seka\Portal\Modul\Vzdelavanie\Models\Pouzivatel\Testovanie\StavVyhodnoteniaTestuEnum;
use Seka\Portal\Modul\Vzdelavanie\Models\Pouzivatel\Testovanie\Test;

/**
 * Class PridanyStlpecStavuVyhodnoteniaTestuVzdelavaniePouzivatelTestovanieTestyTable
 */
return new class extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::table('vzdelavanie_pouzivatel_testovanie_testy', function (Blueprint $table) {
            $table
                ->string('stav')
                ->after('popis')
                ->default(StavVyhodnoteniaTestuEnum::NEVYHODNOTENY);
        });

        Test::query()->each(function (Test $test) {
            $stav = is_null(
                $test->casova_znacka_vyhodnotenia,
            ) ? StavVyhodnoteniaTestuEnum::NEVYHODNOTENY : StavVyhodnoteniaTestuEnum::VYHODNOTENY;
            $test->stav = $stav;
            $test->save();
        });
    }

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