<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('addon_products', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('description')->nullable();
            $table->decimal('price', 10, 2);
            $table->string('currency', 3)->default('USD');
            $table->string('type')->default('addon'); // addon, module, feature
            $table->boolean('is_active')->default(true);
            $table->string('stripe_price_id')->nullable();
            $table->string('paypal_item_id')->nullable();
            $table->json('metadata')->nullable(); // extra config
            $table->timestamps();
        });

        Schema::create('addon_purchases', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('addon_product_id')->constrained()->onDelete('cascade');
            $table->string('provider'); // stripe|paypal|crypto|iap
            $table->string('provider_reference')->nullable(); // session_id / order_id / txhash
            $table->string('status')->default('pending'); // pending|completed|failed
            $table->decimal('amount_paid', 10, 2)->nullable();
            $table->string('currency', 3)->default('USD');
            $table->json('meta')->nullable();
            $table->timestamps();

            $table->index(['user_id', 'addon_product_id']);
        });
    }

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