<?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('communication_unit_wallets', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('channel', 30);
            $table->unsignedInteger('balance_units')->default(0);
            $table->unsignedInteger('total_credited_units')->default(0);
            $table->unsignedInteger('total_used_units')->default(0);
            $table->decimal('total_member_charges', 14, 2)->default(0);
            $table->timestamps();

            $table->unique(['tenant_id', 'channel'], 'cu_wallet_tenant_channel_unique');
            $table->index(['channel', 'balance_units'], 'cu_wallet_channel_balance_idx');
        });

        Schema::create('communication_unit_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->string('channel', 30)->index();
            $table->unsignedInteger('units');
            $table->decimal('unit_price', 14, 2)->default(0);
            $table->decimal('amount', 14, 2)->default(0);
            $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'], 'cu_topups_tenant_status_idx');
            $table->index(['tenant_id', 'channel', 'status'], 'cu_topups_tenant_channel_status_idx');
        });

        Schema::create('communication_unit_transactions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->foreignId('communication_unit_wallet_id')->nullable();
            $table->foreignId('communication_unit_topup_id')->nullable();
            $table->foreignId('customer_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('savings_transaction_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('notification_campaign_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->string('channel', 30)->index();
            $table->string('type')->index();
            $table->string('direction', 10)->index();
            $table->unsignedInteger('units')->default(0);
            $table->decimal('unit_price', 14, 2)->default(0);
            $table->decimal('amount', 14, 2)->default(0);
            $table->unsignedInteger('balance_before')->default(0);
            $table->unsignedInteger('balance_after')->default(0);
            $table->string('payer_type')->nullable()->index();
            $table->string('reference')->unique();
            $table->text('description')->nullable();
            $table->nullableMorphs('related');
            $table->json('metadata')->nullable();
            $table->timestamps();

            $table->index(['tenant_id', 'channel', 'created_at'], 'cu_tx_tenant_channel_created_idx');
            $table->index(['tenant_id', 'type', 'created_at'], 'cu_tx_tenant_type_created_idx');

            $table->foreign('communication_unit_wallet_id', 'cu_tx_wallet_fk')->references('id')->on('communication_unit_wallets')->nullOnDelete();
            $table->foreign('communication_unit_topup_id', 'cu_tx_topup_fk')->references('id')->on('communication_unit_topups')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('communication_unit_transactions');
        Schema::dropIfExists('communication_unit_topups');
        Schema::dropIfExists('communication_unit_wallets');
    }
};
