<?php

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

/**
 * Per-provider customer mapping. Kept as its own table rather than a
 * `stripe_customer_id` column on `users` so the schema stays neutral as
 * additional providers (Razorpay 5C, Paddle, Lemon Squeezy) come online —
 * adding a provider is a new row shape, never a new column on `users`.
 *
 * Webhook lookups dominate read traffic (every `customer.subscription.*`
 * event resolves the user via the provider's customer id), so the
 * `(provider_key, provider_customer_id)` index is required, not optional.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('provider_customers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('provider_key', 32);
            $table->string('provider_customer_id', 191);
            $table->timestamps();

            $table->unique(['user_id', 'provider_key']);
            $table->index(['provider_key', 'provider_customer_id']);
        });
    }

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