SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

DROP TABLE IF EXISTS `attendance_logs`;
DROP TABLE IF EXISTS `attendance`;
DROP TABLE IF EXISTS `attendance_records`;
DROP TABLE IF EXISTS `activities`;
DROP TABLE IF EXISTS `students`;
DROP TABLE IF EXISTS `courses`;
DROP TABLE IF EXISTS `departments`;
DROP TABLE IF EXISTS `schools`;
DROP TABLE IF EXISTS `genders`;
DROP TABLE IF EXISTS `system_settings`;
DROP TABLE IF EXISTS `users`;

CREATE TABLE `users` (
    `id` int NOT NULL AUTO_INCREMENT,
    `username` varchar(50) NOT NULL,
    `password` varchar(255) NOT NULL,
    `role` enum('admin','staff') NOT NULL DEFAULT 'staff',
    `full_name` varchar(120) NOT NULL,
    `email` varchar(120) DEFAULT NULL,
    `status` tinyint NOT NULL DEFAULT 1,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `system_settings` (
    `id` int NOT NULL AUTO_INCREMENT,
    `setting_key` varchar(80) NOT NULL,
    `setting_value` text NULL,
    `description` text NULL,
    `updated_by` int DEFAULT NULL,
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_setting_key` (`setting_key`),
    CONSTRAINT `fk_settings_user` FOREIGN KEY (`updated_by`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `schools` (
    `id` int NOT NULL AUTO_INCREMENT,
    `code` varchar(10) NOT NULL,
    `name` varchar(150) NOT NULL,
    `address` text NULL,
    `contact_number` varchar(30) NULL,
    `email` varchar(120) NULL,
    `status` tinyint NOT NULL DEFAULT 1,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_school_code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `genders` (
    `id` int NOT NULL AUTO_INCREMENT,
    `code` varchar(1) NOT NULL,
    `name` varchar(20) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_gender_code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `departments` (
    `id` int NOT NULL AUTO_INCREMENT,
    `code` varchar(10) NOT NULL,
    `name` varchar(150) NOT NULL,
    `description` text NULL,
    `school_id` int NOT NULL,
    `status` tinyint NOT NULL DEFAULT 1,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_department_code` (`code`),
    CONSTRAINT `fk_departments_school` FOREIGN KEY (`school_id`) REFERENCES `schools` (`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `courses` (
    `id` int NOT NULL AUTO_INCREMENT,
    `code` varchar(15) NOT NULL,
    `name` varchar(150) NOT NULL,
    `department_id` int NOT NULL,
    `description` text NULL,
    `status` tinyint NOT NULL DEFAULT 1,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_course_code` (`code`),
    CONSTRAINT `fk_courses_department` FOREIGN KEY (`department_id`) REFERENCES `departments` (`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `students` (
    `id` int NOT NULL AUTO_INCREMENT,
    `student_id` varchar(30) NOT NULL,
    `full_name` varchar(150) NOT NULL,
    `gender_id` int NOT NULL,
    `year_level` int NOT NULL DEFAULT 1,
    `course_id` int NOT NULL,
    `department_id` int NOT NULL,
    `school_id` int NOT NULL,
    `email` varchar(150) NULL,
    `contact_number` varchar(30) NULL,
    `address` text NULL,
    `qr_code` varchar(255) NOT NULL,
    `status` tinyint NOT NULL DEFAULT 1,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_student_id` (`student_id`),
    UNIQUE KEY `uniq_qr_code` (`qr_code`),
    CONSTRAINT `fk_students_gender` FOREIGN KEY (`gender_id`) REFERENCES `genders` (`id`) ON DELETE RESTRICT,
    CONSTRAINT `fk_students_course` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE RESTRICT,
    CONSTRAINT `fk_students_department` FOREIGN KEY (`department_id`) REFERENCES `departments` (`id`) ON DELETE RESTRICT,
    CONSTRAINT `fk_students_school` FOREIGN KEY (`school_id`) REFERENCES `schools` (`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `activities` (
    `id` int NOT NULL AUTO_INCREMENT,
    `name` varchar(150) NOT NULL,
    `date` date NOT NULL,
    `time_in` time NULL,
    `time_out` time NULL,
    `location` varchar(255) NULL,
    `description` text NULL,
    `required_students` enum('all','specific_course','specific_department') NOT NULL DEFAULT 'all',
    `course_id` int DEFAULT NULL,
    `department_id` int DEFAULT NULL,
    `status` tinyint NOT NULL DEFAULT 1,
    `created_by` int NOT NULL,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    CONSTRAINT `fk_activities_course` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE SET NULL,
    CONSTRAINT `fk_activities_department` FOREIGN KEY (`department_id`) REFERENCES `departments` (`id`) ON DELETE SET NULL,
    CONSTRAINT `fk_activities_user` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `attendance` (
    `id` int NOT NULL AUTO_INCREMENT,
    `student_id` int NOT NULL,
    `activity_id` int NOT NULL,
    `time_in` datetime DEFAULT NULL,
    `time_out` datetime DEFAULT NULL,
    `status` enum('present','late','absent','excused') NOT NULL DEFAULT 'present',
    `notes` text NULL,
    `recorded_by` int NOT NULL,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_student_activity` (`student_id`,`activity_id`),
    CONSTRAINT `fk_attendance_student` FOREIGN KEY (`student_id`) REFERENCES `students` (`id`) ON DELETE CASCADE,
    CONSTRAINT `fk_attendance_activity` FOREIGN KEY (`activity_id`) REFERENCES `activities` (`id`) ON DELETE CASCADE,
    CONSTRAINT `fk_attendance_user` FOREIGN KEY (`recorded_by`) REFERENCES `users` (`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `attendance_logs` (
    `id` int NOT NULL AUTO_INCREMENT,
    `attendance_id` int NOT NULL,
    `action` enum('time_in','time_out','status_change','manual_entry') NOT NULL,
    `old_value` varchar(255) NULL,
    `new_value` varchar(255) NULL,
    `changed_by` int NOT NULL,
    `notes` text NULL,
    `changed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    CONSTRAINT `fk_logs_attendance` FOREIGN KEY (`attendance_id`) REFERENCES `attendance` (`id`) ON DELETE CASCADE,
    CONSTRAINT `fk_logs_user` FOREIGN KEY (`changed_by`) REFERENCES `users` (`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `attendance_records` (
    `id` int NOT NULL AUTO_INCREMENT,
    `student_id` int NOT NULL,
    `course_id` int NOT NULL,
    `status` enum('present','late','absent') NOT NULL DEFAULT 'present',
    `date_recorded` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `created_by` int NOT NULL,
    PRIMARY KEY (`id`),
    CONSTRAINT `fk_records_student` FOREIGN KEY (`student_id`) REFERENCES `students` (`id`) ON DELETE RESTRICT,
    CONSTRAINT `fk_records_course` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE RESTRICT,
    CONSTRAINT `fk_records_user` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `users` (`username`, `password`, `role`, `full_name`, `email`, `status`)
VALUES ('admin', '$2y$12$Z0SOoRGlhfq65/a0eNlHL.pnO7qhYWJoUlZKDmnp7SwNttCkY07hC', 'admin', 'System Administrator', 'admin@example.com', 1);

INSERT INTO `genders` (`code`, `name`) VALUES
('M', 'Male'),
('F', 'Female'),
('O', 'Other');

INSERT INTO `schools` (`code`, `name`, `address`, `contact_number`, `email`) VALUES
('ACAD', 'Academy of Innovation', '123 University Ave, Solano', '+63 900 000 0000', 'info@academy.edu.ph');

INSERT INTO `departments` (`code`, `name`, `description`, `school_id`) VALUES
('CASE', 'College of Arts & Sciences', 'General education and sciences', 1),
('CBA', 'College of Business & Accountancy', 'Business programs', 1);

INSERT INTO `courses` (`code`, `name`, `department_id`, `description`) VALUES
('BSIT', 'BS Information Technology', 1, 'Technology track'),
('BSBA', 'BS Business Administration', 2, 'Business track');

INSERT INTO `system_settings` (`setting_key`, `setting_value`, `description`, `updated_by`) VALUES
('app.name', 'QR Attendance System', 'System display name', 1),
('attendance.cutoff_minutes', '15', 'Minutes before absent marked late', 1),
('qr.prefix', 'STU', 'Prefix for generated QR tokens', 1);

SET FOREIGN_KEY_CHECKS = 1;
