<?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 machine_payments table for tracking MTN MoMo payments
     * for digital health machine marketing listings.
     *
     * Requirements: 3.3, 3.4, 9.2, 10.1, 10.2
     */
    public function up(): void
    {
        Schema::create('machine_payments', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Foreign key to digital_machines
            $table->foreignId('digital_machine_id')
                ->nullable()
                ->constrained('digital_machines')
                ->onDelete('set null')
                ->comment('Associated machine listing');

            // Foreign key to user (payer)
            $table->foreignId('user_id')
                ->nullable()
                ->constrained('users')
                ->onDelete('set null')
                ->comment('User who made the payment');

            // Payment Amount
            $table->decimal('amount', 15, 2)->comment('Payment amount');
            $table->string('currency', 10)->default('SSP')->comment('Currency code (SSP or USD)');

            // Payment Plan
            $table->enum('payment_frequency', [
                'one_time',
                'monthly',
                'quarterly',
                'biannual',
                'yearly'
            ])->default('yearly')->comment('Subscription frequency');

            // Payment Method
            $table->string('payment_method', 50)->default('mtn_momo')->comment('Payment method used');

            // MTN MoMo Transaction Details
            $table->string('transaction_id', 191)->nullable()->comment('MTN MoMo transaction ID');
            $table->string('reference', 191)->unique()->comment('Internal payment reference');
            $table->string('phone_number', 20)->nullable()->comment('Phone number used for payment');

            // Payment Messages
            $table->string('payer_message', 255)->nullable()->comment('Message shown to payer');
            $table->string('payee_note', 255)->nullable()->comment('Note from payee');

            // Payment Status
            $table->enum('status', [
                'pending',
                'processing',
                'successful',
                'failed',
                'timeout',
                'cancelled',
                'refunded'
            ])->default('pending')->comment('Payment status');

            // Status Details
            $table->text('status_message')->nullable()->comment('Status description or error message');
            $table->string('failure_reason', 255)->nullable()->comment('Reason for failure');

            // Timestamps
            $table->timestamp('initiated_at')->nullable()->comment('When payment was initiated');
            $table->timestamp('paid_at')->nullable()->comment('When payment was confirmed');
            $table->timestamp('expires_at')->nullable()->comment('Payment link expiration');

            // Coverage Period
            $table->timestamp('coverage_start')->nullable()->comment('Start of subscription period');
            $table->timestamp('coverage_end')->nullable()->comment('End of subscription period');

            // Additional Data
            $table->json('metadata')->nullable()->comment('Additional payment data');

            // Timestamps
            $table->timestamps();

            // Indexes
            $table->index('digital_machine_id');
            $table->index('user_id');
            $table->index('transaction_id');
            $table->index('reference');
            $table->index('status');
            $table->index('payment_frequency');
            $table->index('created_at');
            $table->index(['user_id', 'status']);
            $table->index(['digital_machine_id', 'status']);
        });
    }

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