<?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_courses table to add:
     * - Dual-fee system (registration_fee + application_fee replacing single fee_amount)
     * - Payment mode configuration (online/cash/both)
     * - Certificate fee and collection mode
     * - Course book upload and purchase fee
     * - Exam failure/re-sit fee
     *
     * Requirements: 1.3, 2.1, 8.1, 9.1, 10.1, 17.1, 18.1
     */
    public function up(): void
    {
        Schema::table('self_paced_courses', function (Blueprint $table) {
            // Payment Mode
            $table->enum('payment_mode', ['online', 'cash', 'both'])
                ->default('online')
                ->after('fee_amount')
                ->comment('Payment acceptance mode: online (Paystack only), cash (manual proof upload only), or both');

            // Dual Fee System
            $table->decimal('registration_fee', 12, 2)
                ->default(0.00)
                ->after('payment_mode')
                ->comment('Registration fee charged at enrollment');

            $table->decimal('application_fee', 12, 2)
                ->default(0.00)
                ->after('registration_fee')
                ->comment('Application fee charged at enrollment');

            // Certificate Fee and Collection Mode
            $table->decimal('certificate_fee', 12, 2)
                ->default(0.00)
                ->after('application_fee')
                ->comment('Fee to download certificate after passing');

            $table->enum('certificate_collection_mode', ['self_download', 'hardcopy'])
                ->default('self_download')
                ->after('certificate_fee')
                ->comment('Certificate collection mode: self_download (instant download) or hardcopy (collect from institution)');

            // Course Book
            $table->string('book_path', 500)
                ->nullable()
                ->after('certificate_collection_mode')
                ->comment('Storage path to course book PDF');

            $table->string('book_file_name', 255)
                ->nullable()
                ->after('book_path')
                ->comment('Original file name of course book');

            $table->unsignedInteger('book_file_size')
                ->nullable()
                ->after('book_file_name')
                ->comment('File size in bytes');

            $table->decimal('book_fee', 12, 2)
                ->default(0.00)
                ->after('book_file_size')
                ->comment('Purchase fee for course book');

            // Exam Failure/Re-sit Fee
            $table->decimal('exam_failure_fee', 12, 2)
                ->default(0.00)
                ->after('book_fee')
                ->comment('Fee to regain access after exhausting exam attempts');

            $table->unsignedInteger('max_exam_attempts')
                ->default(3)
                ->after('exam_failure_fee')
                ->comment('Maximum exam attempts allowed before requiring re-sit fee');
        });
    }

    public function down(): void
    {
        Schema::table('self_paced_courses', function (Blueprint $table) {
            $table->dropColumn([
                'payment_mode',
                'registration_fee',
                'application_fee',
                'certificate_fee',
                'certificate_collection_mode',
                'book_path',
                'book_file_name',
                'book_file_size',
                'book_fee',
                'exam_failure_fee',
                'max_exam_attempts',
            ]);
        });
    }
};
