<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Payment transactions table for all membership-related payments
        Schema::create('membership_transactions', function (Blueprint $table) {
            $table->id();
            $table->string('transaction_reference', 50)->unique();
            $table->string('external_reference', 100)->nullable();
            
            // Transaction type
            $table->enum('type', ['subscription', 'contribution', 'loan_repayment', 'penalty', 'refund', 'disbursement', 'renewal_fee']);
            
            // Related entities
            $table->foreignId('member_id')->nullable()->constrained()->onDelete('set null');
            $table->foreignId('membership_application_id')->nullable()->constrained()->onDelete('set null');
            $table->foreignId('contribution_id')->nullable()->constrained()->onDelete('set null');
            $table->foreignId('loan_application_id')->nullable()->constrained()->onDelete('set null');
            $table->foreignId('loan_repayment_id')->nullable()->constrained()->onDelete('set null');
            
            // Amount details
            $table->decimal('amount', 15, 2);
            $table->enum('currency', ['USD', 'SSP', 'KES', 'UGX'])->default('SSP');
            $table->decimal('fee_amount', 15, 2)->default(0);
            $table->decimal('net_amount', 15, 2);
            
            // Payment method
            $table->string('payment_method', 50)->default('mtn_momo');
            $table->string('payment_phone', 20)->nullable();
            $table->string('payment_account', 100)->nullable();
            
            // Status
            $table->enum('status', ['pending', 'processing', 'completed', 'failed', 'cancelled', 'refunded'])->default('pending');
            
            // Timestamps
            $table->timestamp('initiated_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->timestamp('failed_at')->nullable();
            
            // Response data
            $table->json('request_data')->nullable();
            $table->json('response_data')->nullable();
            $table->text('error_message')->nullable();
            $table->string('error_code', 50)->nullable();
            
            // Audit
            $table->foreignId('initiated_by')->nullable()->constrained('users')->onDelete('set null');
            $table->foreignId('completed_by')->nullable()->constrained('users')->onDelete('set null');
            
            $table->text('notes')->nullable();
            $table->timestamps();
            
            $table->index('transaction_reference');
            $table->index('external_reference');
            $table->index('type');
            $table->index('status');
            $table->index('member_id');
            $table->index('created_at');
        });

        // Member activity log
        Schema::create('member_activity_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('member_id')->nullable()->constrained()->onDelete('set null');
            $table->foreignId('user_id')->nullable()->constrained('users')->onDelete('set null');
            $table->string('activity_type', 100);
            $table->string('description');
            $table->json('metadata')->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->string('user_agent')->nullable();
            $table->timestamps();
            
            $table->index('member_id');
            $table->index('user_id');
            $table->index('activity_type');
            $table->index('created_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('member_activity_logs');
        Schema::dropIfExists('membership_transactions');
    }
};
