<?php
/**
 * Plugin Name: uLesson Learning Core Engine
 * Description: Manages the custom database tables, curriculum logic, and extended backend frameworks for the Luminous Learning LMS.
 * Version:     1.1.3
 * Author:      KushLab
 * License:     GPL2
 */

// Exit if accessed directly to prevent malicious execution
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Initialize All Custom Database Tables for Luminous Learning System
 */
function luminous_learning_init_database_tables() {
    global $wpdb;
    $charset_collate = $wpdb->get_charset_collate();

    // 1. Structural Blueprint Framework Tables
    $table_curriculum      = $wpdb->prefix . 'lms_academy_curriculum';
    $table_topics          = $wpdb->prefix . 'lms_curriculum_topics';
    $table_lookup          = $wpdb->prefix . 'lms_course_curriculum_lookup';
    $table_teacher_courses = $wpdb->prefix . 'lms_teacher_courses';

    // 2. Content Resource & Custom Layout Tables
    $table_teachers_resources = $wpdb->prefix . 'lms_teachers_resources';
    $table_custom             = $wpdb->prefix . 'lms_custom_topics';

    // 3. Evaluation & Assessment Tables
    $table_exercise        = $wpdb->prefix . 'lms_course_exercise';

    // Include the core upgrade engine file required to parse dbDelta strings
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

    // Deploy Base Structure Workflow Tables
    $sql_base = "CREATE TABLE $table_curriculum (
        id BIGINT(20) NOT NULL AUTO_INCREMENT,
        class_group VARCHAR(50) NOT NULL,    
        class_level VARCHAR(50) NOT NULL,    
        term VARCHAR(50) NOT NULL,           
        subject_name VARCHAR(255) NOT NULL,  
        PRIMARY KEY (id)
    ) $charset_collate;

    CREATE TABLE $table_topics (
        id BIGINT(20) NOT NULL AUTO_INCREMENT,
        curriculum_id BIGINT(20) NOT NULL,   
        topic_title VARCHAR(255) NOT NULL,   
        parent_topic_id BIGINT(20) DEFAULT 0,
        sort_order INT(11) DEFAULT 0,
        PRIMARY KEY (id)
    ) $charset_collate;

    CREATE TABLE $table_lookup (
        id BIGINT(20) NOT NULL AUTO_INCREMENT,
        course_id BIGINT(20) NOT NULL,       
        topic_id BIGINT(20) NOT NULL,        
        teacher_id BIGINT(20) NOT NULL,      
        PRIMARY KEY (id),
        UNIQUE KEY course_topic (course_id, topic_id)
    ) $charset_collate;

    CREATE TABLE $table_teacher_courses (
        id BIGINT(20) NOT NULL AUTO_INCREMENT,
        teacher_id BIGINT(20) NOT NULL,
        curriculum_id BIGINT(20) NOT NULL,
        course_title VARCHAR(255) NOT NULL,
        course_description TEXT NOT NULL,
        course_type VARCHAR(50) DEFAULT 'academy',
        creation_status VARCHAR(50) DEFAULT 'step_1',
        created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (id)
    ) $charset_collate;";

    dbDelta( $sql_base );

    // Deploy Unified Teachers Resources Table mapping directly to Course Meta tracks
    $sql_teachers_resources = "CREATE TABLE $table_teachers_resources (
        id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
        course_id bigint(20) unsigned NOT NULL,
        curriculum_id bigint(20) unsigned DEFAULT 0,
        course_type varchar(50) NOT NULL DEFAULT 'Academy',
        topic_id bigint(20) unsigned NOT NULL,
        topic_type varchar(50) NOT NULL DEFAULT 'preset',
        resource_title varchar(255) NOT NULL,
        resource_type varchar(50) NOT NULL,
        resource_path text NOT NULL,
        created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
        PRIMARY KEY  (id),
        KEY course_id (course_id),
        KEY curriculum_id (curriculum_id),
        KEY topic_id (topic_id)
    ) $charset_collate;";

    $sql_custom = "CREATE TABLE $table_custom (
        id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
        course_id bigint(20) unsigned NOT NULL,
        topic_title varchar(255) NOT NULL,
        parent_topic_id int(11) NOT NULL DEFAULT 0,
        sort_order int(11) NOT NULL DEFAULT 1,
        created_at datetime NOT NULL,
        PRIMARY KEY  (id),
        KEY course_id (course_id),
        KEY parent_topic_id (parent_topic_id)
    ) $charset_collate;";

    dbDelta( $sql_teachers_resources );
    dbDelta( $sql_custom );

    // Deploy Course Evaluation and Quiz Assessment schema frameworks
    $sql_exercise = "CREATE TABLE $table_exercise (
        id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
        course_id bigint(20) unsigned NOT NULL,
        topic_id int(11) NOT NULL,
        exercise_type varchar(50) NOT NULL DEFAULT 'quiz',
        questions text NOT NULL,
        option_1 text NOT NULL,
        option_2 text NOT NULL,
        option_3 text DEFAULT NULL,
        option_4 text DEFAULT NULL,
        answer tinyint(3) unsigned NOT NULL COMMENT 'Accepts numbers 1, 2, 3, or 4 pointing to the correct choice',
        created_at datetime NOT NULL,
        PRIMARY KEY  (id),
        KEY course_id (course_id),
        KEY topic_id (topic_id)
    ) $charset_collate;";

    dbDelta( $sql_exercise );
}

/**
 * Register Activation Hook
 * This ensures all structured database containers are generated exactly at the moment the plugin is turned on.
 */
register_activation_hook( __FILE__, 'luminous_learning_init_database_tables' );