<?php

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

/**
 * Whether a copy of this invoice reached the archive, and what went wrong.
 *
 * Two columns rather than one flag, because "not yet" and "tried and failed"
 * need different answers from a person. An invoice issued while the office
 * connection was down is not on the archive and nothing about the invoice says
 * so — the sweep that retries it needs to know which ones to pick up, and the
 * console needs something to show for the ones it cannot.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('invoices', function (Blueprint $table) {
            $table->timestamp('exported_at')->nullable()->after('sent_at');
            $table->string('export_error')->nullable()->after('exported_at');
            // The sweep asks for "issued but not exported" on every tick.
            $table->index('exported_at');
        });
    }

    public function down(): void
    {
        Schema::table('invoices', function (Blueprint $table) {
            $table->dropIndex(['exported_at']);
            $table->dropColumn(['exported_at', 'export_error']);
        });
    }
};
