<?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('user_wallets', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('currency', 10)->default('NGN');
            $table->decimal('balance', 14, 2)->default(0);
            $table->string('status', 30)->default('active')->index();
            $table->string('virtual_account_provider')->nullable()->index();
            $table->string('virtual_account_bank_name')->nullable();
            $table->string('virtual_account_bank_code')->nullable();
            $table->string('virtual_account_number')->nullable()->index();
            $table->string('virtual_account_name')->nullable();
            $table->string('virtual_account_reference')->nullable()->index();
            $table->string('virtual_account_customer_code')->nullable();
            $table->string('virtual_account_status')->nullable()->index();
            $table->timestamp('virtual_account_assigned_at')->nullable();
            $table->json('virtual_account_metadata')->nullable();
            $table->timestamps();

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

        Schema::create('user_wallet_transactions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_wallet_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('initiated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->string('type', 20)->index();
            $table->decimal('amount', 14, 2);
            $table->decimal('fee', 14, 2)->default(0);
            $table->decimal('balance_before', 14, 2)->default(0);
            $table->decimal('balance_after', 14, 2)->default(0);
            $table->string('reference')->unique();
            $table->string('method', 80)->nullable()->index();
            $table->string('status', 30)->default('posted')->index();
            $table->string('provider_reference')->nullable()->index();
            $table->text('description')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamp('posted_at')->nullable()->index();
            $table->timestamps();

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

        Schema::table('bill_payments', function (Blueprint $table): void {
            $table->foreignId('user_wallet_id')->nullable()->after('savings_transaction_id')->constrained('user_wallets')->nullOnDelete();
            $table->foreignId('wallet_transaction_id')->nullable()->after('user_wallet_id')->constrained('user_wallet_transactions')->nullOnDelete();
            $table->index(['tenant_id', 'user_wallet_id', 'status']);
        });
    }

    public function down(): void
    {
        Schema::table('bill_payments', function (Blueprint $table): void {
            $table->dropIndex(['tenant_id', 'user_wallet_id', 'status']);
            $table->dropConstrainedForeignId('wallet_transaction_id');
            $table->dropConstrainedForeignId('user_wallet_id');
        });

        Schema::dropIfExists('user_wallet_transactions');
        Schema::dropIfExists('user_wallets');
    }
};
