/*
  Warnings:

  - You are about to drop the `db_modules` table. If the table is not empty, all the data it contains will be lost.
  - A unique constraint covering the columns `[facebookId]` on the table `db_users` will be added. If there are existing duplicate values, this will fail.

*/
-- DropForeignKey
ALTER TABLE `db_modules` DROP FOREIGN KEY `db_modules_courseId_fkey`;

-- DropForeignKey
ALTER TABLE `db_teacher_order` DROP FOREIGN KEY `db_teacher_order_moduleId_fkey`;

-- DropForeignKey
ALTER TABLE `db_teacher_topics` DROP FOREIGN KEY `db_teacher_topics_moduleId_fkey`;

-- DropForeignKey
ALTER TABLE `db_topics` DROP FOREIGN KEY `db_topics_moduleId_fkey`;

-- DropIndex
DROP INDEX `db_teacher_order_moduleId_fkey` ON `db_teacher_order`;

-- DropIndex
DROP INDEX `db_teacher_topics_moduleId_fkey` ON `db_teacher_topics`;

-- DropIndex
DROP INDEX `db_topics_moduleId_fkey` ON `db_topics`;

-- AlterTable
ALTER TABLE `db_users` ADD COLUMN `facebookId` VARCHAR(255) NULL;

-- DropTable
DROP TABLE `db_modules`;

