<?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 self_paced_courses table — the core entity of the portal.
     * Stores course content metadata, fee, duration, exam settings, and status.
     */
    public function up(): void
    {
        Schema::create('self_paced_courses', function (Blueprint $table) {
            $table->id();

            // Basic Information
            $table->string('title', 191);
            $table->string('slug', 191)->unique();
            $table->text('description');
            $table->text('objectives')->nullable();
            $table->text('prerequisites')->nullable();
            $table->string('featured_image')->nullable();

            // Category and Instructor
            $table->foreignId('category_id')
                ->nullable()
                ->constrained('self_paced_categories')
                ->nullOnDelete();
            $table->foreignId('instructor_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();

            // Fee and Duration
            $table->decimal('fee_amount', 10, 2)->default(0.00);
            $table->string('currency', 10)->default('UGX');
            $table->integer('duration_hours')->default(0);

            // Exam Settings
            $table->integer('passing_score')->default(70);
            $table->integer('exam_attempts_allowed')->default(3);
            $table->integer('exam_time_limit')->default(60)->comment('minutes');
            $table->integer('exam_questions_per_attempt')->default(10);

            // Status
            $table->enum('status', ['draft', 'published', 'archived'])->default('draft');

            // Audit
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();

            $table->softDeletes();
            $table->timestamps();

            $table->index('slug');
            $table->index('status');
            $table->index('category_id');
            $table->index('instructor_id');
            $table->index('created_by');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('self_paced_courses');
    }
};
