<?php

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

/**
 * Credentials an operator can set from the console instead of over SSH.
 *
 * Deliberately its own table rather than app_settings: those are plaintext,
 * cached in Redis, and read on every request by the public-site gate. Nothing
 * about that is right for a payment key.
 *
 * The value is encrypted with a key of its own (SECRETS_KEY), not APP_KEY.
 * Rotating APP_KEY is an ordinary thing to do; silently destroying every stored
 * credential when it happens — and only finding out when Stripe stops
 * answering — is not.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('app_secrets', function (Blueprint $table) {
            $table->id();
            $table->string('key')->unique();
            $table->text('value');
            // Enough to tell two keys apart at a glance, useless to a shoulder.
            $table->string('outline', 32)->nullable();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('app_secrets');
    }
};
