<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        if (!Schema::hasTable('course_progress')) {
            Schema::create('course_progress', function (Blueprint $table) {
                $table->id();
                $table->foreignId('enrollment_id')->constrained('course_enrollments')->onDelete('cascade');
                $table->foreignId('course_id')->constrained('courses')->onDelete('cascade');
                $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
                $table->unsignedBigInteger('lesson_id')->nullable(); // Foreign key will be added in separate migration
                $table->unsignedInteger('progress_percentage')->default(0);
                $table->unsignedInteger('lessons_completed')->default(0);
                $table->unsignedInteger('time_spent_minutes')->default(0);
                $table->timestamp('last_accessed_at')->nullable();
                $table->timestamp('completed_at')->nullable();
                $table->boolean('certificate_eligible')->default(false);
                $table->timestamps();
                
                $table->index(['user_id', 'course_id']);
                $table->index(['enrollment_id', 'lesson_id']);
            });
        }
    }

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