<?php

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

/**
 * Drop the legacy `plan_provider_links` table — the canonical pricing
 * surface is now `plan_prices`. Migration 000012 has already copied the
 * data over.
 *
 * `down()` recreates the original schema (matching
 * `2026_04_29_000004_create_billing_plan_provider_links_table` +
 * `2026_05_04_000002_add_display_amount_to_plan_provider_links`) so a
 * full rollback chain remains coherent. Data isn't restored on rollback —
 * the dropped table comes back empty; a follow-up migration would have
 * to re-populate from `plan_prices`.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::dropIfExists('plan_provider_links');
    }

    public function down(): void
    {
        Schema::create('plan_provider_links', function (Blueprint $table) {
            $table->id();
            $table->foreignId('plan_id')->constrained('plans')->cascadeOnDelete();
            $table->string('provider_key', 32);
            $table->string('period', 8);
            $table->string('provider_price_id', 191);
            $table->unsignedInteger('display_amount_minor')->nullable();
            $table->string('display_currency', 3)->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->unique(['plan_id', 'provider_key', 'period']);
            $table->index(['provider_key', 'period']);
        });
    }
};
