-- 0020_leave_requests.sql
-- L-5 在线请假流程：创建 leave_requests 表。

CREATE TABLE IF NOT EXISTS `leave_requests` (
  `id` varchar(128) NOT NULL,
  `student_id` varchar(128) NOT NULL,
  `requester_id` varchar(128) NOT NULL,
  `class_id` varchar(128) NOT NULL,
  `leave_type` enum('sick','personal','family','other') NOT NULL,
  `start_date` date NOT NULL,
  `end_date` date NOT NULL,
  `reason` text NOT NULL,
  `status` enum('pending','approved','rejected','cancelled') NOT NULL DEFAULT 'pending',
  `reviewer_id` varchar(128) DEFAULT NULL,
  `review_comment` text,
  `reviewed_at` timestamp NULL DEFAULT NULL,
  `attendance_synced` boolean NOT NULL DEFAULT false,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `leave_requests_student_idx` (`student_id`),
  KEY `leave_requests_requester_idx` (`requester_id`),
  KEY `leave_requests_class_idx` (`class_id`),
  KEY `leave_requests_status_idx` (`status`),
  KEY `leave_requests_reviewer_idx` (`reviewer_id`),
  KEY `leave_requests_class_status_idx` (`class_id`, `status`),
  KEY `leave_requests_student_status_idx` (`student_id`, `status`),
  CONSTRAINT `lr_c_fk` FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`) ON DELETE CASCADE,
  CONSTRAINT `lr_s_fk` FOREIGN KEY (`student_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `lr_rq_fk` FOREIGN KEY (`requester_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `lr_rv_fk` FOREIGN KEY (`reviewer_id`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 为 builtin 角色注入新权限（INSERT IGNORE 幂等）
-- admin：三个权限全有
INSERT IGNORE INTO `role_permissions` (`role_id`, `permission`)
  SELECT id, 'leave_request:create' FROM `roles` WHERE `name` = 'admin'
  UNION ALL SELECT id, 'leave_request:read' FROM `roles` WHERE `name` = 'admin'
  UNION ALL SELECT id, 'leave_request:review' FROM `roles` WHERE `name` = 'admin'
  -- teacher：read + review（班主任审批本班请假）
  UNION ALL SELECT id, 'leave_request:read' FROM `roles` WHERE `name` = 'teacher'
  UNION ALL SELECT id, 'leave_request:review' FROM `roles` WHERE `name` = 'teacher'
  -- parent：create + read（代子女提交、查看子女请假记录）
  UNION ALL SELECT id, 'leave_request:create' FROM `roles` WHERE `name` = 'parent'
  UNION ALL SELECT id, 'leave_request:read' FROM `roles` WHERE `name` = 'parent'
  -- student：create + read（本人提交、查看本人请假记录）
  UNION ALL SELECT id, 'leave_request:create' FROM `roles` WHERE `name` = 'student'
  UNION ALL SELECT id, 'leave_request:read' FROM `roles` WHERE `name` = 'student'
  -- grade_head / teaching_head：read + review（年级主任/教务主任可审批）
  UNION ALL SELECT id, 'leave_request:read' FROM `roles` WHERE `name` = 'grade_head'
  UNION ALL SELECT id, 'leave_request:review' FROM `roles` WHERE `name` = 'grade_head'
  UNION ALL SELECT id, 'leave_request:read' FROM `roles` WHERE `name` = 'teaching_head'
  UNION ALL SELECT id, 'leave_request:review' FROM `roles` WHERE `name` = 'teaching_head';
