<?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('vtu_wallets', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('currency', 10)->default('NGN');
            $table->decimal('balance', 14, 2)->default(0);
            $table->decimal('total_credited', 14, 2)->default(0);
            $table->decimal('total_used', 14, 2)->default(0);
            $table->decimal('total_refunded', 14, 2)->default(0);
            $table->timestamps();

            $table->unique('tenant_id');
            $table->index(['tenant_id', 'balance']);
        });

        Schema::create('vtu_wallet_topups', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->decimal('amount', 14, 2);
            $table->string('currency', 10)->default('NGN');
            $table->string('payment_channel')->default('manual')->index();
            $table->string('status')->default('pending')->index();
            $table->string('reference')->unique();
            $table->string('gateway_reference')->nullable()->index();
            $table->string('proof_path')->nullable();
            $table->text('notes')->nullable();
            $table->text('rejected_reason')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('processed_at')->nullable();
            $table->timestamps();

            $table->index(['tenant_id', 'status', 'created_at']);
        });

        Schema::create('vtu_wallet_transactions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->foreignId('vtu_wallet_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('vtu_wallet_topup_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('bill_payment_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->string('type')->index();
            $table->string('direction')->index();
            $table->decimal('amount', 14, 2);
            $table->decimal('balance_before', 14, 2)->default(0);
            $table->decimal('balance_after', 14, 2)->default(0);
            $table->string('reference')->unique();
            $table->string('description')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamps();

            $table->index(['tenant_id', 'type', 'created_at']);
            $table->index(['tenant_id', 'direction', 'created_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('vtu_wallet_transactions');
        Schema::dropIfExists('vtu_wallet_topups');
        Schema::dropIfExists('vtu_wallets');
    }
};
