<?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(); // bigint unsigned AUTO_INCREMENT

            $table->string('payment_number'); // Format: PAY-YYYYMMDD-XXXX
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('subscription_id')->nullable();

            $table->decimal('amount', 10, 2);
            $table->decimal('tax_amount', 10, 2)->default(0.00);
            $table->decimal('total_amount', 10, 2);

            $table->string('currency')->default('USD');

            $table->enum('payment_method', ['card', 'paypal', 'bank_transfer', 'cash'])->nullable();
            $table->enum('payment_type', ['subscription', 'featured_listing', 'manual'])->default('subscription');

            $table->string('status')->default('0');

            $table->string('transaction_id')->nullable();

            $table->timestamp('paid_at')->nullable();
            $table->timestamp('billing_period_start')->nullable();
            $table->timestamp('billing_period_end')->nullable();

            $table->string('receipt_url')->nullable();
            $table->timestamp('receipt_sent_at')->nullable();

            $table->decimal('refunded_amount', 10, 2)->default(0.00);
            $table->timestamp('refunded_at')->nullable();
            $table->text('refund_reason')->nullable();

            $table->string('stripe_payment_intent_id')->nullable();
            $table->string('stripe_charge_id')->nullable();
            $table->string('stripe_invoice_id')->nullable();
            $table->string('stripe_refund_id')->nullable();

            $table->longText('metadata')->nullable();
            $table->text('notes')->nullable();

            $table->timestamps(); // created_at, updated_at

            // Optional indexes
            $table->index('payment_number');
            $table->index('user_id');
            $table->index('subscription_id');
            $table->index('transaction_id');
        });
    }

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