<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('wo_works', function (Blueprint $t) {
            if (! Schema::hasColumn('wo_works', 'mechanic_status')) {
                $t->string('mechanic_status', 16)->default('pending')->after('status');
                // values: pending | in_progress | paused | done | blocked
            }
            if (! Schema::hasColumn('wo_works', 'mechanic_started_at')) {
                $t->timestamp('mechanic_started_at')->nullable()->after('mechanic_status');
            }
            if (! Schema::hasColumn('wo_works', 'mechanic_done_at')) {
                $t->timestamp('mechanic_done_at')->nullable()->after('mechanic_started_at');
            }
            if (! Schema::hasColumn('wo_works', 'actual_hours')) {
                $t->decimal('actual_hours', 6, 2)->nullable()->after('mechanic_done_at');
            }
            if (! Schema::hasColumn('wo_works', 'paused_seconds_total')) {
                $t->integer('paused_seconds_total')->default(0)->after('actual_hours');
            }
            if (! Schema::hasColumn('wo_works', 'paused_at')) {
                $t->timestamp('paused_at')->nullable()->after('paused_seconds_total');
            }
            if (! Schema::hasColumn('wo_works', 'block_reason')) {
                $t->string('block_reason', 32)->nullable()->after('paused_at');
                // values: missing_part | awaiting_approval | broken_equipment | other
            }
            if (! Schema::hasColumn('wo_works', 'block_note')) {
                $t->text('block_note')->nullable()->after('block_reason');
            }
        });
    }

    public function down(): void
    {
        Schema::table('wo_works', function (Blueprint $t) {
            foreach (['mechanic_status', 'mechanic_started_at', 'mechanic_done_at', 'actual_hours', 'paused_seconds_total', 'paused_at', 'block_reason', 'block_note'] as $col) {
                if (Schema::hasColumn('wo_works', $col)) $t->dropColumn($col);
            }
        });
    }
};
