<?php

/**
 * Exam Format Profile — schema self-heal.
 *
 * Deliberately standalone (no requires) so it can be safely included from
 * either config world in this codebase: ajax scripts that load the root
 * edutrack/config.php (for checkLicense()/EXAM_UPLOAD_DIR) and the
 * generation pipeline that loads exams_generator/config.php via exam_utils.php.
 * Both define their own getDBConnection(); requiring both in one request is
 * a fatal redeclare, so this file must never require either.
 */

/**
 * Ensure the exam_format_profiles tables exist. Self-heals installs that have
 * not run database/migrations/exam_format_profiles.sql yet. Idempotent and
 * guarded so schema DDL never throws into the request.
 */
function ensureExamFormatProfileTables($conn) {
    static $ensured = false;
    if ($ensured) {
        return;
    }
    if (!$conn instanceof mysqli) {
        return;
    }
    mysqli_report(MYSQLI_REPORT_OFF);
    $conn->query("CREATE TABLE IF NOT EXISTS `exam_format_profiles` (
        `id`                 INT AUTO_INCREMENT PRIMARY KEY,
        `subject_id`         INT          NOT NULL,
        `class_name`         VARCHAR(100) NOT NULL,
        `subject_name`       VARCHAR(150) NULL,
        `source_type`        ENUM('image','pdf','word','text') NOT NULL DEFAULT 'text',
        `original_filename`  VARCHAR(255) NULL,
        `stored_filename`    VARCHAR(255) NULL,
        `file_size`          INT          NOT NULL DEFAULT 0,
        `raw_extracted_text` LONGTEXT     NULL,
        `format_spec_json`   LONGTEXT     NULL,
        `summary_text`       TEXT         NULL,
        `status`             ENUM('processing','ready','failed') NOT NULL DEFAULT 'processing',
        `status_note`        VARCHAR(255) NULL,
        `uploaded_by`        INT          NULL,
        `created_at`         TIMESTAMP    DEFAULT CURRENT_TIMESTAMP,
        `updated_at`         TIMESTAMP    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        UNIQUE KEY `uq_format_profile_subject_class` (`subject_id`, `class_name`),
        KEY `idx_format_profile_status` (`status`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");
    $conn->query("CREATE TABLE IF NOT EXISTS `exam_format_profile_versions` (
        `id`                 INT AUTO_INCREMENT PRIMARY KEY,
        `profile_id`         INT          NOT NULL,
        `subject_id`         INT          NOT NULL,
        `class_name`         VARCHAR(100) NOT NULL,
        `source_type`        ENUM('image','pdf','word','text') NOT NULL DEFAULT 'text',
        `original_filename`  VARCHAR(255) NULL,
        `stored_filename`    VARCHAR(255) NULL,
        `raw_extracted_text` LONGTEXT     NULL,
        `format_spec_json`   LONGTEXT     NULL,
        `summary_text`       TEXT         NULL,
        `status`             VARCHAR(20)  NULL,
        `archived_by`        INT          NULL,
        `created_at`         TIMESTAMP    DEFAULT CURRENT_TIMESTAMP,
        KEY `idx_format_profile_versions_profile` (`profile_id`, `id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci");
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $ensured = true;
}
