<?php

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

/**
 * When monitoring last actually answered for a target.
 *
 * The status column was written once, at provisioning, with the value 'up', and
 * nothing ever updated it. Every reader — the console's notices, the public
 * status page — therefore reported every instance as healthy forever, including
 * through an outage. A stored verdict with no timestamp cannot be told apart
 * from a fresh one, so the timestamp comes first and the sync job comes with it.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('monitoring_targets', function (Blueprint $table) {
            $table->timestamp('checked_at')->nullable()->after('status');
        });

        // Every existing row is an unverified claim. Clearing it is the point:
        // "we have not looked" must not keep reading as "it is fine".
        DB::table('monitoring_targets')->update(['status' => 'unknown', 'checked_at' => null]);
    }

    public function down(): void
    {
        Schema::table('monitoring_targets', fn (Blueprint $table) => $table->dropColumn('checked_at'));
    }
};
