Files
saqel/backend/database_schema.sql
T

466 lines
25 KiB
SQL

-- ==============================================================================
-- SAQEL PLATFORM (منصة صَقِل) - PRIMARY DATABASE SCHEMA (MySQL 8.4+)
-- Complete Production Database Definition
-- ==============================================================================
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ------------------------------------------------------------------------------
-- DROP ALL EXISTING TABLES IN REVERSE ORDER TO PREVENT FOREIGN KEY CONFLICTS
-- ------------------------------------------------------------------------------
DROP TABLE IF EXISTS `teacher_reviews`;
DROP TABLE IF EXISTS `teacher_performance_metrics`;
DROP TABLE IF EXISTS `chat_messages`;
DROP TABLE IF EXISTS `student_mastery_analytics`;
DROP TABLE IF EXISTS `student_question_answers`;
DROP TABLE IF EXISTS `exam_attempts`;
DROP TABLE IF EXISTS `question_options`;
DROP TABLE IF EXISTS `questions`;
DROP TABLE IF EXISTS `exams`;
DROP TABLE IF EXISTS `quizzes`;
DROP TABLE IF EXISTS `otp_verifications`;
DROP TABLE IF EXISTS `user_devices`;
DROP TABLE IF EXISTS `voice_notes`;
DROP TABLE IF EXISTS `lesson_progress`;
DROP TABLE IF EXISTS `progress`;
DROP TABLE IF EXISTS `lessons`;
DROP TABLE IF EXISTS `courses`;
DROP TABLE IF EXISTS `subjects`;
DROP TABLE IF EXISTS `teacher_profiles`;
DROP TABLE IF EXISTS `guardian_student`;
DROP TABLE IF EXISTS `users`;
DROP TABLE IF EXISTS `schools`;
-- ------------------------------------------------------------------------------
-- 1. Table: schools (المدارس والجهات الشريكة - B2B مثل الثقافة العسكرية)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `schools` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`uuid` CHAR(36) NOT NULL,
`name` VARCHAR(255) NOT NULL,
`code` VARCHAR(50) NOT NULL UNIQUE,
`type` ENUM('military_culture', 'private', 'public', 'center') NOT NULL DEFAULT 'private',
`director_name` VARCHAR(255) DEFAULT NULL,
`phone` VARCHAR(50) DEFAULT NULL,
`city` VARCHAR(100) DEFAULT 'Amman',
`is_active` TINYINT(1) NOT NULL DEFAULT 1,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_schools_uuid` (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 2. Table: users (المستخدمون الموحدون - طلاب، معلمون، أولياء أمور، إدارة)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `users` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`uuid` CHAR(36) NOT NULL,
`full_name` TEXT NOT NULL,
`phone_number` TEXT NOT NULL,
`phone_hash` VARCHAR(64) NOT NULL,
`password_hash` VARCHAR(255) DEFAULT NULL,
`role` ENUM('student', 'guardian', 'teacher', 'school_admin', 'super_admin') NOT NULL DEFAULT 'student',
`school_id` BIGINT UNSIGNED DEFAULT NULL,
`grade_level` VARCHAR(50) DEFAULT 'tawjihi_2007',
`stream` ENUM('scientific', 'literary', 'vocational', 'general') DEFAULT 'scientific',
`token_version` INT UNSIGNED NOT NULL DEFAULT 1,
`status` ENUM('active', 'pending_otp', 'suspended') NOT NULL DEFAULT 'pending_otp',
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_users_uuid` (`uuid`),
KEY `idx_users_phone_hash` (`phone_hash`),
KEY `idx_users_school_id` (`school_id`),
CONSTRAINT `fk_users_school` FOREIGN KEY (`school_id`) REFERENCES `schools` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 3. Table: guardian_student (ربط أولياء الأمور بالأبناء - N:M)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `guardian_student` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`guardian_id` BIGINT UNSIGNED NOT NULL,
`student_id` BIGINT UNSIGNED NOT NULL,
`relationship_type` ENUM('father', 'mother', 'brother', 'guardian') NOT NULL DEFAULT 'father',
`is_verified` TINYINT(1) NOT NULL DEFAULT 0,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_guardian_student_unique` (`guardian_id`, `student_id`),
KEY `idx_guardian_student_student` (`student_id`),
CONSTRAINT `fk_gs_guardian` FOREIGN KEY (`guardian_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_gs_student` FOREIGN KEY (`student_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 4. Table: teacher_profiles (ملفات المعلمين ونسب الأرباح)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `teacher_profiles` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` BIGINT UNSIGNED NOT NULL,
`bio` TEXT DEFAULT NULL,
`specialization` VARCHAR(150) NOT NULL,
`revenue_share_pct` DECIMAL(5,2) NOT NULL DEFAULT 45.00,
`contract_type` ENUM('exclusive', 'non_exclusive') NOT NULL DEFAULT 'exclusive',
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_teacher_user` (`user_id`),
CONSTRAINT `fk_teacher_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 5. Table: subjects (المواد التعليمية)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `subjects` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(150) NOT NULL,
`code` VARCHAR(50) NOT NULL UNIQUE,
`stream` ENUM('scientific', 'literary', 'common') NOT NULL DEFAULT 'common',
`is_active` TINYINT(1) NOT NULL DEFAULT 1,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 6. Table: courses (الدورات التدريبية)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `courses` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`uuid` CHAR(36) NOT NULL,
`subject_id` BIGINT UNSIGNED NOT NULL,
`teacher_id` BIGINT UNSIGNED NOT NULL,
`title` VARCHAR(255) NOT NULL,
`description` TEXT DEFAULT NULL,
`semester` ENUM('first', 'second', 'full_year', 'intensive') NOT NULL DEFAULT 'first',
`price_jod` DECIMAL(8,2) NOT NULL DEFAULT 35.00,
`thumbnail_url` VARCHAR(500) DEFAULT NULL,
`is_published` TINYINT(1) NOT NULL DEFAULT 0,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_courses_uuid` (`uuid`),
KEY `idx_courses_subject` (`subject_id`),
KEY `idx_courses_teacher` (`teacher_id`),
CONSTRAINT `fk_courses_subject` FOREIGN KEY (`subject_id`) REFERENCES `subjects` (`id`) ON DELETE RESTRICT,
CONSTRAINT `fk_courses_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `users` (`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 7. Table: lessons (الدروس والفيديوهات المربوطة بـ Bunny Stream)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `lessons` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`course_id` BIGINT UNSIGNED NOT NULL,
`title` VARCHAR(255) NOT NULL,
`sequence_order` INT UNSIGNED NOT NULL DEFAULT 1,
`video_uuid` VARCHAR(64) DEFAULT NULL,
`hls_url` VARCHAR(500) DEFAULT NULL,
`timeline_chapters_json` JSON DEFAULT NULL,
`bunny_video_id` VARCHAR(100) DEFAULT NULL,
`duration_seconds` INT UNSIGNED NOT NULL DEFAULT 0,
`is_free_preview` TINYINT(1) NOT NULL DEFAULT 0,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_lessons_course` (`course_id`),
KEY `idx_lessons_video_uuid` (`video_uuid`),
CONSTRAINT `fk_lessons_course` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 8. Table: exams (الامتحانات الشاملة: درس، وحدة، فصل، ومحاكاة وزاري)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `exams` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`uuid` CHAR(36) NOT NULL,
`course_id` BIGINT UNSIGNED NOT NULL,
`lesson_id` BIGINT UNSIGNED DEFAULT NULL,
`created_by_id` BIGINT UNSIGNED DEFAULT NULL,
`creator_type` ENUM('teacher', 'ai_adaptive', 'ministry_standard') NOT NULL DEFAULT 'teacher',
`scope` ENUM('in_video_checkpoint', 'lesson_exam', 'unit_exam', 'semester_final') NOT NULL DEFAULT 'lesson_exam',
`title` VARCHAR(255) NOT NULL,
`description` TEXT DEFAULT NULL,
`duration_minutes` INT UNSIGNED NOT NULL DEFAULT 30,
`timestamp_seconds` INT UNSIGNED NOT NULL DEFAULT 0,
`rewind_on_fail_seconds` INT UNSIGNED NOT NULL DEFAULT 45,
`passing_percentage` DECIMAL(5,2) NOT NULL DEFAULT 60.00,
`total_points` INT UNSIGNED NOT NULL DEFAULT 100,
`is_mandatory` TINYINT(1) NOT NULL DEFAULT 1,
`is_published` TINYINT(1) NOT NULL DEFAULT 1,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_exams_uuid` (`uuid`),
KEY `idx_exams_course` (`course_id`),
KEY `idx_exams_lesson` (`lesson_id`),
KEY `idx_exams_scope` (`scope`),
KEY `idx_exams_creator` (`creator_type`),
CONSTRAINT `fk_exams_course` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_exams_lesson` FOREIGN KEY (`lesson_id`) REFERENCES `lessons` (`id`) ON DELETE SET NULL,
CONSTRAINT `fk_exams_creator` FOREIGN KEY (`created_by_id`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 9. Table: questions (الأسئلة والتحليل المعرفي للذكاء الاصطناعي)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `questions` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`uuid` CHAR(36) NOT NULL,
`exam_id` BIGINT UNSIGNED NOT NULL,
`question_text` TEXT NOT NULL,
`question_type` ENUM('multiple_choice', 'true_false', 'short_answer') NOT NULL DEFAULT 'multiple_choice',
`bloom_taxonomy` ENUM('recall', 'comprehension', 'application', 'analysis') NOT NULL DEFAULT 'comprehension',
`topic_tag` VARCHAR(150) DEFAULT NULL,
`points` INT UNSIGNED NOT NULL DEFAULT 10,
`explanation_text` TEXT DEFAULT NULL,
`ai_hint` TEXT DEFAULT NULL,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_questions_uuid` (`uuid`),
KEY `idx_questions_exam` (`exam_id`),
KEY `idx_questions_topic` (`topic_tag`),
CONSTRAINT `fk_questions_exam` FOREIGN KEY (`exam_id`) REFERENCES `exams` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 10. Table: question_options (خيارات الإجابة والتغذية الراجعة)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `question_options` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`question_id` BIGINT UNSIGNED NOT NULL,
`option_text` TEXT NOT NULL,
`is_correct` TINYINT(1) NOT NULL DEFAULT 0,
`feedback_text` TEXT DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_options_question` (`question_id`),
CONSTRAINT `fk_options_question` FOREIGN KEY (`question_id`) REFERENCES `questions` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 11. Table: exam_attempts (محاولات ونتائج الامتحانات وتحليل الذكاء الاصطناعي)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `exam_attempts` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`uuid` CHAR(36) NOT NULL,
`student_id` BIGINT UNSIGNED NOT NULL,
`exam_id` BIGINT UNSIGNED NOT NULL,
`attempt_number` INT UNSIGNED NOT NULL DEFAULT 1,
`score` DECIMAL(6,2) NOT NULL DEFAULT 0.00,
`total_score` DECIMAL(6,2) NOT NULL DEFAULT 100.00,
`percentage` DECIMAL(5,2) NOT NULL DEFAULT 0.00,
`status` ENUM('passed', 'failed', 'needs_remediation', 'in_progress') NOT NULL DEFAULT 'in_progress',
`time_spent_seconds` INT UNSIGNED NOT NULL DEFAULT 0,
`weak_topics_json` JSON DEFAULT NULL,
`ai_diagnostic_report` TEXT DEFAULT NULL,
`completed_at` TIMESTAMP NULL DEFAULT NULL,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_attempts_uuid` (`uuid`),
KEY `idx_attempts_student_exam` (`student_id`, `exam_id`),
KEY `idx_attempts_status` (`status`),
CONSTRAINT `fk_attempts_student` FOREIGN KEY (`student_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_attempts_exam` FOREIGN KEY (`exam_id`) REFERENCES `exams` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 12. Table: student_question_answers (تفاصيل إجابات الطالب على كل سؤال)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `student_question_answers` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`attempt_id` BIGINT UNSIGNED NOT NULL,
`student_id` BIGINT UNSIGNED NOT NULL,
`question_id` BIGINT UNSIGNED NOT NULL,
`selected_option_id` BIGINT UNSIGNED DEFAULT NULL,
`text_answer` TEXT DEFAULT NULL,
`is_correct` TINYINT(1) NOT NULL DEFAULT 0,
`points_awarded` DECIMAL(5,2) NOT NULL DEFAULT 0.00,
`time_spent_seconds` INT UNSIGNED NOT NULL DEFAULT 0,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_sqa_attempt` (`attempt_id`),
KEY `idx_sqa_student_question` (`student_id`, `question_id`),
CONSTRAINT `fk_sqa_attempt` FOREIGN KEY (`attempt_id`) REFERENCES `exam_attempts` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_sqa_student` FOREIGN KEY (`student_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_sqa_question` FOREIGN KEY (`question_id`) REFERENCES `questions` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_sqa_option` FOREIGN KEY (`selected_option_id`) REFERENCES `question_options` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 13. Table: student_mastery_analytics (مؤشر الفهم التراكمي والجاهزية للوزاري)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `student_mastery_analytics` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`student_id` BIGINT UNSIGNED NOT NULL,
`course_id` BIGINT UNSIGNED NOT NULL,
`subject_id` BIGINT UNSIGNED NOT NULL,
`mastery_percentage` DECIMAL(5,2) NOT NULL DEFAULT 0.00,
`tawjihi_readiness_score` DECIMAL(5,2) NOT NULL DEFAULT 0.00,
`lessons_completed_count` INT UNSIGNED NOT NULL DEFAULT 0,
`exams_passed_count` INT UNSIGNED NOT NULL DEFAULT 0,
`exams_total_count` INT UNSIGNED NOT NULL DEFAULT 0,
`weak_concepts_summary` TEXT DEFAULT NULL,
`ai_recommendations` TEXT DEFAULT NULL,
`guardian_last_notified_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_mastery_student_course` (`student_id`, `course_id`),
KEY `idx_mastery_subject` (`subject_id`),
CONSTRAINT `fk_mastery_student` FOREIGN KEY (`student_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_mastery_course` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_mastery_subject` FOREIGN KEY (`subject_id`) REFERENCES `subjects` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 14. Table: lesson_progress (سجل متابعة ونبض المشاهدة)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `lesson_progress` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` BIGINT UNSIGNED NOT NULL,
`lesson_id` BIGINT UNSIGNED NOT NULL,
`last_watched_second` INT UNSIGNED NOT NULL DEFAULT 0,
`max_watched_second` INT UNSIGNED NOT NULL DEFAULT 0,
`completion_pct` DECIMAL(5,2) NOT NULL DEFAULT 0.00,
`is_completed` TINYINT(1) NOT NULL DEFAULT 0,
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_user_lesson_progress` (`user_id`, `lesson_id`),
KEY `idx_progress_lesson` (`lesson_id`),
CONSTRAINT `fk_progress_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_progress_lesson` FOREIGN KEY (`lesson_id`) REFERENCES `lessons` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 15. Table: voice_notes (الملاحظات الصوتية والتفريغ الذكي)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `voice_notes` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`uuid` CHAR(36) NOT NULL,
`user_id` BIGINT UNSIGNED NOT NULL,
`lesson_id` BIGINT UNSIGNED NOT NULL,
`timestamp_seconds` INT UNSIGNED NOT NULL DEFAULT 0,
`audio_url` VARCHAR(500) NOT NULL,
`transcript_text` TEXT DEFAULT NULL,
`ai_summary` TEXT DEFAULT NULL,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_voice_notes_uuid` (`uuid`),
KEY `idx_voice_notes_user_lesson` (`user_id`, `lesson_id`),
CONSTRAINT `fk_vn_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_vn_lesson` FOREIGN KEY (`lesson_id`) REFERENCES `lessons` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 16. Table: user_devices (بصمة الأجهزة المربوطة بالجلسات)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `user_devices` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` BIGINT UNSIGNED NOT NULL,
`device_fingerprint` VARCHAR(64) NOT NULL,
`device_name` VARCHAR(150) DEFAULT NULL,
`platform` ENUM('android', 'ios', 'windows', 'macos', 'web') NOT NULL,
`is_active` TINYINT(1) NOT NULL DEFAULT 1,
`last_active_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_user_device_fingerprint` (`user_id`, `device_fingerprint`),
CONSTRAINT `fk_ud_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 17. Table: otp_verifications (رموز التحقق عبر منصة صقل)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `otp_verifications` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`phone_hash` VARCHAR(64) NOT NULL,
`otp_code_hash` VARCHAR(255) NOT NULL,
`attempts` INT UNSIGNED NOT NULL DEFAULT 0,
`is_used` TINYINT(1) NOT NULL DEFAULT 0,
`expires_at` TIMESTAMP NOT NULL,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_otp_phone_hash` (`phone_hash`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 18. Table: chat_messages (المحادثات المباشرة بين الطلاب والمعلمين)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `chat_messages` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`uuid` CHAR(36) NOT NULL,
`sender_id` BIGINT UNSIGNED NOT NULL,
`receiver_id` BIGINT UNSIGNED NOT NULL,
`course_id` BIGINT UNSIGNED DEFAULT NULL,
`lesson_id` BIGINT UNSIGNED DEFAULT NULL,
`message` TEXT NOT NULL,
`message_type` ENUM('text', 'voice', 'image', 'file') NOT NULL DEFAULT 'text',
`media_url` VARCHAR(500) DEFAULT NULL,
`is_read` TINYINT(1) NOT NULL DEFAULT 0,
`read_at` TIMESTAMP NULL DEFAULT NULL,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_chat_messages_uuid` (`uuid`),
KEY `idx_chat_sender_receiver` (`sender_id`, `receiver_id`),
KEY `idx_chat_receiver_unread` (`receiver_id`, `is_read`),
KEY `idx_chat_course` (`course_id`),
KEY `idx_chat_created_at` (`created_at`),
CONSTRAINT `fk_chat_sender` FOREIGN KEY (`sender_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_chat_receiver` FOREIGN KEY (`receiver_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_chat_course` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE SET NULL,
CONSTRAINT `fk_chat_lesson` FOREIGN KEY (`lesson_id`) REFERENCES `lessons` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 19. Table: teacher_reviews (تقييمات الطلاب المعايرة والمحصنة ضد الكيد)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `teacher_reviews` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`teacher_id` BIGINT UNSIGNED NOT NULL,
`student_id` BIGINT UNSIGNED NOT NULL,
`course_id` BIGINT UNSIGNED DEFAULT NULL,
`lesson_id` BIGINT UNSIGNED DEFAULT NULL,
`rating_overall` DECIMAL(3, 2) NOT NULL DEFAULT 5.00,
`rating_clarity` INT NOT NULL DEFAULT 5,
`rating_response_speed` INT NOT NULL DEFAULT 5,
`rating_socratic_interaction` INT NOT NULL DEFAULT 5,
`review_text` TEXT DEFAULT NULL,
`review_weight` DECIMAL(4, 3) NOT NULL DEFAULT 1.000,
`student_watch_percentage` DECIMAL(5, 2) NOT NULL DEFAULT 100.00,
`socratic_accuracy_rate` DECIMAL(5, 2) NOT NULL DEFAULT 100.00,
`is_flagged_anomaly` TINYINT(1) NOT NULL DEFAULT 0,
`is_verified` TINYINT(1) NOT NULL DEFAULT 1,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_tr_teacher` (`teacher_id`),
KEY `idx_tr_student` (`student_id`),
CONSTRAINT `fk_tr_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_tr_student` FOREIGN KEY (`student_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- ------------------------------------------------------------------------------
-- 20. Table: teacher_performance_metrics (مؤشرات الجدارة وسرعة الرد مع خوارزمية الطابور العادل)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `teacher_performance_metrics` (
`teacher_id` BIGINT UNSIGNED NOT NULL,
`avg_response_minutes` INT NOT NULL DEFAULT 4,
`response_rate_percentage` DECIMAL(5, 2) NOT NULL DEFAULT 98.50,
`active_queue_count` INT UNSIGNED NOT NULL DEFAULT 0,
`total_students_enrolled` INT UNSIGNED NOT NULL DEFAULT 0,
`total_reviews_count` INT UNSIGNED NOT NULL DEFAULT 0,
`raw_avg_rating` DECIMAL(3, 2) NOT NULL DEFAULT 5.00,
`weighted_student_rating` DECIMAL(3, 2) NOT NULL DEFAULT 5.00,
`ai_engagement_score` DECIMAL(5, 2) NOT NULL DEFAULT 96.00,
`sla_speed_score` DECIMAL(5, 2) NOT NULL DEFAULT 98.00,
`mastery_impact_score` DECIMAL(5, 2) NOT NULL DEFAULT 94.00,
`composite_merit_score` DECIMAL(5, 2) NOT NULL DEFAULT 96.50,
`star_equivalent` DECIMAL(3, 2) NOT NULL DEFAULT 4.90,
`reputation_tier` VARCHAR(100) NOT NULL DEFAULT 'معلم نخبوي معتمد 💎',
`last_calculated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`teacher_id`),
CONSTRAINT `fk_tpm_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SET FOREIGN_KEY_CHECKS = 1;