<?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
    {
        // Platform-level payments (clinic → MediBook), distinct from clinic
        // → patient payments in M4. Drives MRR and the financial dashboard.
        Schema::create('subscription_payments', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->foreignId('subscription_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('plan_id')->constrained()->restrictOnDelete();
            $table->string('gateway', 20);                 // paddle|zaincash|superqi
            $table->unsignedBigInteger('amount');          // minor units of currency
            $table->char('currency', 3)->default('IQD');   // IQD or USD
            $table->string('status', 20)->default('pending'); // pending|approved|rejected|info_requested
            $table->unsignedSmallInteger('period_months')->default(1);
            $table->date('covers_from')->nullable();
            $table->date('covers_until')->nullable();
            // Manual proof (Zain Cash / SuperQi)
            $table->string('proof_path')->nullable();
            $table->string('proof_reference', 120)->nullable();
            // Paddle
            $table->string('gateway_txn_id', 190)->nullable();
            $table->timestamps();
            $table->index(['tenant_id', 'status']);
            $table->index(['status', 'created_at']);
        });
    }

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