<?php

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

/**
 * What the proxy actually serves, as opposed to what the customer was promised.
 *
 * A verified custom domain was announced everywhere — Instance::address(), the
 * portal, the credentials mail — while nothing routed it: the Traefik router's
 * rule was hard-coded to the platform subdomain, so the address the customer
 * was given answered nothing. These two columns are the difference between the
 * promise and the fact.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('instances', function (Blueprint $table) {
            // The hostnames the router file on the traffic host currently
            // carries. Written by ConfigureDnsAndTls after the file lands, and
            // compared against the wanted list on the next run — that
            // comparison is what lets an address re-apply rewrite the route
            // while an ordinary retry still short-circuits. `route_written`
            // alone cannot answer it: it says a file exists, not what is in it.
            $table->json('routed_hostnames')->nullable()->after('route_written');

            // Whether the CUSTOM domain's certificate is being served, kept
            // apart from `cert_ok` (the platform address) on purpose. The
            // platform address is ours: if its certificate does not appear, the
            // run has failed and somebody has to look. The custom domain's
            // certificate depends on the customer pointing their DNS at us,
            // which may not have happened and may never happen — so it is
            // recorded, never waited on, and never a reason to fail anything.
            $table->boolean('domain_cert_ok')->default(false)->after('domain_verified_at');
        });
    }

    public function down(): void
    {
        Schema::table('instances', function (Blueprint $table) {
            $table->dropColumn(['routed_hostnames', 'domain_cert_ok']);
        });
    }
};
