CREATE TABLE IF NOT EXISTS schema_metadata (
    id TINYINT UNSIGNED NOT NULL,
    schema_version INT UNSIGNED NOT NULL DEFAULT 1,
    app_version INT UNSIGNED NOT NULL DEFAULT 1,
    self_registration_enabled BOOLEAN NOT NULL DEFAULT FALSE,
    created_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (id),
    CONSTRAINT chk_schema_metadata_singleton CHECK (id = 1)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS organization (
    id TINYINT UNSIGNED NOT NULL,
    name VARCHAR(120) NOT NULL,
    code VARCHAR(60) NOT NULL,
    phone VARCHAR(60) NOT NULL,
    address VARCHAR(255) NOT NULL,
    PRIMARY KEY (id),
    CONSTRAINT chk_organization_singleton CHECK (id = 1)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS schools (
    id VARCHAR(64) NOT NULL,
    name VARCHAR(160) NOT NULL,
    code VARCHAR(40) NOT NULL,
    address VARCHAR(255) NULL,
    is_source_school BOOLEAN NOT NULL DEFAULT TRUE,
    is_admission_school BOOLEAN NOT NULL DEFAULT TRUE,
    active BOOLEAN NOT NULL DEFAULT TRUE,
    PRIMARY KEY (id),
    UNIQUE KEY uq_schools_name (name),
    UNIQUE KEY uq_schools_code (code)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS school_classes (
    id VARCHAR(64) NOT NULL,
    school_id VARCHAR(64) NOT NULL,
    name VARCHAR(100) NOT NULL,
    grade VARCHAR(60) NOT NULL,
    active BOOLEAN NOT NULL DEFAULT TRUE,
    PRIMARY KEY (id),
    UNIQUE KEY uq_classes_school_name (school_id, name),
    CONSTRAINT fk_classes_school FOREIGN KEY (school_id) REFERENCES schools(id) ON DELETE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS school_student_partitions (
    school_id VARCHAR(64) NOT NULL,
    partition_key VARCHAR(32) NOT NULL,
    students_table VARCHAR(64) NOT NULL,
    created_at VARCHAR(35) NOT NULL,
    updated_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (school_id),
    UNIQUE KEY uq_school_partitions_key (partition_key),
    UNIQUE KEY uq_school_partitions_table (students_table),
    CONSTRAINT fk_school_partitions_school FOREIGN KEY (school_id) REFERENCES schools(id) ON DELETE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS users (
    id VARCHAR(64) NOT NULL,
    username VARCHAR(100) NOT NULL,
    candidate_number VARCHAR(120) NULL,
    password_hash VARCHAR(255) NOT NULL,
    role ENUM('admin', 'candidate', 'admission_school') NOT NULL,
    admin_level ENUM('super', 'school', 'class') NULL,
    school_id VARCHAR(64) NULL,
    class_id VARCHAR(64) NULL,
    active BOOLEAN NOT NULL DEFAULT TRUE,
    must_change_password BOOLEAN NOT NULL DEFAULT FALSE,
    totp_enabled BOOLEAN NOT NULL DEFAULT FALSE,
    totp_secret_encrypted VARCHAR(512) NULL,
    totp_recovery_codes VARCHAR(2048) NOT NULL DEFAULT '[]',
    totp_last_used_step BIGINT NULL,
    archived_at VARCHAR(35) NULL,
    archived_by VARCHAR(64) NULL,
    display_name VARCHAR(100) NOT NULL,
    created_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uq_users_username (username),
    UNIQUE KEY uq_users_candidate_number (candidate_number),
    KEY idx_users_admin_scope (role, admin_level, school_id, class_id),
    KEY idx_users_archive_scope (role, school_id, class_id, archived_at),
    CONSTRAINT fk_users_school FOREIGN KEY (school_id) REFERENCES schools(id) ON DELETE SET NULL,
    CONSTRAINT fk_users_class FOREIGN KEY (class_id) REFERENCES school_classes(id) ON DELETE SET NULL,
    CONSTRAINT fk_users_archiver FOREIGN KEY (archived_by) REFERENCES users(id) ON DELETE RESTRICT
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS candidate_profiles (
    id VARCHAR(64) NOT NULL,
    user_id VARCHAR(64) NOT NULL,
    name VARCHAR(100) NOT NULL,
    gender VARCHAR(20) NULL,
    id_number VARCHAR(60) NOT NULL,
    phone VARCHAR(60) NOT NULL,
    email VARCHAR(160) NULL,
    school VARCHAR(160) NULL,
    grade VARCHAR(100) NULL,
    school_id VARCHAR(64) NULL,
    class_id VARCHAR(64) NULL,
    province_code VARCHAR(6) NULL,
    province_name VARCHAR(80) NULL,
    city_code VARCHAR(6) NULL,
    city_name VARCHAR(100) NULL,
    district_code VARCHAR(6) NULL,
    district_name VARCHAR(100) NULL,
    address VARCHAR(255) NULL,
    emergency_contact VARCHAR(100) NULL,
    emergency_phone VARCHAR(60) NULL,
    native_place VARCHAR(160) NULL,
    birth_date VARCHAR(20) NULL,
    ethnicity VARCHAR(60) NULL,
    postal_code VARCHAR(20) NULL,
    guardian_name VARCHAR(100) NULL,
    guardian_phone VARCHAR(60) NULL,
    specialty_category VARCHAR(30) NULL,
    specialty_type VARCHAR(40) NULL,
    specialty_types JSON NOT NULL DEFAULT (JSON_ARRAY()),
    specialty_certificate VARCHAR(255) NULL,
    policy_eligibility VARCHAR(255) NULL,
    profile_completed BOOLEAN NOT NULL DEFAULT FALSE,
    status ENUM('pending', 'approved', 'rejected') NOT NULL,
    review_note VARCHAR(500) NULL,
    reviewed_at VARCHAR(35) NULL,
    reviewer_id VARCHAR(64) NULL,
    updated_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uq_profiles_user (user_id),
    UNIQUE KEY uq_profiles_id_number (id_number),
    KEY idx_profiles_status (status),
    CONSTRAINT fk_profiles_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    CONSTRAINT fk_profiles_reviewer FOREIGN KEY (reviewer_id) REFERENCES users(id) ON DELETE SET NULL,
    CONSTRAINT fk_profiles_school FOREIGN KEY (school_id) REFERENCES schools(id) ON DELETE SET NULL,
    CONSTRAINT fk_profiles_class FOREIGN KEY (class_id) REFERENCES school_classes(id) ON DELETE SET NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS notices (
    id VARCHAR(64) NOT NULL,
    title VARCHAR(240) NOT NULL,
    summary VARCHAR(500) NOT NULL,
    content TEXT NOT NULL,
    category VARCHAR(60) NOT NULL,
    pinned BOOLEAN NOT NULL DEFAULT FALSE,
    status ENUM('draft', 'published') NOT NULL,
    publish_at VARCHAR(35) NULL,
    created_at VARCHAR(35) NULL,
    author VARCHAR(100) NOT NULL,
    PRIMARY KEY (id),
    KEY idx_notices_status_publish (status, publish_at)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS exams (
    id VARCHAR(64) NOT NULL,
    code VARCHAR(60) NOT NULL,
    name VARCHAR(200) NOT NULL,
    description TEXT NOT NULL,
    registration_start VARCHAR(35) NOT NULL,
    registration_end VARCHAR(35) NOT NULL,
    exam_start VARCHAR(35) NOT NULL,
    exam_end VARCHAR(35) NOT NULL,
    admit_download_start VARCHAR(35) NOT NULL,
    admit_download_end VARCHAR(35) NOT NULL,
    location VARCHAR(200) NOT NULL,
    pass_policy ENUM('fixed_score', 'score_ratio', 'rank_percent', 'subject_scores', 'none') NOT NULL DEFAULT 'rank_percent',
    pass_value DOUBLE NOT NULL DEFAULT 60,
    status ENUM('draft', 'published', 'closed') NOT NULL,
    archived_at VARCHAR(35) NULL,
    archived_by VARCHAR(64) NULL,
    created_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uq_exams_code (code),
    KEY idx_exams_status_registration (status, registration_start, registration_end),
    KEY idx_exams_archive (archived_at, exam_end),
    CONSTRAINT fk_exams_archived_by FOREIGN KEY (archived_by) REFERENCES users(id) ON DELETE RESTRICT
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS exam_data_partitions (
    exam_id VARCHAR(64) NOT NULL,
    partition_key VARCHAR(32) NOT NULL,
    candidates_table VARCHAR(64) NOT NULL,
    admissions_table VARCHAR(64) NOT NULL,
    results_table VARCHAR(64) NOT NULL,
    centers_table VARCHAR(64) NOT NULL,
    created_at VARCHAR(35) NOT NULL,
    updated_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (exam_id),
    UNIQUE KEY uq_exam_partitions_key (partition_key),
    UNIQUE KEY uq_exam_partitions_candidates (candidates_table),
    UNIQUE KEY uq_exam_partitions_admissions (admissions_table),
    UNIQUE KEY uq_exam_partitions_results (results_table),
    UNIQUE KEY uq_exam_partitions_centers (centers_table),
    CONSTRAINT fk_exam_partitions_exam FOREIGN KEY (exam_id) REFERENCES exams(id) ON DELETE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS exam_subjects (
    id VARCHAR(64) NOT NULL,
    exam_id VARCHAR(64) NOT NULL,
    name VARCHAR(100) NOT NULL,
    subject_date VARCHAR(35) NOT NULL,
    start_time VARCHAR(20) NOT NULL,
    end_time VARCHAR(20) NOT NULL,
    fee DOUBLE NOT NULL DEFAULT 0,
    full_score DOUBLE NOT NULL DEFAULT 150,
    pass_score DOUBLE NOT NULL DEFAULT 90,
    pass_rule ENUM('fixed_score', 'score_ratio', 'none') NOT NULL DEFAULT 'fixed_score',
    pass_value DOUBLE NOT NULL DEFAULT 90,
    position INT UNSIGNED NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uq_subjects_exam_position (exam_id, position),
    KEY idx_subjects_exam (exam_id, position),
    CONSTRAINT fk_subjects_exam FOREIGN KEY (exam_id) REFERENCES exams(id) ON DELETE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS registrations (
    id VARCHAR(64) NOT NULL,
    user_id VARCHAR(64) NOT NULL,
    exam_id VARCHAR(64) NOT NULL,
    status ENUM('pending', 'approved', 'rejected') NOT NULL,
    payment_status ENUM('unpaid', 'paid') NOT NULL,
    paid_at VARCHAR(35) NULL,
    paid_by VARCHAR(64) NULL,
    created_at VARCHAR(35) NOT NULL,
    reviewed_at VARCHAR(35) NULL,
    review_note VARCHAR(500) NULL,
    registration_number VARCHAR(120) NULL,
    number_rule_id VARCHAR(64) NULL,
    feature_score DECIMAL(8,2) NOT NULL DEFAULT 0,
    PRIMARY KEY (id),
    UNIQUE KEY uq_registrations_user_exam (user_id, exam_id),
    KEY idx_registrations_status (status),
    KEY idx_registrations_payment (payment_status, paid_at),
    KEY idx_registrations_exam (exam_id),
    CONSTRAINT fk_registrations_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    CONSTRAINT fk_registrations_exam FOREIGN KEY (exam_id) REFERENCES exams(id) ON DELETE CASCADE,
    CONSTRAINT fk_registrations_paid_by FOREIGN KEY (paid_by) REFERENCES users(id) ON DELETE SET NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS registration_subjects (
    registration_id VARCHAR(64) NOT NULL,
    subject_id VARCHAR(64) NOT NULL,
    PRIMARY KEY (registration_id, subject_id),
    CONSTRAINT fk_registration_subjects_registration FOREIGN KEY (registration_id) REFERENCES registrations(id) ON DELETE CASCADE,
    CONSTRAINT fk_registration_subjects_subject FOREIGN KEY (subject_id) REFERENCES exam_subjects(id) ON DELETE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS results (
    id VARCHAR(64) NOT NULL,
    registration_id VARCHAR(64) NOT NULL,
    subject_id VARCHAR(64) NOT NULL,
    score DOUBLE NOT NULL,
    grade VARCHAR(20) NOT NULL,
    published BOOLEAN NOT NULL DEFAULT FALSE,
    updated_at VARCHAR(35) NULL,
    published_at VARCHAR(35) NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uq_results_registration_subject (registration_id, subject_id),
    KEY idx_results_registration (registration_id, published),
    CONSTRAINT chk_results_score CHECK (score >= 0 AND score <= 150),
    CONSTRAINT fk_results_registration FOREIGN KEY (registration_id) REFERENCES registrations(id) ON DELETE CASCADE,
    CONSTRAINT fk_results_subject FOREIGN KEY (subject_id) REFERENCES exam_subjects(id) ON DELETE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS result_statistics (
    result_id VARCHAR(64) NOT NULL,
    subject_rank INT NULL,
    cohort_size INT UNSIGNED NOT NULL DEFAULT 0,
    rank_percent DOUBLE NULL,
    grade VARCHAR(20) NOT NULL,
    qualified BOOLEAN NULL,
    pass_score DOUBLE NULL,
    cutoff_rank INT NULL,
    calculated_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (result_id),
    CONSTRAINT fk_result_statistics_result FOREIGN KEY (result_id) REFERENCES results(id) ON DELETE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS registration_result_summaries (
    registration_id VARCHAR(64) NOT NULL,
    exam_id VARCHAR(64) NOT NULL,
    published_subject_count INT UNSIGNED NOT NULL DEFAULT 0,
    subject_count INT UNSIGNED NOT NULL DEFAULT 0,
    complete BOOLEAN NOT NULL DEFAULT FALSE,
    total_score DOUBLE NOT NULL DEFAULT 0,
    full_score DOUBLE NOT NULL DEFAULT 0,
    score_ratio DOUBLE NOT NULL DEFAULT 0,
    total_rank INT NULL,
    cohort_size INT UNSIGNED NOT NULL DEFAULT 0,
    rank_percent DOUBLE NULL,
    qualified BOOLEAN NULL,
    cutoff_rank INT NULL,
    calculated_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (registration_id),
    KEY idx_result_summaries_exam (exam_id, complete, qualified),
    CONSTRAINT fk_result_summaries_registration FOREIGN KEY (registration_id) REFERENCES registrations(id) ON DELETE CASCADE,
    CONSTRAINT fk_result_summaries_exam FOREIGN KEY (exam_id) REFERENCES exams(id) ON DELETE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS test_centers (
    id VARCHAR(64) NOT NULL,
    school_id VARCHAR(64) NOT NULL,
    code VARCHAR(40) NOT NULL,
    name VARCHAR(200) NOT NULL,
    province_code VARCHAR(6) NOT NULL,
    province_name VARCHAR(80) NOT NULL,
    city_code VARCHAR(6) NOT NULL,
    city_name VARCHAR(100) NOT NULL,
    district_code VARCHAR(6) NOT NULL,
    district_name VARCHAR(100) NOT NULL,
    address VARCHAR(255) NOT NULL,
    contact VARCHAR(100) NULL,
    manager_name VARCHAR(100) NULL,
    manager_phone VARCHAR(60) NULL,
    emergency_phone VARCHAR(60) NULL,
    gate_open_time VARCHAR(40) NULL,
    transport VARCHAR(500) NULL,
    status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
    notes VARCHAR(1000) NULL,
    rooms TEXT NOT NULL,
    updated_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uq_centers_code (code),
    UNIQUE KEY uq_centers_school_name (school_id, name),
    CONSTRAINT fk_centers_school FOREIGN KEY (school_id) REFERENCES schools(id) ON DELETE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS test_rooms (
    id VARCHAR(64) NOT NULL,
    center_id VARCHAR(64) NOT NULL,
    code VARCHAR(40) NOT NULL,
    name VARCHAR(120) NOT NULL,
    building VARCHAR(120) NOT NULL,
    floor VARCHAR(40) NULL,
    capacity INT UNSIGNED NOT NULL,
    seat_plan VARCHAR(500) NULL,
    seat_start INT UNSIGNED NOT NULL DEFAULT 1,
    seat_end INT UNSIGNED NOT NULL,
    room_type ENUM('standard', 'computer', 'accessible', 'spare') NOT NULL,
    status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
    notes VARCHAR(500) NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uq_rooms_center_code (center_id, code),
    KEY idx_rooms_center (center_id, status, code),
    CONSTRAINT fk_rooms_center FOREIGN KEY (center_id) REFERENCES test_centers(id) ON DELETE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS admission_number_rules (
    id VARCHAR(64) NOT NULL,
    code VARCHAR(80) NOT NULL,
    name VARCHAR(160) NOT NULL,
    description VARCHAR(500) NOT NULL,
    `separator` VARCHAR(10) NOT NULL DEFAULT '',
    segments_json JSON NOT NULL,
    example VARCHAR(120) NOT NULL,
    active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uq_admission_rules_code (code)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS exam_arrangement_plans (
    id VARCHAR(64) NOT NULL,
    exam_id VARCHAR(64) NOT NULL,
    number_rule_id VARCHAR(64) NOT NULL,
    mixing_scope ENUM('class', 'school', 'district', 'city', 'province') NOT NULL,
    random_seed VARCHAR(80) NOT NULL,
    candidate_count INT UNSIGNED NOT NULL,
    center_count INT UNSIGNED NOT NULL,
    subject_assignment_count INT UNSIGNED NOT NULL,
    subject_combination_count INT UNSIGNED NOT NULL,
    same_school_center_rate DOUBLE NOT NULL,
    warnings_json JSON NOT NULL,
    generated_by VARCHAR(64) NULL,
    generated_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uq_arrangement_plans_exam (exam_id),
    CONSTRAINT fk_arrangement_plans_exam FOREIGN KEY (exam_id) REFERENCES exams(id) ON DELETE CASCADE,
    CONSTRAINT fk_arrangement_plans_rule FOREIGN KEY (number_rule_id) REFERENCES admission_number_rules(id),
    CONSTRAINT fk_arrangement_plans_generator FOREIGN KEY (generated_by) REFERENCES users(id) ON DELETE SET NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS admit_cards (
    registration_id VARCHAR(64) NOT NULL,
    plan_id VARCHAR(64) NOT NULL,
    card_number VARCHAR(100) NOT NULL,
    center_id VARCHAR(64) NULL,
    test_center VARCHAR(200) NOT NULL,
    center_code VARCHAR(40) NOT NULL,
    center_address VARCHAR(500) NOT NULL,
    generated_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (registration_id),
    UNIQUE KEY uq_admit_cards_number (card_number),
    CONSTRAINT fk_admit_cards_registration FOREIGN KEY (registration_id) REFERENCES registrations(id) ON DELETE CASCADE,
    CONSTRAINT fk_admit_cards_plan FOREIGN KEY (plan_id) REFERENCES exam_arrangement_plans(id) ON DELETE CASCADE,
    CONSTRAINT fk_admit_cards_center FOREIGN KEY (center_id) REFERENCES test_centers(id) ON DELETE SET NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS admit_card_subjects (
    registration_id VARCHAR(64) NOT NULL,
    subject_id VARCHAR(64) NOT NULL,
    room_id VARCHAR(64) NULL,
    room VARCHAR(120) NOT NULL,
    room_code VARCHAR(40) NOT NULL,
    exam_room_code VARCHAR(20) NOT NULL,
    building VARCHAR(120) NOT NULL,
    floor VARCHAR(80) NOT NULL,
    seat VARCHAR(30) NOT NULL,
    subject_signature VARCHAR(1000) NOT NULL,
    PRIMARY KEY (registration_id, subject_id),
    UNIQUE KEY uq_admit_subject_room_seat (subject_id, room_id, seat),
    KEY idx_admit_subjects_room (subject_id, room_id, seat),
    CONSTRAINT fk_admit_subjects_card FOREIGN KEY (registration_id) REFERENCES admit_cards(registration_id) ON DELETE CASCADE,
    CONSTRAINT fk_admit_subjects_subject FOREIGN KEY (subject_id) REFERENCES exam_subjects(id) ON DELETE CASCADE,
    CONSTRAINT fk_admit_subjects_room FOREIGN KEY (room_id) REFERENCES test_rooms(id) ON DELETE SET NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS center_change_requests (
    id VARCHAR(64) NOT NULL,
    center_id VARCHAR(64) NULL,
    school_id VARCHAR(64) NOT NULL,
    request_type ENUM('create', 'update') NOT NULL,
    code VARCHAR(40) NOT NULL,
    name VARCHAR(200) NOT NULL,
    province_code VARCHAR(6) NOT NULL,
    province_name VARCHAR(80) NOT NULL,
    city_code VARCHAR(6) NOT NULL,
    city_name VARCHAR(100) NOT NULL,
    district_code VARCHAR(6) NOT NULL,
    district_name VARCHAR(100) NOT NULL,
    address VARCHAR(255) NOT NULL,
    contact VARCHAR(100) NULL,
    manager_name VARCHAR(100) NULL,
    manager_phone VARCHAR(60) NULL,
    emergency_phone VARCHAR(60) NULL,
    gate_open_time VARCHAR(40) NULL,
    transport VARCHAR(500) NULL,
    center_status ENUM('active', 'inactive') NOT NULL,
    notes VARCHAR(1000) NULL,
    status ENUM('pending', 'approved', 'rejected') NOT NULL,
    review_note VARCHAR(500) NULL,
    requested_by VARCHAR(64) NULL,
    created_at VARCHAR(35) NOT NULL,
    reviewed_at VARCHAR(35) NULL,
    PRIMARY KEY (id),
    KEY idx_center_changes_school (school_id, status, created_at),
    CONSTRAINT fk_center_change_center FOREIGN KEY (center_id) REFERENCES test_centers(id) ON DELETE SET NULL,
    CONSTRAINT fk_center_change_school FOREIGN KEY (school_id) REFERENCES schools(id) ON DELETE CASCADE,
    CONSTRAINT fk_center_change_requester FOREIGN KEY (requested_by) REFERENCES users(id) ON DELETE SET NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS center_change_rooms (
    id VARCHAR(64) NOT NULL,
    request_id VARCHAR(64) NOT NULL,
    room_id VARCHAR(64) NULL,
    code VARCHAR(40) NOT NULL,
    name VARCHAR(120) NOT NULL,
    building VARCHAR(120) NOT NULL,
    floor VARCHAR(40) NULL,
    capacity INT UNSIGNED NOT NULL,
    seat_plan VARCHAR(500) NULL,
    seat_start INT UNSIGNED NOT NULL DEFAULT 1,
    seat_end INT UNSIGNED NOT NULL,
    room_type ENUM('standard', 'computer', 'accessible', 'spare') NOT NULL,
    status ENUM('active', 'inactive') NOT NULL,
    notes VARCHAR(500) NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uq_center_change_rooms_code (request_id, code),
    CONSTRAINT fk_center_change_rooms_request FOREIGN KEY (request_id) REFERENCES center_change_requests(id) ON DELETE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS number_rules (
    id VARCHAR(64) NOT NULL,
    name VARCHAR(120) NOT NULL,
    `separator` VARCHAR(10) NOT NULL DEFAULT '',
    active BOOLEAN NOT NULL DEFAULT FALSE,
    created_by VARCHAR(64) NULL,
    updated_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (id),
    CONSTRAINT fk_number_rules_creator FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS number_rule_segments (
    id VARCHAR(64) NOT NULL,
    rule_id VARCHAR(64) NOT NULL,
    position INT UNSIGNED NOT NULL,
    type ENUM('year', 'school_code', 'gender', 'sequence', 'literal') NOT NULL,
    value VARCHAR(60) NULL,
    width INT UNSIGNED NOT NULL DEFAULT 0,
    PRIMARY KEY (id),
    UNIQUE KEY uq_rule_segments_position (rule_id, position),
    CONSTRAINT fk_rule_segments_rule FOREIGN KEY (rule_id) REFERENCES number_rules(id) ON DELETE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS candidate_account_batches (
    id VARCHAR(64) NOT NULL,
    school_id VARCHAR(64) NOT NULL,
    requested_by VARCHAR(64) NULL,
    status ENUM('pending', 'approved', 'rejected') NOT NULL,
    review_note VARCHAR(500) NULL,
    created_at VARCHAR(35) NOT NULL,
    reviewed_at VARCHAR(35) NULL,
    PRIMARY KEY (id),
    KEY idx_account_batches_school (school_id, status, created_at),
    CONSTRAINT fk_account_batches_school FOREIGN KEY (school_id) REFERENCES schools(id) ON DELETE CASCADE,
    CONSTRAINT fk_account_batches_requester FOREIGN KEY (requested_by) REFERENCES users(id) ON DELETE SET NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS candidate_account_batch_items (
    id VARCHAR(64) NOT NULL,
    batch_id VARCHAR(64) NOT NULL,
    class_id VARCHAR(64) NOT NULL,
    position INT UNSIGNED NOT NULL,
    candidate_number VARCHAR(120) NULL,
    initial_password VARCHAR(120) NULL,
    user_id VARCHAR(64) NULL,
    created_at VARCHAR(35) NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uq_account_batch_position (batch_id, position),
    UNIQUE KEY uq_account_batch_number (candidate_number),
    UNIQUE KEY uq_account_batch_user (user_id),
    KEY idx_account_batch_items (batch_id, class_id, position),
    CONSTRAINT fk_account_batch_items_batch FOREIGN KEY (batch_id) REFERENCES candidate_account_batches(id) ON DELETE CASCADE,
    CONSTRAINT fk_account_batch_items_class FOREIGN KEY (class_id) REFERENCES school_classes(id),
    CONSTRAINT fk_account_batch_items_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS workflow_definitions (
    id VARCHAR(64) NOT NULL,
    business_type ENUM('profile_change', 'registration_review', 'center_change', 'candidate_account_batch', 'score_appeal') NOT NULL,
    name VARCHAR(120) NOT NULL,
    active BOOLEAN NOT NULL DEFAULT TRUE,
    updated_by VARCHAR(64) NULL,
    updated_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uq_workflow_type_active (business_type, active),
    CONSTRAINT fk_workflow_updater FOREIGN KEY (updated_by) REFERENCES users(id) ON DELETE SET NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS workflow_steps (
    id VARCHAR(64) NOT NULL,
    workflow_id VARCHAR(64) NOT NULL,
    position INT UNSIGNED NOT NULL,
    name VARCHAR(120) NOT NULL,
    admin_level ENUM('class', 'school', 'super') NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uq_workflow_steps_position (workflow_id, position),
    CONSTRAINT fk_workflow_steps_definition FOREIGN KEY (workflow_id) REFERENCES workflow_definitions(id) ON DELETE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS workflow_instances (
    id VARCHAR(64) NOT NULL,
    workflow_id VARCHAR(64) NOT NULL,
    business_type VARCHAR(40) NOT NULL,
    business_id VARCHAR(64) NOT NULL,
    status ENUM('pending', 'approved', 'rejected') NOT NULL,
    current_step INT UNSIGNED NOT NULL DEFAULT 1,
    assignee_id VARCHAR(64) NULL,
    created_at VARCHAR(35) NOT NULL,
    completed_at VARCHAR(35) NULL,
    PRIMARY KEY (id),
    KEY idx_workflow_inbox (status, assignee_id, business_type),
    CONSTRAINT fk_workflow_instance_definition FOREIGN KEY (workflow_id) REFERENCES workflow_definitions(id),
    CONSTRAINT fk_workflow_instance_assignee FOREIGN KEY (assignee_id) REFERENCES users(id) ON DELETE SET NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS workflow_actions (
    id VARCHAR(64) NOT NULL,
    instance_id VARCHAR(64) NOT NULL,
    actor_id VARCHAR(64) NULL,
    action ENUM('submit', 'approve', 'reject', 'transfer', 'return', 'supervise') NOT NULL,
    note VARCHAR(500) NULL,
    from_assignee_id VARCHAR(64) NULL,
    to_assignee_id VARCHAR(64) NULL,
    created_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (id),
    KEY idx_workflow_actions_instance (instance_id, created_at),
    CONSTRAINT fk_workflow_action_instance FOREIGN KEY (instance_id) REFERENCES workflow_instances(id) ON DELETE CASCADE,
    CONSTRAINT fk_workflow_action_actor FOREIGN KEY (actor_id) REFERENCES users(id) ON DELETE SET NULL,
    CONSTRAINT fk_workflow_action_from FOREIGN KEY (from_assignee_id) REFERENCES users(id) ON DELETE SET NULL,
    CONSTRAINT fk_workflow_action_to FOREIGN KEY (to_assignee_id) REFERENCES users(id) ON DELETE SET NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS admission_records (
    id VARCHAR(64) NOT NULL,
    kind ENUM('setting', 'plan', 'preference', 'placement', 'notification', 'indicator_qualification', 'qualification_publication', 'cutoff_publication') NOT NULL,
    exam_id VARCHAR(64) NOT NULL,
    user_id VARCHAR(64) NULL,
    school_id VARCHAR(64) NULL,
    status VARCHAR(40) NOT NULL,
    payload_json JSON NOT NULL,
    created_at VARCHAR(35) NOT NULL,
    updated_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (id),
    KEY idx_admission_records_lookup (kind, exam_id, school_id, user_id, status),
    CONSTRAINT fk_admission_record_exam FOREIGN KEY (exam_id) REFERENCES exams(id) ON DELETE CASCADE,
    CONSTRAINT fk_admission_record_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    CONSTRAINT fk_admission_record_school FOREIGN KEY (school_id) REFERENCES schools(id) ON DELETE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
-- EIS-STATEMENT
CREATE TABLE IF NOT EXISTS audit_logs (
    id VARCHAR(64) NOT NULL,
    actor_id VARCHAR(64) NULL,
    action VARCHAR(100) NOT NULL,
    detail VARCHAR(1000) NOT NULL,
    created_at VARCHAR(35) NOT NULL,
    PRIMARY KEY (id),
    KEY idx_audit_created (created_at),
    CONSTRAINT fk_audit_actor FOREIGN KEY (actor_id) REFERENCES users(id) ON DELETE SET NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
