<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('payments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('provider', 50)->default('razorpay')->index();
            $table->string('razorpay_order_id')->unique();
            $table->string('razorpay_payment_id')->nullable()->unique();
            $table->string('razorpay_signature')->nullable();
            $table->unsignedInteger('amount_paise');
            $table->string('currency', 3)->default('INR')->index();
            $table->string('status', 50)->default('created')->index();
            $table->json('order_payload')->nullable();
            $table->json('payment_payload')->nullable();
            $table->json('webhook_payload')->nullable();
            $table->timestamp('verified_at')->nullable()->index();
            $table->timestamp('failed_at')->nullable()->index();
            $table->timestamp('unlocked_at')->nullable()->index();
            $table->timestamps();

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

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('payments');
    }
};
