CREATE TABLE IF NOT EXISTS `revenue_periods` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `uuid` CHAR(36) NOT NULL, `starts_at` TIMESTAMP NOT NULL, `ends_at` TIMESTAMP NOT NULL, `policy_version` VARCHAR(64) NOT NULL, `status` ENUM('open','closing','closed','reopened') NOT NULL DEFAULT 'open', `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `uq_revenue_period_uuid` (`uuid`), UNIQUE KEY `uq_revenue_period_dates` (`starts_at`,`ends_at`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE IF NOT EXISTS `teacher_revenue_allocations` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `revenue_period_id` BIGINT UNSIGNED NOT NULL, `teacher_id` BIGINT UNSIGNED NOT NULL, `video_version_id` BIGINT UNSIGNED NOT NULL, `eligible_seconds` BIGINT UNSIGNED NOT NULL, `amount_fils` BIGINT UNSIGNED NOT NULL, `status` ENUM('estimated','held','approved','reversed') NOT NULL DEFAULT 'estimated', `ledger_entry_id` BIGINT UNSIGNED NULL, `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `uq_teacher_allocation` (`revenue_period_id`,`teacher_id`,`video_version_id`), CONSTRAINT `fk_allocation_period` FOREIGN KEY (`revenue_period_id`) REFERENCES `revenue_periods` (`id`) ON DELETE RESTRICT, CONSTRAINT `fk_allocation_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teachers` (`id`) ON DELETE RESTRICT, CONSTRAINT `fk_allocation_version` FOREIGN KEY (`video_version_id`) REFERENCES `video_versions` (`id`) ON DELETE RESTRICT, CONSTRAINT `fk_allocation_ledger` FOREIGN KEY (`ledger_entry_id`) REFERENCES `ledger_entries` (`id`) ON DELETE RESTRICT ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE IF NOT EXISTS `lesson_video_ratings` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `student_id` BIGINT UNSIGNED NOT NULL, `video_version_id` BIGINT UNSIGNED NOT NULL, `rating` TINYINT UNSIGNED NOT NULL, `watch_session_id` BIGINT UNSIGNED NOT NULL, `status` ENUM('pending','verified','flagged','rejected') NOT NULL DEFAULT 'pending', `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `uq_student_video_rating` (`student_id`,`video_version_id`), CONSTRAINT `fk_lvr_student` FOREIGN KEY (`student_id`) REFERENCES `students` (`id`) ON DELETE RESTRICT, CONSTRAINT `fk_lvr_version` FOREIGN KEY (`video_version_id`) REFERENCES `video_versions` (`id`) ON DELETE RESTRICT, CONSTRAINT `fk_lvr_session` FOREIGN KEY (`watch_session_id`) REFERENCES `watch_sessions` (`id`) ON DELETE RESTRICT, CONSTRAINT `chk_lvr_rating` CHECK (`rating` BETWEEN 1 AND 5) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;