<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * Creates the mentorship_payments table for payment transaction tracking.
     * Integrates with MTN MoMo PaymentService.
     */
    public function up(): void
    {
        Schema::create('mentorship_payments', function (Blueprint $table) {
            $table->id();
            
            // Application relationship
            $table->foreignId('application_id')
                ->constrained('mentorship_applications')
                ->cascadeOnDelete();
            
            // Programme reference
            $table->foreignId('programme_id')
                ->constrained('mentorship_programmes')
                ->cascadeOnDelete();
            
            // User relationship (if payment is for existing user)
            $table->foreignId('user_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            
            // Payment details
            $table->decimal('amount', 12, 2);
            $table->string('currency', 3)->default('UGX');
            $table->string('payment_type', 50)->default('programme_fee');
            $table->text('description')->nullable();
            
            // Payment method
            $table->string('payment_method', 50)->default('mtn_momo');
            $table->string('phone_number', 50)->nullable();
            
            // Transaction details
            $table->string('reference', 191)->unique();
            $table->string('transaction_id', 191)->nullable()->index();
            $table->string('external_reference', 191)->nullable()->comment('Reference from payment provider');
            
            // Status tracking
            $table->enum('status', [
                'pending',
                'processing',
                'successful',
                'failed',
                'cancelled',
                'refunded',
                'timeout'
            ])->default('pending')->index();
            
            // MTN MoMo specific fields
            $table->string('momo_transaction_id', 191)->nullable();
            $table->string('momo_reference', 191)->nullable();
            $table->string('momo_financial_transaction_id', 191)->nullable();
            
            // Payment timestamps
            $table->timestamp('initiated_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->timestamp('failed_at')->nullable();
            $table->timestamp('refunded_at')->nullable();
            
            // Response data
            $table->json('request_data')->nullable()->comment('Original request payload');
            $table->json('response_data')->nullable()->comment('Response from payment provider');
            $table->json('callback_data')->nullable()->comment('Webhook callback data');
            
            // Error handling
            $table->string('error_code', 50)->nullable();
            $table->text('error_message')->nullable();
            
            // Retry tracking
            $table->integer('retry_count')->default(0);
            $table->timestamp('last_retry_at')->nullable();
            $table->text('retry_reason')->nullable();
            
            // Refund information
            $table->decimal('refund_amount', 12, 2)->nullable();
            $table->string('refund_reference', 191)->nullable();
            $table->text('refund_reason')->nullable();
            $table->foreignId('refunded_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            
            // Receipt
            $table->string('receipt_number', 191)->nullable();
            $table->timestamp('receipt_sent_at')->nullable();
            
            // Admin notes
            $table->text('admin_notes')->nullable();
            
            // IP and user agent tracking
            $table->string('ip_address', 45)->nullable();
            $table->string('user_agent', 500)->nullable();
            
            // Timestamps
            $table->timestamps();
            
            // Indexes
            $table->index(['application_id', 'status']);
            $table->index(['programme_id', 'status']);
            $table->index(['status', 'created_at']);
        });
    }

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