<?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
    {
        // 1. COURSES TABLE
        Schema::table('lms_courses', function (Blueprint $table) {
            // New JSON column (Using longText for 4GB storage safety)
            $table->longText('description_json')->nullable()->after('description');
            
            // CRITICAL: Upgrade existing HTML column from TEXT (64KB) to LONGTEXT (4GB)
            $table->longText('description')->nullable()->change();
        });

        // 2. SETTINGS TABLE
        Schema::table('lms_course_settings', function (Blueprint $table) {
            $table->longText('instructions_json')->nullable()->after('instructions');
            $table->longText('completion_message_json')->nullable()->after('completion_message');
            
            // Upgrade existing HTML columns
            $table->longText('instructions')->nullable()->change();
            $table->longText('completion_message')->nullable()->change();
        });

        // 3. SECTIONS TABLE
        Schema::table('lms_course_sections', function (Blueprint $table) {
            $table->longText('description_json')->nullable()->after('description');
            
            // Upgrade existing HTML column
            $table->longText('description')->nullable()->change();
        });

        // 4. LESSONS TABLE (Most critical - course content is usually huge)
        Schema::table('lms_course_lessons', function (Blueprint $table) {
            $table->longText('content_json')->nullable()->after('content');
            
            // Upgrade existing HTML column
            $table->longText('content')->nullable()->change();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('lms_courses', function (Blueprint $table) {
            $table->dropColumn('description_json');
            // We usually don't downgrade the text column size back to small to avoid data loss on rollback
        });

        Schema::table('lms_course_settings', function (Blueprint $table) {
            $table->dropColumn(['instructions_json', 'completion_message_json']);
        });

        Schema::table('lms_course_sections', function (Blueprint $table) {
            $table->dropColumn('description_json');
        });

        Schema::table('lms_course_lessons', function (Blueprint $table) {
            $table->dropColumn('content_json');
        });
    }
};