<?php

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

/**
 * Drop the `ai_keys` table. It was a v2 forward-compat scaffold (created
 * 2026-04-21) for per-tenant BYO AI provider keys. The AI-key direction
 * settled 2026-06-02: in BOTH modes the key is a single admin/operator-
 * configured credential — self-hosted admin sets one org-wide key, SaaS
 * operator sets one global key (charged via plan tiers). That's a `Settings`
 * value (like mail / storage / billing-provider keys), not a per-tenant row.
 * No Eloquent model, no reads/writes (only stale FK-cascade comments, now
 * cleaned up), zero rows on every install.
 *
 * Squashed 2026-06-02: the create migration
 * (`2026_04_21_190012_create_ai_keys_table.php`) was deleted from the
 * migration history alongside this drop so fresh installs never spin up the
 * table. `down()` re-creates it for the rare case of rolling back this one
 * specifically.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::dropIfExists('ai_keys');
    }

    public function down(): void
    {
        Schema::create('ai_keys', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->nullable()->constrained('tenants')->cascadeOnDelete();
            $table->string('provider', 32);
            $table->text('encrypted_key');
            $table->timestamps();

            $table->unique(['tenant_id', 'provider']);
        });
    }
};
