<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Schema;
use Seka\Portal\Modul\Terminy\Entities\Udalost\StavUpozornenia;
use Seka\Portal\Modul\Terminy\Entities\Udalost\Udalost;

/**
 * Class AddFieldUpozornenieTerminyUdalostUdalostiTable
 */
return new class extends Migration {
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('terminy_udalost_udalosti', function (Blueprint $table) {
            $table
                ->string('upozornenie')
                ->default(StavUpozornenia::NEPOVOLENE->name)
                ->after('registracia');
        });

        Udalost::query()->chunk(50, function (Collection $collection) {
            $collection->each(function (Udalost $udalost) {
                $udalost->upozornenie = StavUpozornenia::POVOLENE->name;
                $udalost->save();
            });
        });
    }

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