<?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('subscription_payments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('subscription_id')->constrained()->cascadeOnDelete();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();

            $table->string('reference')->unique();   // RES-2026-000001
            $table->string('driver', 50)->default('manual');  // wompi, stripe, manual...
            $table->string('external_id')->nullable();        // id de la pasarela
            $table->enum('status', ['pending', 'paid', 'failed', 'refunded', 'cancelled'])
                  ->default('pending');

            $table->decimal('amount', 12, 2);
            $table->string('currency', 3)->default('COP');
            $table->timestamp('paid_at')->nullable();
            $table->timestamp('period_start')->nullable();
            $table->timestamp('period_end')->nullable();

            $table->string('receipt_path')->nullable();      // comprobante manual
            $table->json('gateway_response')->nullable();
            $table->text('notes')->nullable();

            $table->timestamps();

            $table->index(['company_id', 'status']);
            $table->index('paid_at');
        });
    }

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