-- CreateTable
CREATE TABLE `db_courses_modules` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(255) NOT NULL,
    `slug` VARCHAR(255) NOT NULL,
    `description` TEXT NULL,
    `bannerUrl` VARCHAR(255) NULL,
    `thumbnailUrl` VARCHAR(255) NULL,
    `isPublished` BOOLEAN NOT NULL DEFAULT false,
    `status` ENUM('ACTIVE', 'INACTIVE') NOT NULL DEFAULT 'ACTIVE',
    `deleted` BOOLEAN NOT NULL DEFAULT false,
    `index` INTEGER NULL,
    `courseId` INTEGER NOT NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,
    `deleted_at` DATETIME(3) NOT NULL,
    `created_by` INTEGER NULL,
    `updated_by` INTEGER NULL,
    `deleted_by` INTEGER NULL,

    UNIQUE INDEX `db_courses_modules_slug_key`(`slug`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `db_student_activities_log` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `studentId` INTEGER NULL,
    `schoolId` INTEGER NULL,
    `courseId` INTEGER NULL,
    `moduleId` INTEGER NULL,
    `teacherModuleId` INTEGER NULL,
    `topicId` INTEGER NULL,
    `teacherTopicId` INTEGER NULL,
    `schoolCourseId` INTEGER NULL,
    `teacherId` INTEGER NULL,
    `activityId` INTEGER NULL,
    `teacherActivityId` INTEGER NULL,
    `activityType` VARCHAR(191) NULL,
    `type` VARCHAR(191) NULL DEFAULT 'ACTIVITY',
    `totalMark` DOUBLE NULL,
    `obtainMark` DOUBLE NULL,
    `opened` BOOLEAN NULL,
    `openedAt` DATETIME(3) NULL,
    `timeSpent` INTEGER NULL DEFAULT 0,
    `status` ENUM('ACTIVE', 'INACTIVE') NOT NULL DEFAULT 'ACTIVE',
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `createdBy` INTEGER NULL,
    `updatedBy` INTEGER NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `db_student_activities_answer_log` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `studentId` INTEGER NULL,
    `quizId` INTEGER NULL,
    `teacherQuizId` INTEGER NULL,
    `answer` VARCHAR(191) NULL,
    `correctAnswer` VARCHAR(191) NULL,
    `question` TEXT NULL,
    `isRight` BOOLEAN NOT NULL DEFAULT false,
    `score` INTEGER NULL DEFAULT 0,
    `timeSpent` INTEGER NULL DEFAULT 0,
    `studentActivitiesLogId` INTEGER NULL,
    `status` ENUM('ACTIVE', 'INACTIVE') NOT NULL DEFAULT 'ACTIVE',
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `createdBy` INTEGER NULL,
    `updatedBy` INTEGER NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `db_student_topic_progress` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `studentId` INTEGER NULL,
    `courseId` INTEGER NULL,
    `moduleId` INTEGER NULL,
    `teacherModuleId` INTEGER NULL,
    `topicId` INTEGER NULL,
    `teacherTopicId` INTEGER NULL,
    `schoolCourseId` INTEGER NULL,
    `teacherId` INTEGER NULL,
    `totalNoOfActivites` INTEGER NULL,
    `completedNoOfActivites` INTEGER NULL,
    `progress` DOUBLE NULL DEFAULT 0,
    `progressStatus` ENUM('NOTSTARTED', 'INPROGRESS', 'COMPLETED', 'FAILED') NOT NULL DEFAULT 'INPROGRESS',
    `completedAt` DATETIME(3) NULL,
    `completed` BOOLEAN NOT NULL DEFAULT false,
    `score` DOUBLE NULL DEFAULT 0,
    `status` ENUM('ACTIVE', 'INACTIVE') NOT NULL DEFAULT 'ACTIVE',
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `createdBy` INTEGER NULL,
    `updatedBy` INTEGER NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `db_student_module_progress` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `studentId` INTEGER NULL,
    `courseId` INTEGER NULL,
    `moduleId` INTEGER NULL,
    `teacherModuleId` INTEGER NULL,
    `schoolCourseId` INTEGER NULL,
    `teacherId` INTEGER NULL,
    `totalNoOfTopic` INTEGER NULL,
    `completedNoOfTopic` INTEGER NULL,
    `progress` DOUBLE NULL DEFAULT 0,
    `progressStatus` ENUM('NOTSTARTED', 'INPROGRESS', 'COMPLETED', 'FAILED') NOT NULL DEFAULT 'INPROGRESS',
    `completedAt` DATETIME(3) NULL,
    `completed` BOOLEAN NOT NULL DEFAULT false,
    `score` DOUBLE NULL DEFAULT 0,
    `status` ENUM('ACTIVE', 'INACTIVE') NOT NULL DEFAULT 'ACTIVE',
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `createdBy` INTEGER NULL,
    `updatedBy` INTEGER NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `db_student_course_progress` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `studentId` INTEGER NULL,
    `courseId` INTEGER NULL,
    `schoolCourseId` INTEGER NULL,
    `teacherId` INTEGER NULL,
    `totalNoOfModule` INTEGER NULL,
    `completedNoOfModule` INTEGER NULL,
    `progress` DOUBLE NULL DEFAULT 0,
    `progressStatus` ENUM('NOTSTARTED', 'INPROGRESS', 'COMPLETED', 'FAILED') NOT NULL DEFAULT 'INPROGRESS',
    `completedAt` DATETIME(3) NULL,
    `completed` BOOLEAN NOT NULL DEFAULT false,
    `score` DOUBLE NULL DEFAULT 0,
    `status` ENUM('ACTIVE', 'INACTIVE') NOT NULL DEFAULT 'ACTIVE',
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `createdBy` INTEGER NULL,
    `updatedBy` INTEGER NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `db_rule_book` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(255) NULL,
    `category` VARCHAR(255) NULL,
    `description` TEXT NULL,
    `ruleKey` VARCHAR(255) NULL,
    `pointType` VARCHAR(255) NULL DEFAULT 'REWARD',
    `points` DOUBLE NULL DEFAULT 0,
    `isPublished` BOOLEAN NOT NULL DEFAULT false,
    `status` ENUM('ACTIVE', 'INACTIVE') NOT NULL DEFAULT 'ACTIVE',
    `index` INTEGER NULL DEFAULT 1,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `createdBy` INTEGER NULL,
    `updatedBy` INTEGER NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateIndex
CREATE UNIQUE INDEX `db_users_facebookId_key` ON `db_users`(`facebookId`);

-- AddForeignKey
ALTER TABLE `db_courses_modules` ADD CONSTRAINT `db_courses_modules_courseId_fkey` FOREIGN KEY (`courseId`) REFERENCES `db_courses`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_topics` ADD CONSTRAINT `db_topics_moduleId_fkey` FOREIGN KEY (`moduleId`) REFERENCES `db_courses_modules`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_teacher_topics` ADD CONSTRAINT `db_teacher_topics_moduleId_fkey` FOREIGN KEY (`moduleId`) REFERENCES `db_courses_modules`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_teacher_order` ADD CONSTRAINT `db_teacher_order_moduleId_fkey` FOREIGN KEY (`moduleId`) REFERENCES `db_courses_modules`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_log` ADD CONSTRAINT `db_student_activities_log_studentId_fkey` FOREIGN KEY (`studentId`) REFERENCES `db_students`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_log` ADD CONSTRAINT `db_student_activities_log_schoolId_fkey` FOREIGN KEY (`schoolId`) REFERENCES `db_schools`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_log` ADD CONSTRAINT `db_student_activities_log_courseId_fkey` FOREIGN KEY (`courseId`) REFERENCES `db_courses`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_log` ADD CONSTRAINT `db_student_activities_log_moduleId_fkey` FOREIGN KEY (`moduleId`) REFERENCES `db_courses_modules`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_log` ADD CONSTRAINT `db_student_activities_log_teacherModuleId_fkey` FOREIGN KEY (`teacherModuleId`) REFERENCES `db_teacher_modules`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_log` ADD CONSTRAINT `db_student_activities_log_topicId_fkey` FOREIGN KEY (`topicId`) REFERENCES `db_topics`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_log` ADD CONSTRAINT `db_student_activities_log_teacherTopicId_fkey` FOREIGN KEY (`teacherTopicId`) REFERENCES `db_teacher_topics`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_log` ADD CONSTRAINT `db_student_activities_log_schoolCourseId_fkey` FOREIGN KEY (`schoolCourseId`) REFERENCES `db_schools_course`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_log` ADD CONSTRAINT `db_student_activities_log_teacherId_fkey` FOREIGN KEY (`teacherId`) REFERENCES `db_users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_log` ADD CONSTRAINT `db_student_activities_log_activityId_fkey` FOREIGN KEY (`activityId`) REFERENCES `db_activities`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_log` ADD CONSTRAINT `db_student_activities_log_teacherActivityId_fkey` FOREIGN KEY (`teacherActivityId`) REFERENCES `db_teacher_activites`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_answer_log` ADD CONSTRAINT `db_student_activities_answer_log_studentId_fkey` FOREIGN KEY (`studentId`) REFERENCES `db_students`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_answer_log` ADD CONSTRAINT `db_student_activities_answer_log_quizId_fkey` FOREIGN KEY (`quizId`) REFERENCES `db_activites_quiz`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_answer_log` ADD CONSTRAINT `db_student_activities_answer_log_teacherQuizId_fkey` FOREIGN KEY (`teacherQuizId`) REFERENCES `db_teacher_activites_quiz`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_activities_answer_log` ADD CONSTRAINT `db_student_activities_answer_log_studentActivitiesLogId_fkey` FOREIGN KEY (`studentActivitiesLogId`) REFERENCES `db_student_activities_log`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_topic_progress` ADD CONSTRAINT `db_student_topic_progress_studentId_fkey` FOREIGN KEY (`studentId`) REFERENCES `db_students`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_topic_progress` ADD CONSTRAINT `db_student_topic_progress_courseId_fkey` FOREIGN KEY (`courseId`) REFERENCES `db_courses`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_topic_progress` ADD CONSTRAINT `db_student_topic_progress_moduleId_fkey` FOREIGN KEY (`moduleId`) REFERENCES `db_courses_modules`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_topic_progress` ADD CONSTRAINT `db_student_topic_progress_teacherModuleId_fkey` FOREIGN KEY (`teacherModuleId`) REFERENCES `db_teacher_modules`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_topic_progress` ADD CONSTRAINT `db_student_topic_progress_topicId_fkey` FOREIGN KEY (`topicId`) REFERENCES `db_topics`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_topic_progress` ADD CONSTRAINT `db_student_topic_progress_teacherTopicId_fkey` FOREIGN KEY (`teacherTopicId`) REFERENCES `db_teacher_topics`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_topic_progress` ADD CONSTRAINT `db_student_topic_progress_schoolCourseId_fkey` FOREIGN KEY (`schoolCourseId`) REFERENCES `db_schools_course`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_topic_progress` ADD CONSTRAINT `db_student_topic_progress_teacherId_fkey` FOREIGN KEY (`teacherId`) REFERENCES `db_users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_module_progress` ADD CONSTRAINT `db_student_module_progress_studentId_fkey` FOREIGN KEY (`studentId`) REFERENCES `db_students`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_module_progress` ADD CONSTRAINT `db_student_module_progress_courseId_fkey` FOREIGN KEY (`courseId`) REFERENCES `db_courses`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_module_progress` ADD CONSTRAINT `db_student_module_progress_moduleId_fkey` FOREIGN KEY (`moduleId`) REFERENCES `db_courses_modules`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_module_progress` ADD CONSTRAINT `db_student_module_progress_teacherModuleId_fkey` FOREIGN KEY (`teacherModuleId`) REFERENCES `db_teacher_modules`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_module_progress` ADD CONSTRAINT `db_student_module_progress_schoolCourseId_fkey` FOREIGN KEY (`schoolCourseId`) REFERENCES `db_schools_course`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_module_progress` ADD CONSTRAINT `db_student_module_progress_teacherId_fkey` FOREIGN KEY (`teacherId`) REFERENCES `db_users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_course_progress` ADD CONSTRAINT `db_student_course_progress_studentId_fkey` FOREIGN KEY (`studentId`) REFERENCES `db_students`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_course_progress` ADD CONSTRAINT `db_student_course_progress_courseId_fkey` FOREIGN KEY (`courseId`) REFERENCES `db_courses`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_course_progress` ADD CONSTRAINT `db_student_course_progress_schoolCourseId_fkey` FOREIGN KEY (`schoolCourseId`) REFERENCES `db_schools_course`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `db_student_course_progress` ADD CONSTRAINT `db_student_course_progress_teacherId_fkey` FOREIGN KEY (`teacherId`) REFERENCES `db_users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
