<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * Alters the self_paced_enrollments table to add:
     * - Payment method change from mtn_momo to paystack
     * - Dual-fee tracking (registration_fee_paid, application_fee_paid)
     * - Currency tracking
     * - Certificate fee payment flag
     * - Exam unlock timestamp
     * - Exam exhaustion flag
     * - Re-sit attempt counter
     * - Content access expiry timestamp
     * - Status enum update to include pending_verification and deregistered
     *
     * Requirements: 1.3, 1.9, 2.5, 5.1, 5.5, 6.1, 8.10, 18.13
     */
    public function up(): void
    {
        Schema::table('self_paced_enrollments', function (Blueprint $table) {
            // Update payment_method default value
            $table->string('payment_method', 50)->default('paystack')->change();

            // Add currency tracking
            $table->string('currency', 10)
                ->default('KES')
                ->after('amount_paid')
                ->comment('Currency code for this enrollment (KES, USD, etc.)');

            // Dual-fee tracking
            $table->decimal('registration_fee_paid', 12, 2)
                ->default(0.00)
                ->after('currency')
                ->comment('Registration fee amount paid');

            $table->decimal('application_fee_paid', 12, 2)
                ->default(0.00)
                ->after('registration_fee_paid')
                ->comment('Application fee amount paid');

            // Certificate fee payment flag
            $table->boolean('certificate_fee_paid')
                ->default(false)
                ->after('application_fee_paid')
                ->comment('Whether certificate fee has been paid');

            // Exam unlock tracking
            $table->timestamp('exam_unlocked_at')
                ->nullable()
                ->after('certificate_fee_paid')
                ->comment('Timestamp when exam was unlocked for this learner');

            // Exam exhaustion flag
            $table->boolean('exam_exhausted')
                ->default(false)
                ->after('exam_unlocked_at')
                ->comment('Whether learner has exhausted all exam attempts without passing');

            // Re-sit attempt counter
            $table->unsignedInteger('resit_attempt_count')
                ->default(0)
                ->after('exam_exhausted')
                ->comment('Number of times learner has paid re-sit fee');

            // Content access expiry
            $table->timestamp('content_access_expires_at')
                ->nullable()
                ->after('resit_attempt_count')
                ->comment('30 days after completion; enrollment deregistered when this passes');

            // Add new status values: pending_verification, deregistered
            // Need to drop and recreate the enum
            $table->dropColumn('status');
        });

        Schema::table('self_paced_enrollments', function (Blueprint $table) {
            $table->enum('status', [
                'pending_payment',
                'pending_verification',
                'active',
                'completed',
                'payment_failed',
                'cancelled',
                'deregistered',
            ])->default('pending_payment')->after('course_id');
        });

        // Add indexes for performance
        Schema::table('self_paced_enrollments', function (Blueprint $table) {
            $table->index('status', 'idx_sp_enr_status');
            $table->index('content_access_expires_at', 'idx_sp_enr_expires');
        });
    }

    public function down(): void
    {
        Schema::table('self_paced_enrollments', function (Blueprint $table) {
            // Restore original enum first
            $table->dropIndex('idx_sp_enr_status');
            $table->dropIndex('idx_sp_enr_expires');
            $table->dropColumn('status');
        });

        Schema::table('self_paced_enrollments', function (Blueprint $table) {
            $table->enum('status', [
                'pending_payment',
                'active',
                'completed',
                'payment_failed',
                'cancelled',
            ])->default('pending_payment')->after('course_id');

            // Revert payment_method default
            $table->string('payment_method', 50)->default('mtn_momo')->change();

            $table->dropColumn([
                'currency',
                'registration_fee_paid',
                'application_fee_paid',
                'certificate_fee_paid',
                'exam_unlocked_at',
                'exam_exhausted',
                'resit_attempt_count',
                'content_access_expires_at',
            ]);
        });
    }
};
