-- ============================================================================
-- LEOTRIX IDEMPOTENT SCHEMA SYNC
-- Generated from: leotwcun_leotrix.sql
-- Source server: MariaDB 11.4.13
--
-- What this file does:
--   * CREATE TABLE IF NOT EXISTS for every table in the dump.
--   * ADD COLUMN IF NOT EXISTS for every column in every table.
--   * ADD missing primary/unique/normal indexes with IF NOT EXISTS.
--   * Re-apply AUTO_INCREMENT attributes using MODIFY COLUMN IF EXISTS.
--   * ADD missing foreign keys with IF NOT EXISTS.
--
-- What this file does NOT do:
--   * It does not DROP tables or columns.
--   * It does not TRUNCATE or DELETE rows.
--   * It does not import the INSERT/data section from the dump.
--
-- IMPORTANT: Back up a production database before any schema migration.
-- ============================================================================

-- Uncomment if the selected database should be the dump database:
-- USE `leotwcun_leotrix`;

SET @LEOTRIX_OLD_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS;
SET FOREIGN_KEY_CHECKS = 0;

-- ============================================================================
-- 1) CREATE MISSING TABLES
-- ============================================================================

-- `academic_sessions`
CREATE TABLE IF NOT EXISTS `academic_sessions` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `session_year` varchar(20) NOT NULL COMMENT 'e.g. 2025/2026',
  `term` enum('1st','2nd','3rd') NOT NULL,
  `status` tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
  `app_view` int(11) DEFAULT NULL,
  `start_date` date DEFAULT NULL,
  `end_date` date DEFAULT NULL,
  `closing` varchar(150) DEFAULT NULL,
  `open` varchar(150) DEFAULT NULL,
  `no_open` int(11) NOT NULL DEFAULT 0,
  `no_opening` int(11) NOT NULL,
  `is_current` tinyint(1) NOT NULL DEFAULT 0 COMMENT '1 = currently active session/term',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `activity_participation`
CREATE TABLE IF NOT EXISTS `activity_participation` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `activity_id` bigint(20) UNSIGNED NOT NULL,
  `join_date` date NOT NULL,
  `status` varchar(50) DEFAULT 'active',
  `participation_level` varchar(100) DEFAULT NULL,
  `achievements` text DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `announcements`
CREATE TABLE IF NOT EXISTS `announcements` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `title` varchar(200) NOT NULL,
  `content` text NOT NULL,
  `type` enum('general','class','emergency','event') NOT NULL DEFAULT 'general',
  `target_class_id` bigint(20) UNSIGNED DEFAULT NULL,
  `priority` enum('low','medium','high') DEFAULT 'medium',
  `created_by` bigint(20) UNSIGNED NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `expires_at` datetime DEFAULT NULL,
  `status` tinyint(1) UNSIGNED NOT NULL DEFAULT 1,
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `app_settings`
CREATE TABLE IF NOT EXISTS `app_settings` (
  `id` int(11) NOT NULL,
  `setting_key` varchar(255) DEFAULT NULL,
  `setting_value` decimal(10,2) DEFAULT NULL,
  `download_link` varchar(500) DEFAULT NULL,
  `update_text` text DEFAULT NULL,
  `description` text DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

-- `assignments`
CREATE TABLE IF NOT EXISTS `assignments` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `class_id` bigint(20) UNSIGNED NOT NULL,
  `subject_id` bigint(20) UNSIGNED NOT NULL,
  `teacher_id` bigint(20) UNSIGNED NOT NULL,
  `title` varchar(200) NOT NULL,
  `description` text DEFAULT NULL,
  `body` text NOT NULL,
  `instructions` text DEFAULT NULL,
  `assigned_date` date NOT NULL,
  `due_date` date NOT NULL,
  `due_time` time DEFAULT NULL,
  `assignment_type` varchar(50) DEFAULT 'homework',
  `pdf` varchar(1200) NOT NULL,
  `word` varchar(1200) NOT NULL,
  `total_marks` decimal(5,2) DEFAULT 0.00,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `assignment_submissions`
CREATE TABLE IF NOT EXISTS `assignment_submissions` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `assignment_id` bigint(20) UNSIGNED NOT NULL,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `submission_date` date NOT NULL,
  `submission_time` time NOT NULL,
  `submission_content` text DEFAULT NULL,
  `submission_status` varchar(50) DEFAULT 'pending',
  `marks_obtained` decimal(5,2) DEFAULT NULL,
  `teacher_feedback` text DEFAULT NULL,
  `graded_at` datetime DEFAULT NULL,
  `graded_by` bigint(20) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `attendance`
CREATE TABLE IF NOT EXISTS `attendance` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `class_id` int(11) NOT NULL,
  `session_id` int(11) DEFAULT NULL,
  `session_year` varchar(20) NOT NULL DEFAULT '2025/2026',
  `term` varchar(10) NOT NULL DEFAULT '1st',
  `attendance_date` date NOT NULL,
  `status` enum('present','absent','late','excused') NOT NULL,
  `reason` text DEFAULT NULL,
  `marked_by` bigint(20) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `latitude` decimal(10,8) DEFAULT NULL COMMENT 'GPS latitude coordinate',
  `longitude` decimal(11,8) DEFAULT NULL COMMENT 'GPS longitude coordinate',
  `accuracy` decimal(8,2) DEFAULT NULL COMMENT 'GPS accuracy in meters',
  `location_type` enum('school','pickup','dropoff') DEFAULT NULL COMMENT 'Type of location: school, pickup, or dropoff',
  `distance_from_school` decimal(10,2) DEFAULT NULL COMMENT 'Distance from school geofence in meters'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `bursary_activity`
CREATE TABLE IF NOT EXISTS `bursary_activity` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `action` varchar(40) NOT NULL,
  `record_id` bigint(20) NOT NULL,
  `performed_by_id` bigint(20) UNSIGNED NOT NULL,
  `performed_by` varchar(255) NOT NULL,
  `snapshot` longtext NOT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- `bursary_discounts`
CREATE TABLE IF NOT EXISTS `bursary_discounts` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(150) NOT NULL,
  `description` text NOT NULL,
  `kind` enum('predefined','custom') NOT NULL,
  `value_type` enum('percentage','fixed') NOT NULL,
  `value` decimal(12,2) NOT NULL,
  `student_id` bigint(20) UNSIGNED DEFAULT NULL,
  `applies_to` enum('normal','other') NOT NULL,
  `session_id` bigint(20) UNSIGNED DEFAULT NULL,
  `class_id` bigint(20) UNSIGNED DEFAULT NULL,
  `category` varchar(150) NOT NULL DEFAULT '',
  `other_payment_item_id` bigint(20) UNSIGNED DEFAULT NULL,
  `starts_at` datetime DEFAULT NULL,
  `expires_at` datetime DEFAULT NULL,
  `is_active` tinyint(4) NOT NULL DEFAULT 1,
  `version` int(11) NOT NULL DEFAULT 1,
  `created_by` bigint(20) UNSIGNED NOT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp()
) ;

-- `bursary_family_payments`
CREATE TABLE IF NOT EXISTS `bursary_family_payments` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `receipt_number` varchar(100) NOT NULL,
  `session_id` varchar(20) NOT NULL,
  `payment_date` date NOT NULL,
  `payment_method` varchar(40) NOT NULL,
  `amount_paid` decimal(14,2) NOT NULL,
  `received_by_id` bigint(20) UNSIGNED NOT NULL,
  `received_by` varchar(255) NOT NULL,
  `receipt_snapshot` longtext NOT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp()
) ;

-- `bursary_family_payment_lines`
CREATE TABLE IF NOT EXISTS `bursary_family_payment_lines` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `family_payment_id` bigint(20) UNSIGNED NOT NULL,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `fee_id` bigint(20) UNSIGNED NOT NULL,
  `payment_id` bigint(20) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- `bursary_requests`
CREATE TABLE IF NOT EXISTS `bursary_requests` (
  `actor_id` bigint(20) UNSIGNED NOT NULL,
  `request_id` varchar(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
  `request_hash` char(64) NOT NULL,
  `result` longtext DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- `cbt_answers`
CREATE TABLE IF NOT EXISTS `cbt_answers` (
  `id` int(11) NOT NULL,
  `test_id` int(11) NOT NULL,
  `student_id` int(11) NOT NULL,
  `question_id` int(11) NOT NULL,
  `question_order` int(11) NOT NULL DEFAULT 0,
  `answer` text NOT NULL COMMENT 'Student answer - can be JSON for multiple selections',
  `is_correct` tinyint(1) DEFAULT NULL,
  `marks_obtained` decimal(5,2) DEFAULT 0.00,
  `submitted_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `cbt_contexts`
CREATE TABLE IF NOT EXISTS `cbt_contexts` (
  `id` int(11) NOT NULL,
  `test_id` int(11) NOT NULL,
  `client_id` varchar(100) NOT NULL,
  `title` varchar(255) DEFAULT NULL,
  `passage_text` text DEFAULT NULL,
  `context_image` longtext DEFAULT NULL,
  `order_index` int(11) NOT NULL DEFAULT 0,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `cbt_questions`
CREATE TABLE IF NOT EXISTS `cbt_questions` (
  `id` int(11) NOT NULL,
  `test_id` int(11) NOT NULL,
  `context_id` int(11) DEFAULT NULL,
  `question_text` text NOT NULL,
  `question_type` enum('multiple_choice','true_false','short_answer') NOT NULL DEFAULT 'multiple_choice',
  `question_image` longtext DEFAULT NULL,
  `option_a` text DEFAULT NULL,
  `option_b` text DEFAULT NULL,
  `option_c` text DEFAULT NULL,
  `option_d` text DEFAULT NULL,
  `correct_answer` varchar(10) NOT NULL COMMENT 'A, B, C, D for multiple choice, or actual answer text',
  `marks` decimal(5,2) NOT NULL DEFAULT 1.00,
  `order_index` int(11) NOT NULL DEFAULT 0,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `cbt_results`
CREATE TABLE IF NOT EXISTS `cbt_results` (
  `id` int(11) NOT NULL,
  `test_id` int(11) NOT NULL,
  `student_id` int(11) NOT NULL,
  `total_questions` int(11) NOT NULL,
  `attempted_questions` int(11) NOT NULL DEFAULT 0,
  `correct_answers` int(11) NOT NULL DEFAULT 0,
  `total_marks` decimal(7,2) NOT NULL DEFAULT 0.00,
  `marks_obtained` decimal(7,2) NOT NULL DEFAULT 0.00,
  `percentage` decimal(5,2) NOT NULL DEFAULT 0.00,
  `grade` varchar(5) DEFAULT NULL,
  `status` enum('pending','completed','graded') NOT NULL DEFAULT 'pending',
  `submitted_at` timestamp NULL DEFAULT NULL,
  `graded_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `cbt_tests`
CREATE TABLE IF NOT EXISTS `cbt_tests` (
  `id` int(11) NOT NULL,
  `test_title` varchar(255) NOT NULL,
  `description` text DEFAULT NULL,
  `teacher_id` int(11) NOT NULL,
  `subject_id` int(11) NOT NULL,
  `class_id` int(11) NOT NULL,
  `class_ids` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT 'JSON array of class IDs this test serves' CHECK (json_valid(`class_ids`)),
  `session_id` int(11) NOT NULL,
  `student_id` int(11) DEFAULT NULL COMMENT 'NULL for class-wide tests, specific student ID for individual tests',
  `duration_minutes` int(11) NOT NULL DEFAULT 60,
  `total_questions` int(11) NOT NULL DEFAULT 0,
  `display_questions` int(11) DEFAULT NULL,
  `passing_score` decimal(5,2) NOT NULL DEFAULT 50.00,
  `status` enum('draft','active','completed','archived') NOT NULL DEFAULT 'draft',
  `test_type` enum('class','subject') DEFAULT 'class' COMMENT 'class = single class, subject = multi-class',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `start_date` datetime DEFAULT NULL,
  `end_date` datetime DEFAULT NULL,
  `sync_status` varchar(50) DEFAULT 'synced' COMMENT 'Sync status: synced (default) | local (pending push) | conflict (resolved locally)',
  `local_modified_at` timestamp NULL DEFAULT NULL COMMENT 'Timestamp when test was last modified in offline mode. Used for conflict detection.',
  `sync_source` varchar(50) DEFAULT NULL COMMENT 'Indicates record origin during sync: local | online | null (both sources)'
) ;

-- `cbt_test_assignments`
CREATE TABLE IF NOT EXISTS `cbt_test_assignments` (
  `id` int(11) NOT NULL,
  `test_id` int(11) NOT NULL,
  `student_id` int(11) NOT NULL,
  `assigned_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `accessed_at` timestamp NULL DEFAULT NULL,
  `completed_at` timestamp NULL DEFAULT NULL,
  `status` enum('assigned','accessed','completed') NOT NULL DEFAULT 'assigned',
  `sync_source` varchar(50) DEFAULT 'online' COMMENT 'Record origin: online (default) | local (created offline)',
  `created_offline_at` timestamp NULL DEFAULT NULL COMMENT 'Timestamp when assignment was created in offline mode'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `cbt_test_classes`
CREATE TABLE IF NOT EXISTS `cbt_test_classes` (
  `id` int(11) NOT NULL,
  `test_id` int(11) NOT NULL,
  `class_id` int(11) NOT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `classes`
CREATE TABLE IF NOT EXISTS `classes` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `section_id` bigint(20) UNSIGNED DEFAULT NULL,
  `class_name` varchar(50) NOT NULL,
  `section` varchar(20) DEFAULT NULL,
  `class_teacher_id` bigint(20) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `discipline_records`
CREATE TABLE IF NOT EXISTS `discipline_records` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `incident_date` date NOT NULL,
  `incident_type` varchar(100) DEFAULT NULL,
  `severity` varchar(50) DEFAULT 'minor',
  `description` text DEFAULT NULL,
  `action_taken` text DEFAULT NULL,
  `reported_by` bigint(20) UNSIGNED NOT NULL,
  `parent_notified` tinyint(1) DEFAULT 0,
  `notification_date` date DEFAULT NULL,
  `status` varchar(50) DEFAULT 'pending_action',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `events`
CREATE TABLE IF NOT EXISTS `events` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `event_name` varchar(200) NOT NULL,
  `event_description` text DEFAULT NULL,
  `event_type` varchar(50) DEFAULT 'general',
  `session_year` varchar(20) NOT NULL DEFAULT '2025/2026',
  `term` varchar(10) DEFAULT NULL,
  `event_date` date NOT NULL,
  `start_time` time DEFAULT NULL,
  `end_time` time DEFAULT NULL,
  `location` varchar(255) DEFAULT NULL,
  `target_class_id` bigint(20) UNSIGNED DEFAULT NULL,
  `created_by` bigint(20) UNSIGNED NOT NULL,
  `is_public` tinyint(1) DEFAULT 1,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `extracurricular_activities`
CREATE TABLE IF NOT EXISTS `extracurricular_activities` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `activity_name` varchar(100) NOT NULL,
  `activity_type` varchar(50) DEFAULT NULL,
  `description` text DEFAULT NULL,
  `start_date` date DEFAULT NULL,
  `end_date` date DEFAULT NULL,
  `meeting_day` varchar(20) DEFAULT NULL,
  `meeting_time` time DEFAULT NULL,
  `location` varchar(255) DEFAULT NULL,
  `coordinator_id` bigint(20) UNSIGNED NOT NULL,
  `session_year` varchar(20) DEFAULT '2025/2026',
  `is_active` tinyint(1) DEFAULT 1,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `fees`
CREATE TABLE IF NOT EXISTS `fees` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `session_id` varchar(20) NOT NULL,
  `student_type` enum('0','1') NOT NULL,
  `amount_due` decimal(12,2) NOT NULL,
  `amount_paid` decimal(12,2) DEFAULT 0.00,
  `balance` decimal(12,2) GENERATED ALWAYS AS (`amount_due` - `amount_paid`) STORED,
  `due_date` date DEFAULT NULL,
  `fee_items` varchar(200) NOT NULL,
  `payment_date` date DEFAULT NULL,
  `payment_method` varchar(50) DEFAULT NULL,
  `receipt_number` varchar(50) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `feess`
CREATE TABLE IF NOT EXISTS `feess` (
  `regno` int(11) NOT NULL,
  `name` varchar(900) NOT NULL,
  `grade` varchar(900) NOT NULL,
  `vocational` varchar(900) NOT NULL,
  `uniform` varchar(900) NOT NULL,
  `clubsport` varchar(900) NOT NULL,
  `practical` varchar(900) NOT NULL,
  `ictfee` varchar(900) NOT NULL,
  `xmas` varchar(900) NOT NULL,
  `caution` varchar(900) NOT NULL,
  `graduation` varchar(900) NOT NULL,
  `pastquestion` varchar(900) NOT NULL,
  `externalexam` varchar(900) NOT NULL,
  `igsce` varchar(900) NOT NULL,
  `1stterm` varchar(900) NOT NULL,
  `2ndterm` varchar(900) NOT NULL,
  `3rdterm` varchar(900) NOT NULL,
  `total` varchar(900) NOT NULL,
  `nbn` varchar(900) NOT NULL,
  `nbna` varchar(900) NOT NULL,
  `w2` varchar(900) NOT NULL,
  `w3` varchar(900) NOT NULL,
  `w4` varchar(900) NOT NULL,
  `w22` varchar(900) NOT NULL,
  `w33` varchar(900) NOT NULL,
  `w44` varchar(900) NOT NULL,
  `w222` varchar(900) NOT NULL,
  `w333` varchar(900) NOT NULL,
  `w444` varchar(900) NOT NULL,
  `w122` varchar(900) NOT NULL,
  `w212` varchar(900) NOT NULL,
  `w233` varchar(900) NOT NULL,
  `w244` varchar(900) NOT NULL,
  `w344` varchar(900) NOT NULL,
  `w3444` varchar(900) NOT NULL,
  `w544` varchar(900) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

-- `fee_charge_details`
CREATE TABLE IF NOT EXISTS `fee_charge_details` (
  `fee_id` bigint(20) UNSIGNED NOT NULL,
  `charge_kind` enum('normal','other') NOT NULL,
  `title` varchar(150) NOT NULL,
  `description` text NOT NULL,
  `category` varchar(150) NOT NULL DEFAULT '',
  `class_id` bigint(20) UNSIGNED DEFAULT NULL,
  `student_name` varchar(255) NOT NULL,
  `items_snapshot` longtext NOT NULL,
  `created_by` bigint(20) UNSIGNED NOT NULL,
  `created_by_name` varchar(255) NOT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- `fee_discounts`
CREATE TABLE IF NOT EXISTS `fee_discounts` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `fee_id` bigint(20) UNSIGNED NOT NULL,
  `discount_id` bigint(20) UNSIGNED NOT NULL,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(150) NOT NULL,
  `kind` enum('predefined','custom') NOT NULL,
  `value_type` enum('percentage','fixed') NOT NULL,
  `value` decimal(12,2) NOT NULL,
  `original_amount` decimal(12,2) NOT NULL,
  `eligible_balance` decimal(12,2) NOT NULL,
  `discount_amount` decimal(12,2) NOT NULL,
  `payable_amount` decimal(12,2) NOT NULL,
  `definition_snapshot` longtext NOT NULL,
  `applied_by` bigint(20) UNSIGNED NOT NULL,
  `applied_by_name` varchar(255) NOT NULL,
  `applied_at` datetime NOT NULL DEFAULT current_timestamp()
) ;

-- `fee_items`
CREATE TABLE IF NOT EXISTS `fee_items` (
  `id` int(11) NOT NULL,
  `fee_name` varchar(100) NOT NULL,
  `class_id` int(11) NOT NULL,
  `new_amount` int(11) NOT NULL,
  `old_amount` int(11) NOT NULL,
  `compulsory` tinyint(1) NOT NULL,
  `session_id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

-- `fee_payments`
CREATE TABLE IF NOT EXISTS `fee_payments` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `session_id` varchar(20) NOT NULL,
  `amount_paid` decimal(12,2) NOT NULL,
  `payment_date` date NOT NULL,
  `payment_method` varchar(40) DEFAULT NULL,
  `receipt_number` varchar(80) NOT NULL,
  `settlement_reference` varchar(100) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

-- `fee_payment_allocations`
CREATE TABLE IF NOT EXISTS `fee_payment_allocations` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `payment_id` bigint(20) UNSIGNED NOT NULL,
  `fee_id` bigint(20) UNSIGNED NOT NULL,
  `amount_applied` decimal(12,2) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

-- `fee_payment_details`
CREATE TABLE IF NOT EXISTS `fee_payment_details` (
  `payment_id` bigint(20) UNSIGNED NOT NULL,
  `fee_id` bigint(20) UNSIGNED NOT NULL,
  `title` varchar(150) NOT NULL,
  `charge_kind` enum('normal','other') NOT NULL,
  `original_amount` decimal(12,2) NOT NULL,
  `discount_amount` decimal(12,2) NOT NULL,
  `amount_due` decimal(12,2) NOT NULL,
  `balance` decimal(12,2) NOT NULL,
  `received_by_id` bigint(20) UNSIGNED NOT NULL,
  `received_by` varchar(255) NOT NULL,
  `student_name` varchar(255) NOT NULL
) ;

-- `grading_keys`
CREATE TABLE IF NOT EXISTS `grading_keys` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `min_score` int(11) DEFAULT NULL,
  `max_score` int(11) DEFAULT NULL,
  `grade` varchar(10) NOT NULL,
  `remark` varchar(50) NOT NULL,
  `color_hex` varchar(7) DEFAULT NULL,
  `sort_order` int(11) DEFAULT 0,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `grading_playclass`
CREATE TABLE IF NOT EXISTS `grading_playclass` (
  `id` int(11) NOT NULL,
  `grade` varchar(5) NOT NULL,
  `remark` varchar(50) NOT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `inventory_categories`
CREATE TABLE IF NOT EXISTS `inventory_categories` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(150) NOT NULL,
  `description` text DEFAULT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `version` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `inventory_issuances`
CREATE TABLE IF NOT EXISTS `inventory_issuances` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `student_id` bigint(20) UNSIGNED DEFAULT NULL,
  `recipient` varchar(255) NOT NULL,
  `department` varchar(150) NOT NULL DEFAULT '',
  `date` date NOT NULL,
  `reference` varchar(150) NOT NULL,
  `notes` text NOT NULL,
  `issued_by` bigint(20) UNSIGNED NOT NULL,
  `issued_by_name` varchar(255) NOT NULL,
  `request_id` varchar(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
  `request_hash` char(64) NOT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- `inventory_issuance_items`
CREATE TABLE IF NOT EXISTS `inventory_issuance_items` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `issuance_id` bigint(20) UNSIGNED NOT NULL,
  `inventory_item_id` bigint(20) UNSIGNED NOT NULL,
  `movement_id` bigint(20) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- `inventory_items`
CREATE TABLE IF NOT EXISTS `inventory_items` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(150) NOT NULL,
  `sku` varchar(80) NOT NULL,
  `category_id` bigint(20) UNSIGNED DEFAULT NULL,
  `unit` varchar(40) NOT NULL,
  `reorder_level` decimal(12,2) NOT NULL DEFAULT 0.00,
  `current_quantity` decimal(12,2) NOT NULL DEFAULT 0.00,
  `unit_cost` decimal(18,4) NOT NULL DEFAULT 0.0000,
  `stock_value` decimal(24,4) NOT NULL DEFAULT 0.0000,
  `location` varchar(200) NOT NULL DEFAULT '',
  `description` text DEFAULT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `version` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ;

-- `inventory_movements`
CREATE TABLE IF NOT EXISTS `inventory_movements` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `item_id` bigint(20) UNSIGNED NOT NULL,
  `item_name` varchar(150) NOT NULL,
  `sku` varchar(80) NOT NULL,
  `unit` varchar(40) NOT NULL,
  `type` enum('in','out','adjustment') NOT NULL,
  `direction` enum('increase','decrease') NOT NULL,
  `quantity` decimal(12,2) NOT NULL,
  `previous_quantity` decimal(12,2) NOT NULL,
  `new_quantity` decimal(12,2) NOT NULL,
  `unit_cost` decimal(18,4) NOT NULL,
  `total_cost` decimal(24,4) NOT NULL,
  `previous_value` decimal(24,4) NOT NULL,
  `new_value` decimal(24,4) NOT NULL,
  `date` date NOT NULL,
  `reference` varchar(150) NOT NULL DEFAULT '',
  `supplier_id` bigint(20) UNSIGNED DEFAULT NULL,
  `supplier_name` varchar(150) NOT NULL DEFAULT '',
  `recipient` varchar(150) NOT NULL DEFAULT '',
  `department` varchar(150) NOT NULL DEFAULT '',
  `notes` text DEFAULT NULL,
  `performed_by_id` bigint(20) UNSIGNED NOT NULL,
  `performed_by` varchar(150) NOT NULL,
  `request_id` varchar(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
  `request_hash` char(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp()
) ;

-- `inventory_suppliers`
CREATE TABLE IF NOT EXISTS `inventory_suppliers` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(150) NOT NULL,
  `contact_person` varchar(150) NOT NULL DEFAULT '',
  `phone` varchar(40) NOT NULL DEFAULT '',
  `email` varchar(254) NOT NULL DEFAULT '',
  `address` text DEFAULT NULL,
  `notes` text DEFAULT NULL,
  `supplied_items` text DEFAULT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `version` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `library_books`
CREATE TABLE IF NOT EXISTS `library_books` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `book_title` varchar(255) NOT NULL,
  `author` varchar(255) DEFAULT NULL,
  `published_year` smallint(4) DEFAULT NULL,
  `book_author` varchar(150) DEFAULT NULL,
  `isbn` varchar(20) DEFAULT NULL,
  `book_code` varchar(50) NOT NULL,
  `category` varchar(100) DEFAULT NULL,
  `publication_year` int(11) DEFAULT NULL,
  `publisher` varchar(150) DEFAULT NULL,
  `total_copies` int(11) DEFAULT 1,
  `available_copies` int(11) DEFAULT 1,
  `book_description` text DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `library_borrowing`
CREATE TABLE IF NOT EXISTS `library_borrowing` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `book_id` bigint(20) UNSIGNED NOT NULL,
  `borrow_date` date NOT NULL,
  `due_date` date NOT NULL,
  `return_date` date DEFAULT NULL,
  `status` varchar(50) DEFAULT 'borrowed',
  `fine_amount` decimal(10,2) DEFAULT 0.00,
  `fine_paid` tinyint(1) DEFAULT 0,
  `librarian_id` bigint(20) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `lms_activity_log`
CREATE TABLE IF NOT EXISTS `lms_activity_log` (
  `id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL COMMENT 'Teacher or admin ID',
  `user_type` varchar(50) DEFAULT NULL COMMENT 'teacher, admin, student',
  `action` varchar(255) NOT NULL COMMENT 'create_topic, update_topic, upload_resource, etc',
  `object_type` varchar(50) DEFAULT NULL COMMENT 'topic, subtopic, resource, announcement',
  `object_id` int(11) DEFAULT NULL COMMENT 'ID of affected object',
  `description` varchar(500) DEFAULT NULL,
  `ip_address` varchar(45) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `lms_announcements`
CREATE TABLE IF NOT EXISTS `lms_announcements` (
  `id` int(11) NOT NULL,
  `teacher_id` int(11) NOT NULL COMMENT 'FK to teachers table',
  `topic_id` int(11) DEFAULT NULL COMMENT 'FK to lms_topics or NULL for general announcement',
  `title` varchar(255) NOT NULL,
  `message` longtext NOT NULL COMMENT 'Rich text message content',
  `visibility` varchar(50) DEFAULT 'public' COMMENT 'public, class-specific, private, draft',
  `is_pinned` tinyint(1) DEFAULT 0,
  `pin_until` timestamp NULL DEFAULT NULL COMMENT 'When to auto-unpin announcement',
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `lms_feedback`
CREATE TABLE IF NOT EXISTS `lms_feedback` (
  `id` int(11) NOT NULL,
  `topic_id` int(11) NOT NULL COMMENT 'FK to lms_topics',
  `subtopic_id` int(11) DEFAULT NULL COMMENT 'FK to lms_subtopics',
  `student_id` int(11) DEFAULT NULL COMMENT 'FK to students table (NULL for anonymous)',
  `teacher_id` int(11) DEFAULT NULL COMMENT 'FK to teachers table (for reply)',
  `comment_type` varchar(50) DEFAULT 'comment' COMMENT 'comment, question, feedback, issue',
  `content` longtext NOT NULL,
  `reply` longtext DEFAULT NULL COMMENT 'Teacher reply to the feedback',
  `is_resolved` tinyint(1) DEFAULT 0,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `replied_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `lms_resources`
CREATE TABLE IF NOT EXISTS `lms_resources` (
  `id` int(11) NOT NULL,
  `teacher_id` int(11) NOT NULL COMMENT 'FK to teachers table',
  `topic_id` int(11) DEFAULT NULL COMMENT 'FK to lms_topics (NULL if subtopic-specific)',
  `subtopic_id` int(11) DEFAULT NULL COMMENT 'FK to lms_subtopics (NULL if topic-level)',
  `file_name` varchar(255) NOT NULL COMMENT 'Unique filename on server',
  `original_name` varchar(255) NOT NULL COMMENT 'Original filename uploaded by user',
  `file_path` varchar(500) NOT NULL COMMENT 'Relative path to stored file',
  `file_type` varchar(50) NOT NULL COMMENT 'File extension: pdf, docx, pptx, jpg, mp4, etc',
  `file_size` int(11) NOT NULL COMMENT 'File size in bytes',
  `description` varchar(500) DEFAULT NULL COMMENT 'User-provided description',
  `download_count` int(11) DEFAULT 0 COMMENT 'Number of downloads',
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `lms_settings`
CREATE TABLE IF NOT EXISTS `lms_settings` (
  `id` int(11) NOT NULL,
  `setting_key` varchar(255) NOT NULL,
  `setting_value` longtext DEFAULT NULL COMMENT 'JSON, boolean, or string value',
  `setting_type` varchar(50) DEFAULT 'string' COMMENT 'string, json, boolean, integer',
  `description` varchar(255) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `lms_student_progress`
CREATE TABLE IF NOT EXISTS `lms_student_progress` (
  `id` int(11) NOT NULL,
  `student_id` int(11) NOT NULL COMMENT 'FK to students table',
  `topic_id` int(11) NOT NULL COMMENT 'FK to lms_topics',
  `subtopic_id` int(11) DEFAULT NULL COMMENT 'FK to lms_subtopics',
  `status` varchar(50) DEFAULT 'not_started' COMMENT 'not_started, in_progress, completed, archived',
  `completion_percentage` int(11) DEFAULT 0 COMMENT 'Percentage of content completed (0-100)',
  `time_spent_seconds` int(11) DEFAULT 0 COMMENT 'Total time spent in seconds',
  `last_accessed` timestamp NULL DEFAULT NULL COMMENT 'Last time student accessed this topic',
  `completed_at` timestamp NULL DEFAULT NULL COMMENT 'When student completed the topic',
  `notes` text DEFAULT NULL COMMENT 'Student notes or highlights',
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `lms_subtopics`
CREATE TABLE IF NOT EXISTS `lms_subtopics` (
  `id` int(11) NOT NULL,
  `topic_id` int(11) NOT NULL COMMENT 'FK to lms_topics',
  `teacher_id` int(11) NOT NULL COMMENT 'FK to teachers table (denormalized for query performance)',
  `title` varchar(255) NOT NULL,
  `description` varchar(500) DEFAULT NULL COMMENT 'Brief description of subtopic',
  `subtopic_number` int(11) NOT NULL COMMENT 'Order/sequence number within the topic',
  `notes` longtext DEFAULT NULL COMMENT 'Rich text lesson notes for this subtopic',
  `view_count` int(11) DEFAULT 0,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `lms_topics`
CREATE TABLE IF NOT EXISTS `lms_topics` (
  `id` int(11) NOT NULL,
  `teacher_id` int(11) NOT NULL COMMENT 'FK to teachers table',
  `title` varchar(255) NOT NULL,
  `description` longtext DEFAULT NULL COMMENT 'Topic description/overview',
  `content` longtext DEFAULT NULL COMMENT 'Rich text content of the topic',
  `status` varchar(50) DEFAULT 'draft' COMMENT 'draft, incomplete, published, completed, archived',
  `subject_id` int(11) DEFAULT NULL COMMENT 'Optional: FK to subjects table',
  `notes` longtext DEFAULT NULL COMMENT 'Teacher private notes for this topic',
  `view_count` int(11) DEFAULT 0 COMMENT 'Number of times viewed by students',
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `topic_number` int(11) DEFAULT 0 COMMENT 'Order/sequence number for topic ordering'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `lms_topic_classes`
CREATE TABLE IF NOT EXISTS `lms_topic_classes` (
  `topic_id` int(11) NOT NULL,
  `class_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `mentor_remark`
CREATE TABLE IF NOT EXISTS `mentor_remark` (
  `id` int(11) NOT NULL,
  `class_id` int(11) NOT NULL,
  `session_id` int(11) NOT NULL,
  `student_id` int(11) NOT NULL,
  `mentor_id` int(11) DEFAULT NULL,
  `mentor_name` varchar(255) DEFAULT NULL,
  `academics` text DEFAULT NULL,
  `social` text DEFAULT NULL,
  `spirit` text DEFAULT NULL,
  `hygiene` text DEFAULT NULL,
  `general` text DEFAULT NULL,
  `remark` text DEFAULT NULL,
  `academics2` text DEFAULT NULL,
  `social2` text DEFAULT NULL,
  `spirit2` text DEFAULT NULL,
  `hygiene2` text DEFAULT NULL,
  `general2` text DEFAULT NULL,
  `remark2` text DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- `messages`
CREATE TABLE IF NOT EXISTS `messages` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `sender_id` bigint(20) UNSIGNED NOT NULL,
  `receiver_id` bigint(20) UNSIGNED NOT NULL,
  `message` text NOT NULL,
  `is_read` tinyint(1) DEFAULT 0,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `notifications`
CREATE TABLE IF NOT EXISTS `notifications` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `title` varchar(200) NOT NULL,
  `body` text NOT NULL,
  `target_type` enum('all','class','role','specific_user') DEFAULT 'all' COMMENT 'Who this notification targets',
  `target_class_id` bigint(20) UNSIGNED DEFAULT NULL COMMENT 'If target_type=class',
  `target_role` enum('admin','teacher','parent','guardian','student') DEFAULT NULL COMMENT 'If target_type=role',
  `target_user_id` bigint(20) UNSIGNED DEFAULT NULL COMMENT 'If target_type=specific_user',
  `notification_type` enum('announcement','event','urgent','info','reminder') DEFAULT 'info',
  `data_type` varchar(50) DEFAULT NULL COMMENT 'announcement, assignment, fee, etc.',
  `data_id` bigint(20) UNSIGNED DEFAULT NULL COMMENT 'ID of related announcement/assignment/etc',
  `priority` enum('low','normal','high') DEFAULT 'normal',
  `delivery_status` enum('pending','sent','failed','delivered') DEFAULT 'pending',
  `scheduled_at` datetime DEFAULT NULL COMMENT 'If NULL, send immediately',
  `sent_at` datetime DEFAULT NULL,
  `failure_reason` text DEFAULT NULL,
  `created_by` bigint(20) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Push notifications sent to users';

-- `notification_devices`
CREATE TABLE IF NOT EXISTS `notification_devices` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `notification_id` bigint(20) UNSIGNED NOT NULL,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `fcm_token` varchar(500) NOT NULL,
  `firebase_message_id` varchar(255) DEFAULT NULL,
  `status` enum('sent','failed','invalid_token') NOT NULL DEFAULT 'sent',
  `error_message` text DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `notification_reads`
CREATE TABLE IF NOT EXISTS `notification_reads` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `notification_id` bigint(20) UNSIGNED NOT NULL,
  `user_id` bigint(20) UNSIGNED NOT NULL,
  `read_at` datetime DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Track which users have read notifications';

-- `other_payments`
CREATE TABLE IF NOT EXISTS `other_payments` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `payment_account_id` bigint(20) UNSIGNED DEFAULT NULL,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `class_id` bigint(20) UNSIGNED NOT NULL,
  `payment_item_id` bigint(20) UNSIGNED NOT NULL,
  `payment_item_name` varchar(150) NOT NULL,
  `session_id` bigint(20) UNSIGNED DEFAULT NULL,
  `term` varchar(20) DEFAULT NULL,
  `amount_due` decimal(12,2) NOT NULL,
  `amount_paid` decimal(12,2) NOT NULL,
  `payment_date` date NOT NULL,
  `payment_method` varchar(50) DEFAULT NULL,
  `receipt_number` varchar(100) NOT NULL,
  `request_id` varchar(80) DEFAULT NULL,
  `reference` varchar(150) DEFAULT NULL,
  `notes` text DEFAULT NULL,
  `recorded_by` bigint(20) UNSIGNED NOT NULL,
  `status` enum('completed','reversed') NOT NULL DEFAULT 'completed',
  `reversed_by` bigint(20) UNSIGNED DEFAULT NULL,
  `reversed_at` datetime DEFAULT NULL,
  `reversal_reason` text DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ;

-- `other_payment_items`
CREATE TABLE IF NOT EXISTS `other_payment_items` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(150) NOT NULL,
  `description` text DEFAULT NULL,
  `default_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
  `allow_amount_override` tinyint(1) NOT NULL DEFAULT 0,
  `allow_partial_payment` tinyint(1) NOT NULL DEFAULT 0,
  `allow_discounts` tinyint(1) NOT NULL DEFAULT 1,
  `session_id` bigint(20) UNSIGNED DEFAULT NULL,
  `term` varchar(20) DEFAULT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `created_by` bigint(20) UNSIGNED NOT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ;

-- `parent_student`
CREATE TABLE IF NOT EXISTS `parent_student` (
  `parent_id` bigint(20) UNSIGNED NOT NULL,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `relationship` varchar(50) DEFAULT 'Parent',
  `is_primary` tinyint(1) DEFAULT 1,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `results`
CREATE TABLE IF NOT EXISTS `results` (
  `id` int(11) NOT NULL,
  `ids` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `class_id` bigint(20) UNSIGNED DEFAULT NULL,
  `session_id` bigint(20) UNSIGNED DEFAULT NULL,
  `subject_id` bigint(20) UNSIGNED NOT NULL,
  `subject_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `wk1` decimal(5,2) NOT NULL DEFAULT 0.00,
  `wk2` decimal(5,2) NOT NULL DEFAULT 0.00,
  `wk3` decimal(5,2) NOT NULL DEFAULT 0.00,
  `wk4` decimal(5,2) NOT NULL DEFAULT 0.00,
  `remark_id` bigint(20) UNSIGNED DEFAULT NULL,
  `teacher_comment` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `ca1` decimal(5,2) DEFAULT 0.00,
  `wk6` decimal(5,2) NOT NULL DEFAULT 0.00,
  `wk7` decimal(5,2) NOT NULL DEFAULT 0.00,
  `wk8` decimal(5,2) NOT NULL DEFAULT 0.00,
  `wk9` decimal(5,2) NOT NULL DEFAULT 0.00,
  `ca2` decimal(5,2) DEFAULT 0.00,
  `st1` decimal(5,2) NOT NULL DEFAULT 0.00,
  `st2` decimal(5,2) NOT NULL DEFAULT 0.00,
  `cat` decimal(5,2) NOT NULL DEFAULT 0.00,
  `exam` decimal(5,2) DEFAULT 0.00,
  `total` decimal(5,2) DEFAULT NULL,
  `grade` char(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `position_in_class` int(11) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `pl` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

-- `school_api_sessions`
CREATE TABLE IF NOT EXISTS `school_api_sessions` (
  `token_hash` char(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
  `user_id` bigint(20) UNSIGNED NOT NULL,
  `expires_at` datetime NOT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `school_info`
CREATE TABLE IF NOT EXISTS `school_info` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `vice_principal` varchar(100) DEFAULT NULL COMMENT 'Name of Vice Principal (if applicable)',
  `school_name` varchar(200) NOT NULL,
  `motto` varchar(255) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  `city` varchar(100) DEFAULT NULL,
  `state` varchar(100) DEFAULT NULL,
  `postal_code` varchar(20) DEFAULT NULL,
  `country` varchar(100) DEFAULT NULL,
  `school_email` varchar(150) DEFAULT NULL,
  `phone` varchar(200) DEFAULT NULL,
  `complaints` varchar(300) DEFAULT NULL,
  `website` varchar(255) DEFAULT NULL,
  `logo_url` varchar(255) DEFAULT NULL,
  `favicon_url` varchar(255) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `principal_primary` varchar(100) DEFAULT NULL COMMENT 'Name of Primary School Principal',
  `principal_secondary` varchar(100) DEFAULT NULL COMMENT 'Name of Secondary School Principal'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `sections`
CREATE TABLE IF NOT EXISTS `sections` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(255) NOT NULL COMMENT 'Section name e.g., Nursery Academy',
  `stage` varchar(100) NOT NULL COMMENT 'Nursery, Primary, or Secondary',
  `coordinator` varchar(255) DEFAULT NULL COMMENT 'Lead coordinator/tutor name',
  `campus` varchar(100) DEFAULT NULL COMMENT 'Campus location e.g., Main, Annex',
  `status` tinyint(1) DEFAULT 1 COMMENT '1 = Active, 0 = Inactive',
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `students`
CREATE TABLE IF NOT EXISTS `students` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `full_name` varchar(100) NOT NULL,
  `username` varchar(255) DEFAULT NULL,
  `gender` enum('M','F','Other') DEFAULT NULL,
  `class_id` bigint(20) UNSIGNED NOT NULL,
  `session` varchar(40) NOT NULL,
  `admission_number` varchar(30) NOT NULL,
  `mentor_id` int(11) DEFAULT NULL,
  `mentor` varchar(255) DEFAULT NULL,
  `parent_code` varchar(6) DEFAULT NULL COMMENT '5-6 digit numeric code for parent login',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `date_of_birth` date DEFAULT NULL,
  `photo_url` varchar(255) DEFAULT NULL,
  `blood_group` varchar(5) DEFAULT NULL,
  `allergies` text DEFAULT NULL,
  `emergency_contact_name` varchar(100) DEFAULT NULL,
  `emergency_contact_phone` varchar(20) DEFAULT NULL,
  `fcm_token` varchar(300) DEFAULT NULL,
  `fcm_token_updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `regno` int(11) NOT NULL,
  `section` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `student_other_payment_accounts`
CREATE TABLE IF NOT EXISTS `student_other_payment_accounts` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `class_id` bigint(20) UNSIGNED NOT NULL,
  `payment_item_id` bigint(20) UNSIGNED NOT NULL,
  `payment_item_name` varchar(150) NOT NULL,
  `session_id` bigint(20) UNSIGNED DEFAULT NULL,
  `term` varchar(20) DEFAULT NULL,
  `amount_due` decimal(12,2) NOT NULL,
  `status` enum('unpaid','partial','paid','cancelled') NOT NULL DEFAULT 'unpaid',
  `created_by` bigint(20) UNSIGNED NOT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ;

-- `student_remarks`
CREATE TABLE IF NOT EXISTS `student_remarks` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `student_id` bigint(20) UNSIGNED NOT NULL,
  `session_id` bigint(20) UNSIGNED NOT NULL,
  `teacher_remark` text DEFAULT NULL,
  `principal_remark` text DEFAULT NULL,
  `class_teacher_remark` text DEFAULT NULL,
  `created_by` bigint(20) UNSIGNED DEFAULT NULL,
  `approved_by` bigint(20) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `a` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'AttendanceSS (0-5)',
  `b` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Attentiveness in class (0-5)',
  `c` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Craft, drawing, painting (0-5)',
  `d` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Cooperation spirit (0-5)',
  `e` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Fluency/Elocution (0-5)',
  `f` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Handwriting (0-5)',
  `g` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Honesty (0-5)',
  `h` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Leadership ability (0-5)',
  `i` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Music skills (0-5)',
  `j` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Neatness (0-5)',
  `k` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Organizational ability (0-5)',
  `l` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Politeness (0-5)',
  `m` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Promptness in assignment (0-5)',
  `n` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Punctuality (0-5)',
  `o` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Relationship with staff (0-5)',
  `p` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Relationship with students (0-5)',
  `q` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Reliability (0-5)',
  `r` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Responsibility (0-5)',
  `s` int(11) DEFAULT NULL COMMENT 'Self-control',
  `t` int(11) DEFAULT NULL COMMENT 'willingness to learn',
  `u` int(11) DEFAULT NULL,
  `v` int(11) DEFAULT NULL,
  `resumption_date` date DEFAULT NULL COMMENT 'Date students resume for this term',
  `school_closing_date` date DEFAULT NULL COMMENT 'Date school closes for this term',
  `mentor` varchar(300) DEFAULT NULL,
  `teacher_comment` varchar(500) DEFAULT NULL,
  `first_term_comment` varchar(500) DEFAULT NULL,
  `second_term_comment` varchar(500) DEFAULT NULL,
  `abs` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

-- `subjects`
CREATE TABLE IF NOT EXISTS `subjects` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `class` varchar(255) DEFAULT NULL,
  `class_id` bigint(20) UNSIGNED NOT NULL,
  `subject_name` varchar(100) NOT NULL,
  `subject_code` varchar(20) DEFAULT NULL,
  `teacher` varchar(50) DEFAULT NULL,
  `teacher_id` bigint(20) UNSIGNED DEFAULT NULL,
  `credit_hours` decimal(3,1) DEFAULT 1.0,
  `is_compulsory` tinyint(1) DEFAULT 1,
  `description` text DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `academic_session_id` bigint(20) UNSIGNED DEFAULT NULL COMMENT 'Links to academic_sessions.id for the term/year this subject applies to',
  `status` tinyint(1) UNSIGNED NOT NULL DEFAULT 1 COMMENT '1 = active (offered), 2 = inactive (not offered / deactivated)',
  `assoc_teacher` varchar(500) DEFAULT NULL,
  `grade_type` varchar(10) DEFAULT 'letters',
  `play_class_category` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `timetable`
CREATE TABLE IF NOT EXISTS `timetable` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `class_id` bigint(20) UNSIGNED NOT NULL,
  `day_of_week` varchar(20) NOT NULL,
  `period_number` tinyint(3) UNSIGNED DEFAULT NULL,
  `subject_id` bigint(20) UNSIGNED NOT NULL,
  `start_time` time NOT NULL,
  `end_time` time NOT NULL,
  `venue` varchar(100) DEFAULT NULL,
  `teacher_id` bigint(20) UNSIGNED DEFAULT NULL,
  `room_number` varchar(20) DEFAULT NULL,
  `session_year` varchar(20) DEFAULT '2025/2026',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- `users`
CREATE TABLE IF NOT EXISTS `users` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `email` varchar(150) DEFAULT NULL,
  `phone` varchar(20) DEFAULT NULL,
  `second_phone` varchar(20) DEFAULT NULL,
  `address` text DEFAULT NULL,
  `date_of_birth` date DEFAULT NULL,
  `password_hash` varchar(2550) NOT NULL,
  `full_name` varchar(100) NOT NULL,
  `gender` enum('Male','Female') DEFAULT NULL,
  `role` enum('parent','guardian','teacher','admin','secretary','bursar','principal','vice_principal','librarian','hos','inventory') NOT NULL DEFAULT 'parent',
  `mentor` varchar(255) DEFAULT NULL,
  `assigned_class` varchar(100) DEFAULT NULL,
  `teacher_role` varchar(200) DEFAULT NULL,
  `photo` varchar(255) DEFAULT NULL,
  `signature` varchar(255) DEFAULT NULL,
  `otp_code` varchar(6) DEFAULT NULL,
  `otp_expires_at` datetime DEFAULT NULL,
  `last_login` datetime DEFAULT NULL,
  `is_active` tinyint(1) DEFAULT 1,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `fcm_token_updated_at` varchar(300) NOT NULL,
  `fcm_token` varchar(300) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ============================================================================
-- 2) ENSURE EVERY COLUMN EXISTS
-- ============================================================================

-- Columns for `academic_sessions`
ALTER TABLE `academic_sessions` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `academic_sessions` ADD COLUMN IF NOT EXISTS `session_year` varchar(20) NOT NULL COMMENT 'e.g. 2025/2026' AFTER `id`;
ALTER TABLE `academic_sessions` ADD COLUMN IF NOT EXISTS `term` enum('1st','2nd','3rd') NOT NULL AFTER `session_year`;
ALTER TABLE `academic_sessions` ADD COLUMN IF NOT EXISTS `status` tinyint(1) UNSIGNED NOT NULL DEFAULT 0 AFTER `term`;
ALTER TABLE `academic_sessions` ADD COLUMN IF NOT EXISTS `app_view` int(11) DEFAULT NULL AFTER `status`;
ALTER TABLE `academic_sessions` ADD COLUMN IF NOT EXISTS `start_date` date DEFAULT NULL AFTER `app_view`;
ALTER TABLE `academic_sessions` ADD COLUMN IF NOT EXISTS `end_date` date DEFAULT NULL AFTER `start_date`;
ALTER TABLE `academic_sessions` ADD COLUMN IF NOT EXISTS `closing` varchar(150) DEFAULT NULL AFTER `end_date`;
ALTER TABLE `academic_sessions` ADD COLUMN IF NOT EXISTS `open` varchar(150) DEFAULT NULL AFTER `closing`;
ALTER TABLE `academic_sessions` ADD COLUMN IF NOT EXISTS `no_open` int(11) NOT NULL DEFAULT 0 AFTER `open`;
ALTER TABLE `academic_sessions` ADD COLUMN IF NOT EXISTS `no_opening` int(11) NOT NULL AFTER `no_open`;
ALTER TABLE `academic_sessions` ADD COLUMN IF NOT EXISTS `is_current` tinyint(1) NOT NULL DEFAULT 0 COMMENT '1 = currently active session/term' AFTER `no_opening`;
ALTER TABLE `academic_sessions` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `is_current`;
ALTER TABLE `academic_sessions` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `activity_participation`
ALTER TABLE `activity_participation` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `activity_participation` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `activity_participation` ADD COLUMN IF NOT EXISTS `activity_id` bigint(20) UNSIGNED NOT NULL AFTER `student_id`;
ALTER TABLE `activity_participation` ADD COLUMN IF NOT EXISTS `join_date` date NOT NULL AFTER `activity_id`;
ALTER TABLE `activity_participation` ADD COLUMN IF NOT EXISTS `status` varchar(50) DEFAULT 'active' AFTER `join_date`;
ALTER TABLE `activity_participation` ADD COLUMN IF NOT EXISTS `participation_level` varchar(100) DEFAULT NULL AFTER `status`;
ALTER TABLE `activity_participation` ADD COLUMN IF NOT EXISTS `achievements` text DEFAULT NULL AFTER `participation_level`;
ALTER TABLE `activity_participation` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `achievements`;

-- Columns for `announcements`
ALTER TABLE `announcements` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `announcements` ADD COLUMN IF NOT EXISTS `title` varchar(200) NOT NULL AFTER `id`;
ALTER TABLE `announcements` ADD COLUMN IF NOT EXISTS `content` text NOT NULL AFTER `title`;
ALTER TABLE `announcements` ADD COLUMN IF NOT EXISTS `type` enum('general','class','emergency','event') NOT NULL DEFAULT 'general' AFTER `content`;
ALTER TABLE `announcements` ADD COLUMN IF NOT EXISTS `target_class_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `type`;
ALTER TABLE `announcements` ADD COLUMN IF NOT EXISTS `priority` enum('low','medium','high') DEFAULT 'medium' AFTER `target_class_id`;
ALTER TABLE `announcements` ADD COLUMN IF NOT EXISTS `created_by` bigint(20) UNSIGNED NOT NULL AFTER `priority`;
ALTER TABLE `announcements` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `created_by`;
ALTER TABLE `announcements` ADD COLUMN IF NOT EXISTS `expires_at` datetime DEFAULT NULL AFTER `created_at`;
ALTER TABLE `announcements` ADD COLUMN IF NOT EXISTS `status` tinyint(1) UNSIGNED NOT NULL DEFAULT 1 AFTER `expires_at`;
ALTER TABLE `announcements` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `status`;

-- Columns for `app_settings`
ALTER TABLE `app_settings` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `app_settings` ADD COLUMN IF NOT EXISTS `setting_key` varchar(255) DEFAULT NULL AFTER `id`;
ALTER TABLE `app_settings` ADD COLUMN IF NOT EXISTS `setting_value` decimal(10,2) DEFAULT NULL AFTER `setting_key`;
ALTER TABLE `app_settings` ADD COLUMN IF NOT EXISTS `download_link` varchar(500) DEFAULT NULL AFTER `setting_value`;
ALTER TABLE `app_settings` ADD COLUMN IF NOT EXISTS `update_text` text DEFAULT NULL AFTER `download_link`;
ALTER TABLE `app_settings` ADD COLUMN IF NOT EXISTS `description` text DEFAULT NULL AFTER `update_text`;
ALTER TABLE `app_settings` ADD COLUMN IF NOT EXISTS `created_at` timestamp NULL DEFAULT current_timestamp() AFTER `description`;
ALTER TABLE `app_settings` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `assignments`
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `class_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `subject_id` bigint(20) UNSIGNED NOT NULL AFTER `class_id`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `teacher_id` bigint(20) UNSIGNED NOT NULL AFTER `subject_id`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `title` varchar(200) NOT NULL AFTER `teacher_id`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `description` text DEFAULT NULL AFTER `title`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `body` text NOT NULL AFTER `description`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `instructions` text DEFAULT NULL AFTER `body`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `assigned_date` date NOT NULL AFTER `instructions`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `due_date` date NOT NULL AFTER `assigned_date`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `due_time` time DEFAULT NULL AFTER `due_date`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `assignment_type` varchar(50) DEFAULT 'homework' AFTER `due_time`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `pdf` varchar(1200) NOT NULL AFTER `assignment_type`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `word` varchar(1200) NOT NULL AFTER `pdf`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `total_marks` decimal(5,2) DEFAULT 0.00 AFTER `word`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `total_marks`;
ALTER TABLE `assignments` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `assignment_submissions`
ALTER TABLE `assignment_submissions` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `assignment_submissions` ADD COLUMN IF NOT EXISTS `assignment_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `assignment_submissions` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `assignment_id`;
ALTER TABLE `assignment_submissions` ADD COLUMN IF NOT EXISTS `submission_date` date NOT NULL AFTER `student_id`;
ALTER TABLE `assignment_submissions` ADD COLUMN IF NOT EXISTS `submission_time` time NOT NULL AFTER `submission_date`;
ALTER TABLE `assignment_submissions` ADD COLUMN IF NOT EXISTS `submission_content` text DEFAULT NULL AFTER `submission_time`;
ALTER TABLE `assignment_submissions` ADD COLUMN IF NOT EXISTS `submission_status` varchar(50) DEFAULT 'pending' AFTER `submission_content`;
ALTER TABLE `assignment_submissions` ADD COLUMN IF NOT EXISTS `marks_obtained` decimal(5,2) DEFAULT NULL AFTER `submission_status`;
ALTER TABLE `assignment_submissions` ADD COLUMN IF NOT EXISTS `teacher_feedback` text DEFAULT NULL AFTER `marks_obtained`;
ALTER TABLE `assignment_submissions` ADD COLUMN IF NOT EXISTS `graded_at` datetime DEFAULT NULL AFTER `teacher_feedback`;
ALTER TABLE `assignment_submissions` ADD COLUMN IF NOT EXISTS `graded_by` bigint(20) UNSIGNED DEFAULT NULL AFTER `graded_at`;
ALTER TABLE `assignment_submissions` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `graded_by`;

-- Columns for `attendance`
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `class_id` int(11) NOT NULL AFTER `student_id`;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `session_id` int(11) DEFAULT NULL AFTER `class_id`;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `session_year` varchar(20) NOT NULL DEFAULT '2025/2026' AFTER `session_id`;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `term` varchar(10) NOT NULL DEFAULT '1st' AFTER `session_year`;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `attendance_date` date NOT NULL AFTER `term`;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `status` enum('present','absent','late','excused') NOT NULL AFTER `attendance_date`;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `reason` text DEFAULT NULL AFTER `status`;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `marked_by` bigint(20) UNSIGNED DEFAULT NULL AFTER `reason`;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `marked_by`;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `latitude` decimal(10,8) DEFAULT NULL COMMENT 'GPS latitude coordinate' AFTER `created_at`;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `longitude` decimal(11,8) DEFAULT NULL COMMENT 'GPS longitude coordinate' AFTER `latitude`;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `accuracy` decimal(8,2) DEFAULT NULL COMMENT 'GPS accuracy in meters' AFTER `longitude`;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `location_type` enum('school','pickup','dropoff') DEFAULT NULL COMMENT 'Type of location: school, pickup, or dropoff' AFTER `accuracy`;
ALTER TABLE `attendance` ADD COLUMN IF NOT EXISTS `distance_from_school` decimal(10,2) DEFAULT NULL COMMENT 'Distance from school geofence in meters' AFTER `location_type`;

-- Columns for `bursary_activity`
ALTER TABLE `bursary_activity` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `bursary_activity` ADD COLUMN IF NOT EXISTS `action` varchar(40) NOT NULL AFTER `id`;
ALTER TABLE `bursary_activity` ADD COLUMN IF NOT EXISTS `record_id` bigint(20) NOT NULL AFTER `action`;
ALTER TABLE `bursary_activity` ADD COLUMN IF NOT EXISTS `performed_by_id` bigint(20) UNSIGNED NOT NULL AFTER `record_id`;
ALTER TABLE `bursary_activity` ADD COLUMN IF NOT EXISTS `performed_by` varchar(255) NOT NULL AFTER `performed_by_id`;
ALTER TABLE `bursary_activity` ADD COLUMN IF NOT EXISTS `snapshot` longtext NOT NULL AFTER `performed_by`;
ALTER TABLE `bursary_activity` ADD COLUMN IF NOT EXISTS `created_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `snapshot`;

-- Columns for `bursary_discounts`
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `name` varchar(150) NOT NULL AFTER `id`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `description` text NOT NULL AFTER `name`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `kind` enum('predefined','custom') NOT NULL AFTER `description`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `value_type` enum('percentage','fixed') NOT NULL AFTER `kind`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `value` decimal(12,2) NOT NULL AFTER `value_type`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `value`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `applies_to` enum('normal','other') NOT NULL AFTER `student_id`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `session_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `applies_to`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `class_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `session_id`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `category` varchar(150) NOT NULL DEFAULT '' AFTER `class_id`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `other_payment_item_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `category`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `starts_at` datetime DEFAULT NULL AFTER `other_payment_item_id`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `expires_at` datetime DEFAULT NULL AFTER `starts_at`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `is_active` tinyint(4) NOT NULL DEFAULT 1 AFTER `expires_at`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `version` int(11) NOT NULL DEFAULT 1 AFTER `is_active`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `created_by` bigint(20) UNSIGNED NOT NULL AFTER `version`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `created_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `created_by`;
ALTER TABLE `bursary_discounts` ADD COLUMN IF NOT EXISTS `updated_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `created_at`;

-- Columns for `bursary_family_payments`
ALTER TABLE `bursary_family_payments` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `bursary_family_payments` ADD COLUMN IF NOT EXISTS `receipt_number` varchar(100) NOT NULL AFTER `id`;
ALTER TABLE `bursary_family_payments` ADD COLUMN IF NOT EXISTS `session_id` varchar(20) NOT NULL AFTER `receipt_number`;
ALTER TABLE `bursary_family_payments` ADD COLUMN IF NOT EXISTS `payment_date` date NOT NULL AFTER `session_id`;
ALTER TABLE `bursary_family_payments` ADD COLUMN IF NOT EXISTS `payment_method` varchar(40) NOT NULL AFTER `payment_date`;
ALTER TABLE `bursary_family_payments` ADD COLUMN IF NOT EXISTS `amount_paid` decimal(14,2) NOT NULL AFTER `payment_method`;
ALTER TABLE `bursary_family_payments` ADD COLUMN IF NOT EXISTS `received_by_id` bigint(20) UNSIGNED NOT NULL AFTER `amount_paid`;
ALTER TABLE `bursary_family_payments` ADD COLUMN IF NOT EXISTS `received_by` varchar(255) NOT NULL AFTER `received_by_id`;
ALTER TABLE `bursary_family_payments` ADD COLUMN IF NOT EXISTS `receipt_snapshot` longtext NOT NULL AFTER `received_by`;
ALTER TABLE `bursary_family_payments` ADD COLUMN IF NOT EXISTS `created_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `receipt_snapshot`;

-- Columns for `bursary_family_payment_lines`
ALTER TABLE `bursary_family_payment_lines` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `bursary_family_payment_lines` ADD COLUMN IF NOT EXISTS `family_payment_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `bursary_family_payment_lines` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `family_payment_id`;
ALTER TABLE `bursary_family_payment_lines` ADD COLUMN IF NOT EXISTS `fee_id` bigint(20) UNSIGNED NOT NULL AFTER `student_id`;
ALTER TABLE `bursary_family_payment_lines` ADD COLUMN IF NOT EXISTS `payment_id` bigint(20) UNSIGNED NOT NULL AFTER `fee_id`;

-- Columns for `bursary_requests`
ALTER TABLE `bursary_requests` ADD COLUMN IF NOT EXISTS `actor_id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `bursary_requests` ADD COLUMN IF NOT EXISTS `request_id` varchar(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL AFTER `actor_id`;
ALTER TABLE `bursary_requests` ADD COLUMN IF NOT EXISTS `request_hash` char(64) NOT NULL AFTER `request_id`;
ALTER TABLE `bursary_requests` ADD COLUMN IF NOT EXISTS `result` longtext DEFAULT NULL AFTER `request_hash`;
ALTER TABLE `bursary_requests` ADD COLUMN IF NOT EXISTS `created_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `result`;

-- Columns for `cbt_answers`
ALTER TABLE `cbt_answers` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `cbt_answers` ADD COLUMN IF NOT EXISTS `test_id` int(11) NOT NULL AFTER `id`;
ALTER TABLE `cbt_answers` ADD COLUMN IF NOT EXISTS `student_id` int(11) NOT NULL AFTER `test_id`;
ALTER TABLE `cbt_answers` ADD COLUMN IF NOT EXISTS `question_id` int(11) NOT NULL AFTER `student_id`;
ALTER TABLE `cbt_answers` ADD COLUMN IF NOT EXISTS `question_order` int(11) NOT NULL DEFAULT 0 AFTER `question_id`;
ALTER TABLE `cbt_answers` ADD COLUMN IF NOT EXISTS `answer` text NOT NULL COMMENT 'Student answer - can be JSON for multiple selections' AFTER `question_order`;
ALTER TABLE `cbt_answers` ADD COLUMN IF NOT EXISTS `is_correct` tinyint(1) DEFAULT NULL AFTER `answer`;
ALTER TABLE `cbt_answers` ADD COLUMN IF NOT EXISTS `marks_obtained` decimal(5,2) DEFAULT 0.00 AFTER `is_correct`;
ALTER TABLE `cbt_answers` ADD COLUMN IF NOT EXISTS `submitted_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `marks_obtained`;

-- Columns for `cbt_contexts`
ALTER TABLE `cbt_contexts` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `cbt_contexts` ADD COLUMN IF NOT EXISTS `test_id` int(11) NOT NULL AFTER `id`;
ALTER TABLE `cbt_contexts` ADD COLUMN IF NOT EXISTS `client_id` varchar(100) NOT NULL AFTER `test_id`;
ALTER TABLE `cbt_contexts` ADD COLUMN IF NOT EXISTS `title` varchar(255) DEFAULT NULL AFTER `client_id`;
ALTER TABLE `cbt_contexts` ADD COLUMN IF NOT EXISTS `passage_text` text DEFAULT NULL AFTER `title`;
ALTER TABLE `cbt_contexts` ADD COLUMN IF NOT EXISTS `context_image` longtext DEFAULT NULL AFTER `passage_text`;
ALTER TABLE `cbt_contexts` ADD COLUMN IF NOT EXISTS `order_index` int(11) NOT NULL DEFAULT 0 AFTER `context_image`;
ALTER TABLE `cbt_contexts` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `order_index`;
ALTER TABLE `cbt_contexts` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `cbt_questions`
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `test_id` int(11) NOT NULL AFTER `id`;
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `context_id` int(11) DEFAULT NULL AFTER `test_id`;
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `question_text` text NOT NULL AFTER `context_id`;
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `question_type` enum('multiple_choice','true_false','short_answer') NOT NULL DEFAULT 'multiple_choice' AFTER `question_text`;
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `question_image` longtext DEFAULT NULL AFTER `question_type`;
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `option_a` text DEFAULT NULL AFTER `question_image`;
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `option_b` text DEFAULT NULL AFTER `option_a`;
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `option_c` text DEFAULT NULL AFTER `option_b`;
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `option_d` text DEFAULT NULL AFTER `option_c`;
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `correct_answer` varchar(10) NOT NULL COMMENT 'A, B, C, D for multiple choice, or actual answer text' AFTER `option_d`;
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `marks` decimal(5,2) NOT NULL DEFAULT 1.00 AFTER `correct_answer`;
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `order_index` int(11) NOT NULL DEFAULT 0 AFTER `marks`;
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `order_index`;
ALTER TABLE `cbt_questions` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `cbt_results`
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `test_id` int(11) NOT NULL AFTER `id`;
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `student_id` int(11) NOT NULL AFTER `test_id`;
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `total_questions` int(11) NOT NULL AFTER `student_id`;
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `attempted_questions` int(11) NOT NULL DEFAULT 0 AFTER `total_questions`;
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `correct_answers` int(11) NOT NULL DEFAULT 0 AFTER `attempted_questions`;
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `total_marks` decimal(7,2) NOT NULL DEFAULT 0.00 AFTER `correct_answers`;
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `marks_obtained` decimal(7,2) NOT NULL DEFAULT 0.00 AFTER `total_marks`;
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `percentage` decimal(5,2) NOT NULL DEFAULT 0.00 AFTER `marks_obtained`;
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `grade` varchar(5) DEFAULT NULL AFTER `percentage`;
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `status` enum('pending','completed','graded') NOT NULL DEFAULT 'pending' AFTER `grade`;
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `submitted_at` timestamp NULL DEFAULT NULL AFTER `status`;
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `graded_at` timestamp NULL DEFAULT NULL AFTER `submitted_at`;
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `graded_at`;
ALTER TABLE `cbt_results` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `cbt_tests`
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `test_title` varchar(255) NOT NULL AFTER `id`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `description` text DEFAULT NULL AFTER `test_title`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `teacher_id` int(11) NOT NULL AFTER `description`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `subject_id` int(11) NOT NULL AFTER `teacher_id`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `class_id` int(11) NOT NULL AFTER `subject_id`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `class_ids` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT 'JSON array of class IDs this test serves' CHECK (json_valid(`class_ids`)) AFTER `class_id`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `session_id` int(11) NOT NULL AFTER `class_ids`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `student_id` int(11) DEFAULT NULL COMMENT 'NULL for class-wide tests, specific student ID for individual tests' AFTER `session_id`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `duration_minutes` int(11) NOT NULL DEFAULT 60 AFTER `student_id`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `total_questions` int(11) NOT NULL DEFAULT 0 AFTER `duration_minutes`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `display_questions` int(11) DEFAULT NULL AFTER `total_questions`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `passing_score` decimal(5,2) NOT NULL DEFAULT 50.00 AFTER `display_questions`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `status` enum('draft','active','completed','archived') NOT NULL DEFAULT 'draft' AFTER `passing_score`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `test_type` enum('class','subject') DEFAULT 'class' COMMENT 'class = single class, subject = multi-class' AFTER `status`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `test_type`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `start_date` datetime DEFAULT NULL AFTER `updated_at`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `end_date` datetime DEFAULT NULL AFTER `start_date`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `sync_status` varchar(50) DEFAULT 'synced' COMMENT 'Sync status: synced (default) | local (pending push) | conflict (resolved locally)' AFTER `end_date`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `local_modified_at` timestamp NULL DEFAULT NULL COMMENT 'Timestamp when test was last modified in offline mode. Used for conflict detection.' AFTER `sync_status`;
ALTER TABLE `cbt_tests` ADD COLUMN IF NOT EXISTS `sync_source` varchar(50) DEFAULT NULL COMMENT 'Indicates record origin during sync: local | online | null (both sources)' AFTER `local_modified_at`;

-- Columns for `cbt_test_assignments`
ALTER TABLE `cbt_test_assignments` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `cbt_test_assignments` ADD COLUMN IF NOT EXISTS `test_id` int(11) NOT NULL AFTER `id`;
ALTER TABLE `cbt_test_assignments` ADD COLUMN IF NOT EXISTS `student_id` int(11) NOT NULL AFTER `test_id`;
ALTER TABLE `cbt_test_assignments` ADD COLUMN IF NOT EXISTS `assigned_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `student_id`;
ALTER TABLE `cbt_test_assignments` ADD COLUMN IF NOT EXISTS `accessed_at` timestamp NULL DEFAULT NULL AFTER `assigned_at`;
ALTER TABLE `cbt_test_assignments` ADD COLUMN IF NOT EXISTS `completed_at` timestamp NULL DEFAULT NULL AFTER `accessed_at`;
ALTER TABLE `cbt_test_assignments` ADD COLUMN IF NOT EXISTS `status` enum('assigned','accessed','completed') NOT NULL DEFAULT 'assigned' AFTER `completed_at`;
ALTER TABLE `cbt_test_assignments` ADD COLUMN IF NOT EXISTS `sync_source` varchar(50) DEFAULT 'online' COMMENT 'Record origin: online (default) | local (created offline)' AFTER `status`;
ALTER TABLE `cbt_test_assignments` ADD COLUMN IF NOT EXISTS `created_offline_at` timestamp NULL DEFAULT NULL COMMENT 'Timestamp when assignment was created in offline mode' AFTER `sync_source`;

-- Columns for `cbt_test_classes`
ALTER TABLE `cbt_test_classes` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `cbt_test_classes` ADD COLUMN IF NOT EXISTS `test_id` int(11) NOT NULL AFTER `id`;
ALTER TABLE `cbt_test_classes` ADD COLUMN IF NOT EXISTS `class_id` int(11) NOT NULL AFTER `test_id`;
ALTER TABLE `cbt_test_classes` ADD COLUMN IF NOT EXISTS `created_at` timestamp NULL DEFAULT current_timestamp() AFTER `class_id`;

-- Columns for `classes`
ALTER TABLE `classes` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `classes` ADD COLUMN IF NOT EXISTS `section_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `id`;
ALTER TABLE `classes` ADD COLUMN IF NOT EXISTS `class_name` varchar(50) NOT NULL AFTER `section_id`;
ALTER TABLE `classes` ADD COLUMN IF NOT EXISTS `section` varchar(20) DEFAULT NULL AFTER `class_name`;
ALTER TABLE `classes` ADD COLUMN IF NOT EXISTS `class_teacher_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `section`;
ALTER TABLE `classes` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `class_teacher_id`;

-- Columns for `discipline_records`
ALTER TABLE `discipline_records` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `discipline_records` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `discipline_records` ADD COLUMN IF NOT EXISTS `incident_date` date NOT NULL AFTER `student_id`;
ALTER TABLE `discipline_records` ADD COLUMN IF NOT EXISTS `incident_type` varchar(100) DEFAULT NULL AFTER `incident_date`;
ALTER TABLE `discipline_records` ADD COLUMN IF NOT EXISTS `severity` varchar(50) DEFAULT 'minor' AFTER `incident_type`;
ALTER TABLE `discipline_records` ADD COLUMN IF NOT EXISTS `description` text DEFAULT NULL AFTER `severity`;
ALTER TABLE `discipline_records` ADD COLUMN IF NOT EXISTS `action_taken` text DEFAULT NULL AFTER `description`;
ALTER TABLE `discipline_records` ADD COLUMN IF NOT EXISTS `reported_by` bigint(20) UNSIGNED NOT NULL AFTER `action_taken`;
ALTER TABLE `discipline_records` ADD COLUMN IF NOT EXISTS `parent_notified` tinyint(1) DEFAULT 0 AFTER `reported_by`;
ALTER TABLE `discipline_records` ADD COLUMN IF NOT EXISTS `notification_date` date DEFAULT NULL AFTER `parent_notified`;
ALTER TABLE `discipline_records` ADD COLUMN IF NOT EXISTS `status` varchar(50) DEFAULT 'pending_action' AFTER `notification_date`;
ALTER TABLE `discipline_records` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `status`;

-- Columns for `events`
ALTER TABLE `events` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `events` ADD COLUMN IF NOT EXISTS `event_name` varchar(200) NOT NULL AFTER `id`;
ALTER TABLE `events` ADD COLUMN IF NOT EXISTS `event_description` text DEFAULT NULL AFTER `event_name`;
ALTER TABLE `events` ADD COLUMN IF NOT EXISTS `event_type` varchar(50) DEFAULT 'general' AFTER `event_description`;
ALTER TABLE `events` ADD COLUMN IF NOT EXISTS `session_year` varchar(20) NOT NULL DEFAULT '2025/2026' AFTER `event_type`;
ALTER TABLE `events` ADD COLUMN IF NOT EXISTS `term` varchar(10) DEFAULT NULL AFTER `session_year`;
ALTER TABLE `events` ADD COLUMN IF NOT EXISTS `event_date` date NOT NULL AFTER `term`;
ALTER TABLE `events` ADD COLUMN IF NOT EXISTS `start_time` time DEFAULT NULL AFTER `event_date`;
ALTER TABLE `events` ADD COLUMN IF NOT EXISTS `end_time` time DEFAULT NULL AFTER `start_time`;
ALTER TABLE `events` ADD COLUMN IF NOT EXISTS `location` varchar(255) DEFAULT NULL AFTER `end_time`;
ALTER TABLE `events` ADD COLUMN IF NOT EXISTS `target_class_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `location`;
ALTER TABLE `events` ADD COLUMN IF NOT EXISTS `created_by` bigint(20) UNSIGNED NOT NULL AFTER `target_class_id`;
ALTER TABLE `events` ADD COLUMN IF NOT EXISTS `is_public` tinyint(1) DEFAULT 1 AFTER `created_by`;
ALTER TABLE `events` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `is_public`;

-- Columns for `extracurricular_activities`
ALTER TABLE `extracurricular_activities` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `extracurricular_activities` ADD COLUMN IF NOT EXISTS `activity_name` varchar(100) NOT NULL AFTER `id`;
ALTER TABLE `extracurricular_activities` ADD COLUMN IF NOT EXISTS `activity_type` varchar(50) DEFAULT NULL AFTER `activity_name`;
ALTER TABLE `extracurricular_activities` ADD COLUMN IF NOT EXISTS `description` text DEFAULT NULL AFTER `activity_type`;
ALTER TABLE `extracurricular_activities` ADD COLUMN IF NOT EXISTS `start_date` date DEFAULT NULL AFTER `description`;
ALTER TABLE `extracurricular_activities` ADD COLUMN IF NOT EXISTS `end_date` date DEFAULT NULL AFTER `start_date`;
ALTER TABLE `extracurricular_activities` ADD COLUMN IF NOT EXISTS `meeting_day` varchar(20) DEFAULT NULL AFTER `end_date`;
ALTER TABLE `extracurricular_activities` ADD COLUMN IF NOT EXISTS `meeting_time` time DEFAULT NULL AFTER `meeting_day`;
ALTER TABLE `extracurricular_activities` ADD COLUMN IF NOT EXISTS `location` varchar(255) DEFAULT NULL AFTER `meeting_time`;
ALTER TABLE `extracurricular_activities` ADD COLUMN IF NOT EXISTS `coordinator_id` bigint(20) UNSIGNED NOT NULL AFTER `location`;
ALTER TABLE `extracurricular_activities` ADD COLUMN IF NOT EXISTS `session_year` varchar(20) DEFAULT '2025/2026' AFTER `coordinator_id`;
ALTER TABLE `extracurricular_activities` ADD COLUMN IF NOT EXISTS `is_active` tinyint(1) DEFAULT 1 AFTER `session_year`;
ALTER TABLE `extracurricular_activities` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `is_active`;
ALTER TABLE `extracurricular_activities` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `fees`
ALTER TABLE `fees` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `fees` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `fees` ADD COLUMN IF NOT EXISTS `session_id` varchar(20) NOT NULL AFTER `student_id`;
ALTER TABLE `fees` ADD COLUMN IF NOT EXISTS `student_type` enum('0','1') NOT NULL AFTER `session_id`;
ALTER TABLE `fees` ADD COLUMN IF NOT EXISTS `amount_due` decimal(12,2) NOT NULL AFTER `student_type`;
ALTER TABLE `fees` ADD COLUMN IF NOT EXISTS `amount_paid` decimal(12,2) DEFAULT 0.00 AFTER `amount_due`;
ALTER TABLE `fees` ADD COLUMN IF NOT EXISTS `balance` decimal(12,2) GENERATED ALWAYS AS (`amount_due` - `amount_paid`) STORED AFTER `amount_paid`;
ALTER TABLE `fees` ADD COLUMN IF NOT EXISTS `due_date` date DEFAULT NULL AFTER `balance`;
ALTER TABLE `fees` ADD COLUMN IF NOT EXISTS `fee_items` varchar(200) NOT NULL AFTER `due_date`;
ALTER TABLE `fees` ADD COLUMN IF NOT EXISTS `payment_date` date DEFAULT NULL AFTER `fee_items`;
ALTER TABLE `fees` ADD COLUMN IF NOT EXISTS `payment_method` varchar(50) DEFAULT NULL AFTER `payment_date`;
ALTER TABLE `fees` ADD COLUMN IF NOT EXISTS `receipt_number` varchar(50) DEFAULT NULL AFTER `payment_method`;
ALTER TABLE `fees` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `receipt_number`;

-- Columns for `feess`
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `regno` int(11) NOT NULL FIRST;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `name` varchar(900) NOT NULL AFTER `regno`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `grade` varchar(900) NOT NULL AFTER `name`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `vocational` varchar(900) NOT NULL AFTER `grade`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `uniform` varchar(900) NOT NULL AFTER `vocational`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `clubsport` varchar(900) NOT NULL AFTER `uniform`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `practical` varchar(900) NOT NULL AFTER `clubsport`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `ictfee` varchar(900) NOT NULL AFTER `practical`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `xmas` varchar(900) NOT NULL AFTER `ictfee`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `caution` varchar(900) NOT NULL AFTER `xmas`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `graduation` varchar(900) NOT NULL AFTER `caution`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `pastquestion` varchar(900) NOT NULL AFTER `graduation`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `externalexam` varchar(900) NOT NULL AFTER `pastquestion`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `igsce` varchar(900) NOT NULL AFTER `externalexam`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `1stterm` varchar(900) NOT NULL AFTER `igsce`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `2ndterm` varchar(900) NOT NULL AFTER `1stterm`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `3rdterm` varchar(900) NOT NULL AFTER `2ndterm`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `total` varchar(900) NOT NULL AFTER `3rdterm`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `nbn` varchar(900) NOT NULL AFTER `total`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `nbna` varchar(900) NOT NULL AFTER `nbn`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w2` varchar(900) NOT NULL AFTER `nbna`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w3` varchar(900) NOT NULL AFTER `w2`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w4` varchar(900) NOT NULL AFTER `w3`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w22` varchar(900) NOT NULL AFTER `w4`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w33` varchar(900) NOT NULL AFTER `w22`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w44` varchar(900) NOT NULL AFTER `w33`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w222` varchar(900) NOT NULL AFTER `w44`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w333` varchar(900) NOT NULL AFTER `w222`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w444` varchar(900) NOT NULL AFTER `w333`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w122` varchar(900) NOT NULL AFTER `w444`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w212` varchar(900) NOT NULL AFTER `w122`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w233` varchar(900) NOT NULL AFTER `w212`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w244` varchar(900) NOT NULL AFTER `w233`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w344` varchar(900) NOT NULL AFTER `w244`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w3444` varchar(900) NOT NULL AFTER `w344`;
ALTER TABLE `feess` ADD COLUMN IF NOT EXISTS `w544` varchar(900) NOT NULL AFTER `w3444`;

-- Columns for `fee_charge_details`
ALTER TABLE `fee_charge_details` ADD COLUMN IF NOT EXISTS `fee_id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `fee_charge_details` ADD COLUMN IF NOT EXISTS `charge_kind` enum('normal','other') NOT NULL AFTER `fee_id`;
ALTER TABLE `fee_charge_details` ADD COLUMN IF NOT EXISTS `title` varchar(150) NOT NULL AFTER `charge_kind`;
ALTER TABLE `fee_charge_details` ADD COLUMN IF NOT EXISTS `description` text NOT NULL AFTER `title`;
ALTER TABLE `fee_charge_details` ADD COLUMN IF NOT EXISTS `category` varchar(150) NOT NULL DEFAULT '' AFTER `description`;
ALTER TABLE `fee_charge_details` ADD COLUMN IF NOT EXISTS `class_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `category`;
ALTER TABLE `fee_charge_details` ADD COLUMN IF NOT EXISTS `student_name` varchar(255) NOT NULL AFTER `class_id`;
ALTER TABLE `fee_charge_details` ADD COLUMN IF NOT EXISTS `items_snapshot` longtext NOT NULL AFTER `student_name`;
ALTER TABLE `fee_charge_details` ADD COLUMN IF NOT EXISTS `created_by` bigint(20) UNSIGNED NOT NULL AFTER `items_snapshot`;
ALTER TABLE `fee_charge_details` ADD COLUMN IF NOT EXISTS `created_by_name` varchar(255) NOT NULL AFTER `created_by`;
ALTER TABLE `fee_charge_details` ADD COLUMN IF NOT EXISTS `created_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `created_by_name`;

-- Columns for `fee_discounts`
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `fee_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `discount_id` bigint(20) UNSIGNED NOT NULL AFTER `fee_id`;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `discount_id`;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `name` varchar(150) NOT NULL AFTER `student_id`;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `kind` enum('predefined','custom') NOT NULL AFTER `name`;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `value_type` enum('percentage','fixed') NOT NULL AFTER `kind`;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `value` decimal(12,2) NOT NULL AFTER `value_type`;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `original_amount` decimal(12,2) NOT NULL AFTER `value`;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `eligible_balance` decimal(12,2) NOT NULL AFTER `original_amount`;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `discount_amount` decimal(12,2) NOT NULL AFTER `eligible_balance`;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `payable_amount` decimal(12,2) NOT NULL AFTER `discount_amount`;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `definition_snapshot` longtext NOT NULL AFTER `payable_amount`;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `applied_by` bigint(20) UNSIGNED NOT NULL AFTER `definition_snapshot`;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `applied_by_name` varchar(255) NOT NULL AFTER `applied_by`;
ALTER TABLE `fee_discounts` ADD COLUMN IF NOT EXISTS `applied_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `applied_by_name`;

-- Columns for `fee_items`
ALTER TABLE `fee_items` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `fee_items` ADD COLUMN IF NOT EXISTS `fee_name` varchar(100) NOT NULL AFTER `id`;
ALTER TABLE `fee_items` ADD COLUMN IF NOT EXISTS `class_id` int(11) NOT NULL AFTER `fee_name`;
ALTER TABLE `fee_items` ADD COLUMN IF NOT EXISTS `new_amount` int(11) NOT NULL AFTER `class_id`;
ALTER TABLE `fee_items` ADD COLUMN IF NOT EXISTS `old_amount` int(11) NOT NULL AFTER `new_amount`;
ALTER TABLE `fee_items` ADD COLUMN IF NOT EXISTS `compulsory` tinyint(1) NOT NULL AFTER `old_amount`;
ALTER TABLE `fee_items` ADD COLUMN IF NOT EXISTS `session_id` int(11) NOT NULL AFTER `compulsory`;

-- Columns for `fee_payments`
ALTER TABLE `fee_payments` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `fee_payments` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `fee_payments` ADD COLUMN IF NOT EXISTS `session_id` varchar(20) NOT NULL AFTER `student_id`;
ALTER TABLE `fee_payments` ADD COLUMN IF NOT EXISTS `amount_paid` decimal(12,2) NOT NULL AFTER `session_id`;
ALTER TABLE `fee_payments` ADD COLUMN IF NOT EXISTS `payment_date` date NOT NULL AFTER `amount_paid`;
ALTER TABLE `fee_payments` ADD COLUMN IF NOT EXISTS `payment_method` varchar(40) DEFAULT NULL AFTER `payment_date`;
ALTER TABLE `fee_payments` ADD COLUMN IF NOT EXISTS `receipt_number` varchar(80) NOT NULL AFTER `payment_method`;
ALTER TABLE `fee_payments` ADD COLUMN IF NOT EXISTS `settlement_reference` varchar(100) NOT NULL AFTER `receipt_number`;
ALTER TABLE `fee_payments` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `settlement_reference`;

-- Columns for `fee_payment_allocations`
ALTER TABLE `fee_payment_allocations` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `fee_payment_allocations` ADD COLUMN IF NOT EXISTS `payment_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `fee_payment_allocations` ADD COLUMN IF NOT EXISTS `fee_id` bigint(20) UNSIGNED NOT NULL AFTER `payment_id`;
ALTER TABLE `fee_payment_allocations` ADD COLUMN IF NOT EXISTS `amount_applied` decimal(12,2) NOT NULL AFTER `fee_id`;
ALTER TABLE `fee_payment_allocations` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `amount_applied`;

-- Columns for `fee_payment_details`
ALTER TABLE `fee_payment_details` ADD COLUMN IF NOT EXISTS `payment_id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `fee_payment_details` ADD COLUMN IF NOT EXISTS `fee_id` bigint(20) UNSIGNED NOT NULL AFTER `payment_id`;
ALTER TABLE `fee_payment_details` ADD COLUMN IF NOT EXISTS `title` varchar(150) NOT NULL AFTER `fee_id`;
ALTER TABLE `fee_payment_details` ADD COLUMN IF NOT EXISTS `charge_kind` enum('normal','other') NOT NULL AFTER `title`;
ALTER TABLE `fee_payment_details` ADD COLUMN IF NOT EXISTS `original_amount` decimal(12,2) NOT NULL AFTER `charge_kind`;
ALTER TABLE `fee_payment_details` ADD COLUMN IF NOT EXISTS `discount_amount` decimal(12,2) NOT NULL AFTER `original_amount`;
ALTER TABLE `fee_payment_details` ADD COLUMN IF NOT EXISTS `amount_due` decimal(12,2) NOT NULL AFTER `discount_amount`;
ALTER TABLE `fee_payment_details` ADD COLUMN IF NOT EXISTS `balance` decimal(12,2) NOT NULL AFTER `amount_due`;
ALTER TABLE `fee_payment_details` ADD COLUMN IF NOT EXISTS `received_by_id` bigint(20) UNSIGNED NOT NULL AFTER `balance`;
ALTER TABLE `fee_payment_details` ADD COLUMN IF NOT EXISTS `received_by` varchar(255) NOT NULL AFTER `received_by_id`;
ALTER TABLE `fee_payment_details` ADD COLUMN IF NOT EXISTS `student_name` varchar(255) NOT NULL AFTER `received_by`;

-- Columns for `grading_keys`
ALTER TABLE `grading_keys` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `grading_keys` ADD COLUMN IF NOT EXISTS `min_score` int(11) DEFAULT NULL AFTER `id`;
ALTER TABLE `grading_keys` ADD COLUMN IF NOT EXISTS `max_score` int(11) DEFAULT NULL AFTER `min_score`;
ALTER TABLE `grading_keys` ADD COLUMN IF NOT EXISTS `grade` varchar(10) NOT NULL AFTER `max_score`;
ALTER TABLE `grading_keys` ADD COLUMN IF NOT EXISTS `remark` varchar(50) NOT NULL AFTER `grade`;
ALTER TABLE `grading_keys` ADD COLUMN IF NOT EXISTS `color_hex` varchar(7) DEFAULT NULL AFTER `remark`;
ALTER TABLE `grading_keys` ADD COLUMN IF NOT EXISTS `sort_order` int(11) DEFAULT 0 AFTER `color_hex`;
ALTER TABLE `grading_keys` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `sort_order`;
ALTER TABLE `grading_keys` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `grading_playclass`
ALTER TABLE `grading_playclass` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `grading_playclass` ADD COLUMN IF NOT EXISTS `grade` varchar(5) NOT NULL AFTER `id`;
ALTER TABLE `grading_playclass` ADD COLUMN IF NOT EXISTS `remark` varchar(50) NOT NULL AFTER `grade`;
ALTER TABLE `grading_playclass` ADD COLUMN IF NOT EXISTS `created_at` timestamp NULL DEFAULT current_timestamp() AFTER `remark`;

-- Columns for `inventory_categories`
ALTER TABLE `inventory_categories` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `inventory_categories` ADD COLUMN IF NOT EXISTS `name` varchar(150) NOT NULL AFTER `id`;
ALTER TABLE `inventory_categories` ADD COLUMN IF NOT EXISTS `description` text DEFAULT NULL AFTER `name`;
ALTER TABLE `inventory_categories` ADD COLUMN IF NOT EXISTS `is_active` tinyint(1) NOT NULL DEFAULT 1 AFTER `description`;
ALTER TABLE `inventory_categories` ADD COLUMN IF NOT EXISTS `version` int(10) UNSIGNED NOT NULL DEFAULT 1 AFTER `is_active`;
ALTER TABLE `inventory_categories` ADD COLUMN IF NOT EXISTS `created_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `version`;
ALTER TABLE `inventory_categories` ADD COLUMN IF NOT EXISTS `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `inventory_issuances`
ALTER TABLE `inventory_issuances` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `inventory_issuances` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `id`;
ALTER TABLE `inventory_issuances` ADD COLUMN IF NOT EXISTS `recipient` varchar(255) NOT NULL AFTER `student_id`;
ALTER TABLE `inventory_issuances` ADD COLUMN IF NOT EXISTS `department` varchar(150) NOT NULL DEFAULT '' AFTER `recipient`;
ALTER TABLE `inventory_issuances` ADD COLUMN IF NOT EXISTS `date` date NOT NULL AFTER `department`;
ALTER TABLE `inventory_issuances` ADD COLUMN IF NOT EXISTS `reference` varchar(150) NOT NULL AFTER `date`;
ALTER TABLE `inventory_issuances` ADD COLUMN IF NOT EXISTS `notes` text NOT NULL AFTER `reference`;
ALTER TABLE `inventory_issuances` ADD COLUMN IF NOT EXISTS `issued_by` bigint(20) UNSIGNED NOT NULL AFTER `notes`;
ALTER TABLE `inventory_issuances` ADD COLUMN IF NOT EXISTS `issued_by_name` varchar(255) NOT NULL AFTER `issued_by`;
ALTER TABLE `inventory_issuances` ADD COLUMN IF NOT EXISTS `request_id` varchar(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL AFTER `issued_by_name`;
ALTER TABLE `inventory_issuances` ADD COLUMN IF NOT EXISTS `request_hash` char(64) NOT NULL AFTER `request_id`;
ALTER TABLE `inventory_issuances` ADD COLUMN IF NOT EXISTS `created_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `request_hash`;

-- Columns for `inventory_issuance_items`
ALTER TABLE `inventory_issuance_items` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `inventory_issuance_items` ADD COLUMN IF NOT EXISTS `issuance_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `inventory_issuance_items` ADD COLUMN IF NOT EXISTS `inventory_item_id` bigint(20) UNSIGNED NOT NULL AFTER `issuance_id`;
ALTER TABLE `inventory_issuance_items` ADD COLUMN IF NOT EXISTS `movement_id` bigint(20) UNSIGNED NOT NULL AFTER `inventory_item_id`;

-- Columns for `inventory_items`
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `name` varchar(150) NOT NULL AFTER `id`;
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `sku` varchar(80) NOT NULL AFTER `name`;
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `category_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `sku`;
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `unit` varchar(40) NOT NULL AFTER `category_id`;
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `reorder_level` decimal(12,2) NOT NULL DEFAULT 0.00 AFTER `unit`;
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `current_quantity` decimal(12,2) NOT NULL DEFAULT 0.00 AFTER `reorder_level`;
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `unit_cost` decimal(18,4) NOT NULL DEFAULT 0.0000 AFTER `current_quantity`;
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `stock_value` decimal(24,4) NOT NULL DEFAULT 0.0000 AFTER `unit_cost`;
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `location` varchar(200) NOT NULL DEFAULT '' AFTER `stock_value`;
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `description` text DEFAULT NULL AFTER `location`;
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `is_active` tinyint(1) NOT NULL DEFAULT 1 AFTER `description`;
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `version` int(10) UNSIGNED NOT NULL DEFAULT 1 AFTER `is_active`;
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `created_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `version`;
ALTER TABLE `inventory_items` ADD COLUMN IF NOT EXISTS `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `inventory_movements`
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `item_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `item_name` varchar(150) NOT NULL AFTER `item_id`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `sku` varchar(80) NOT NULL AFTER `item_name`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `unit` varchar(40) NOT NULL AFTER `sku`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `type` enum('in','out','adjustment') NOT NULL AFTER `unit`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `direction` enum('increase','decrease') NOT NULL AFTER `type`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `quantity` decimal(12,2) NOT NULL AFTER `direction`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `previous_quantity` decimal(12,2) NOT NULL AFTER `quantity`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `new_quantity` decimal(12,2) NOT NULL AFTER `previous_quantity`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `unit_cost` decimal(18,4) NOT NULL AFTER `new_quantity`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `total_cost` decimal(24,4) NOT NULL AFTER `unit_cost`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `previous_value` decimal(24,4) NOT NULL AFTER `total_cost`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `new_value` decimal(24,4) NOT NULL AFTER `previous_value`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `date` date NOT NULL AFTER `new_value`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `reference` varchar(150) NOT NULL DEFAULT '' AFTER `date`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `supplier_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `reference`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `supplier_name` varchar(150) NOT NULL DEFAULT '' AFTER `supplier_id`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `recipient` varchar(150) NOT NULL DEFAULT '' AFTER `supplier_name`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `department` varchar(150) NOT NULL DEFAULT '' AFTER `recipient`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `notes` text DEFAULT NULL AFTER `department`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `performed_by_id` bigint(20) UNSIGNED NOT NULL AFTER `notes`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `performed_by` varchar(150) NOT NULL AFTER `performed_by_id`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `request_id` varchar(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL AFTER `performed_by`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `request_hash` char(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL AFTER `request_id`;
ALTER TABLE `inventory_movements` ADD COLUMN IF NOT EXISTS `created_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `request_hash`;

-- Columns for `inventory_suppliers`
ALTER TABLE `inventory_suppliers` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `inventory_suppliers` ADD COLUMN IF NOT EXISTS `name` varchar(150) NOT NULL AFTER `id`;
ALTER TABLE `inventory_suppliers` ADD COLUMN IF NOT EXISTS `contact_person` varchar(150) NOT NULL DEFAULT '' AFTER `name`;
ALTER TABLE `inventory_suppliers` ADD COLUMN IF NOT EXISTS `phone` varchar(40) NOT NULL DEFAULT '' AFTER `contact_person`;
ALTER TABLE `inventory_suppliers` ADD COLUMN IF NOT EXISTS `email` varchar(254) NOT NULL DEFAULT '' AFTER `phone`;
ALTER TABLE `inventory_suppliers` ADD COLUMN IF NOT EXISTS `address` text DEFAULT NULL AFTER `email`;
ALTER TABLE `inventory_suppliers` ADD COLUMN IF NOT EXISTS `notes` text DEFAULT NULL AFTER `address`;
ALTER TABLE `inventory_suppliers` ADD COLUMN IF NOT EXISTS `supplied_items` text DEFAULT NULL AFTER `notes`;
ALTER TABLE `inventory_suppliers` ADD COLUMN IF NOT EXISTS `is_active` tinyint(1) NOT NULL DEFAULT 1 AFTER `supplied_items`;
ALTER TABLE `inventory_suppliers` ADD COLUMN IF NOT EXISTS `version` int(10) UNSIGNED NOT NULL DEFAULT 1 AFTER `is_active`;
ALTER TABLE `inventory_suppliers` ADD COLUMN IF NOT EXISTS `created_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `version`;
ALTER TABLE `inventory_suppliers` ADD COLUMN IF NOT EXISTS `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `library_books`
ALTER TABLE `library_books` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `library_books` ADD COLUMN IF NOT EXISTS `book_title` varchar(255) NOT NULL AFTER `id`;
ALTER TABLE `library_books` ADD COLUMN IF NOT EXISTS `author` varchar(255) DEFAULT NULL AFTER `book_title`;
ALTER TABLE `library_books` ADD COLUMN IF NOT EXISTS `published_year` smallint(4) DEFAULT NULL AFTER `author`;
ALTER TABLE `library_books` ADD COLUMN IF NOT EXISTS `book_author` varchar(150) DEFAULT NULL AFTER `published_year`;
ALTER TABLE `library_books` ADD COLUMN IF NOT EXISTS `isbn` varchar(20) DEFAULT NULL AFTER `book_author`;
ALTER TABLE `library_books` ADD COLUMN IF NOT EXISTS `book_code` varchar(50) NOT NULL AFTER `isbn`;
ALTER TABLE `library_books` ADD COLUMN IF NOT EXISTS `category` varchar(100) DEFAULT NULL AFTER `book_code`;
ALTER TABLE `library_books` ADD COLUMN IF NOT EXISTS `publication_year` int(11) DEFAULT NULL AFTER `category`;
ALTER TABLE `library_books` ADD COLUMN IF NOT EXISTS `publisher` varchar(150) DEFAULT NULL AFTER `publication_year`;
ALTER TABLE `library_books` ADD COLUMN IF NOT EXISTS `total_copies` int(11) DEFAULT 1 AFTER `publisher`;
ALTER TABLE `library_books` ADD COLUMN IF NOT EXISTS `available_copies` int(11) DEFAULT 1 AFTER `total_copies`;
ALTER TABLE `library_books` ADD COLUMN IF NOT EXISTS `book_description` text DEFAULT NULL AFTER `available_copies`;
ALTER TABLE `library_books` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `book_description`;

-- Columns for `library_borrowing`
ALTER TABLE `library_borrowing` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `library_borrowing` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `library_borrowing` ADD COLUMN IF NOT EXISTS `book_id` bigint(20) UNSIGNED NOT NULL AFTER `student_id`;
ALTER TABLE `library_borrowing` ADD COLUMN IF NOT EXISTS `borrow_date` date NOT NULL AFTER `book_id`;
ALTER TABLE `library_borrowing` ADD COLUMN IF NOT EXISTS `due_date` date NOT NULL AFTER `borrow_date`;
ALTER TABLE `library_borrowing` ADD COLUMN IF NOT EXISTS `return_date` date DEFAULT NULL AFTER `due_date`;
ALTER TABLE `library_borrowing` ADD COLUMN IF NOT EXISTS `status` varchar(50) DEFAULT 'borrowed' AFTER `return_date`;
ALTER TABLE `library_borrowing` ADD COLUMN IF NOT EXISTS `fine_amount` decimal(10,2) DEFAULT 0.00 AFTER `status`;
ALTER TABLE `library_borrowing` ADD COLUMN IF NOT EXISTS `fine_paid` tinyint(1) DEFAULT 0 AFTER `fine_amount`;
ALTER TABLE `library_borrowing` ADD COLUMN IF NOT EXISTS `librarian_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `fine_paid`;
ALTER TABLE `library_borrowing` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `librarian_id`;

-- Columns for `lms_activity_log`
ALTER TABLE `lms_activity_log` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `lms_activity_log` ADD COLUMN IF NOT EXISTS `user_id` int(11) NOT NULL COMMENT 'Teacher or admin ID' AFTER `id`;
ALTER TABLE `lms_activity_log` ADD COLUMN IF NOT EXISTS `user_type` varchar(50) DEFAULT NULL COMMENT 'teacher, admin, student' AFTER `user_id`;
ALTER TABLE `lms_activity_log` ADD COLUMN IF NOT EXISTS `action` varchar(255) NOT NULL COMMENT 'create_topic, update_topic, upload_resource, etc' AFTER `user_type`;
ALTER TABLE `lms_activity_log` ADD COLUMN IF NOT EXISTS `object_type` varchar(50) DEFAULT NULL COMMENT 'topic, subtopic, resource, announcement' AFTER `action`;
ALTER TABLE `lms_activity_log` ADD COLUMN IF NOT EXISTS `object_id` int(11) DEFAULT NULL COMMENT 'ID of affected object' AFTER `object_type`;
ALTER TABLE `lms_activity_log` ADD COLUMN IF NOT EXISTS `description` varchar(500) DEFAULT NULL AFTER `object_id`;
ALTER TABLE `lms_activity_log` ADD COLUMN IF NOT EXISTS `ip_address` varchar(45) DEFAULT NULL AFTER `description`;
ALTER TABLE `lms_activity_log` ADD COLUMN IF NOT EXISTS `created_at` timestamp NULL DEFAULT current_timestamp() AFTER `ip_address`;

-- Columns for `lms_announcements`
ALTER TABLE `lms_announcements` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `lms_announcements` ADD COLUMN IF NOT EXISTS `teacher_id` int(11) NOT NULL COMMENT 'FK to teachers table' AFTER `id`;
ALTER TABLE `lms_announcements` ADD COLUMN IF NOT EXISTS `topic_id` int(11) DEFAULT NULL COMMENT 'FK to lms_topics or NULL for general announcement' AFTER `teacher_id`;
ALTER TABLE `lms_announcements` ADD COLUMN IF NOT EXISTS `title` varchar(255) NOT NULL AFTER `topic_id`;
ALTER TABLE `lms_announcements` ADD COLUMN IF NOT EXISTS `message` longtext NOT NULL COMMENT 'Rich text message content' AFTER `title`;
ALTER TABLE `lms_announcements` ADD COLUMN IF NOT EXISTS `visibility` varchar(50) DEFAULT 'public' COMMENT 'public, class-specific, private, draft' AFTER `message`;
ALTER TABLE `lms_announcements` ADD COLUMN IF NOT EXISTS `is_pinned` tinyint(1) DEFAULT 0 AFTER `visibility`;
ALTER TABLE `lms_announcements` ADD COLUMN IF NOT EXISTS `pin_until` timestamp NULL DEFAULT NULL COMMENT 'When to auto-unpin announcement' AFTER `is_pinned`;
ALTER TABLE `lms_announcements` ADD COLUMN IF NOT EXISTS `created_at` timestamp NULL DEFAULT current_timestamp() AFTER `pin_until`;
ALTER TABLE `lms_announcements` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `lms_feedback`
ALTER TABLE `lms_feedback` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `lms_feedback` ADD COLUMN IF NOT EXISTS `topic_id` int(11) NOT NULL COMMENT 'FK to lms_topics' AFTER `id`;
ALTER TABLE `lms_feedback` ADD COLUMN IF NOT EXISTS `subtopic_id` int(11) DEFAULT NULL COMMENT 'FK to lms_subtopics' AFTER `topic_id`;
ALTER TABLE `lms_feedback` ADD COLUMN IF NOT EXISTS `student_id` int(11) DEFAULT NULL COMMENT 'FK to students table (NULL for anonymous)' AFTER `subtopic_id`;
ALTER TABLE `lms_feedback` ADD COLUMN IF NOT EXISTS `teacher_id` int(11) DEFAULT NULL COMMENT 'FK to teachers table (for reply)' AFTER `student_id`;
ALTER TABLE `lms_feedback` ADD COLUMN IF NOT EXISTS `comment_type` varchar(50) DEFAULT 'comment' COMMENT 'comment, question, feedback, issue' AFTER `teacher_id`;
ALTER TABLE `lms_feedback` ADD COLUMN IF NOT EXISTS `content` longtext NOT NULL AFTER `comment_type`;
ALTER TABLE `lms_feedback` ADD COLUMN IF NOT EXISTS `reply` longtext DEFAULT NULL COMMENT 'Teacher reply to the feedback' AFTER `content`;
ALTER TABLE `lms_feedback` ADD COLUMN IF NOT EXISTS `is_resolved` tinyint(1) DEFAULT 0 AFTER `reply`;
ALTER TABLE `lms_feedback` ADD COLUMN IF NOT EXISTS `created_at` timestamp NULL DEFAULT current_timestamp() AFTER `is_resolved`;
ALTER TABLE `lms_feedback` ADD COLUMN IF NOT EXISTS `replied_at` timestamp NULL DEFAULT NULL AFTER `created_at`;
ALTER TABLE `lms_feedback` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `replied_at`;

-- Columns for `lms_resources`
ALTER TABLE `lms_resources` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `lms_resources` ADD COLUMN IF NOT EXISTS `teacher_id` int(11) NOT NULL COMMENT 'FK to teachers table' AFTER `id`;
ALTER TABLE `lms_resources` ADD COLUMN IF NOT EXISTS `topic_id` int(11) DEFAULT NULL COMMENT 'FK to lms_topics (NULL if subtopic-specific)' AFTER `teacher_id`;
ALTER TABLE `lms_resources` ADD COLUMN IF NOT EXISTS `subtopic_id` int(11) DEFAULT NULL COMMENT 'FK to lms_subtopics (NULL if topic-level)' AFTER `topic_id`;
ALTER TABLE `lms_resources` ADD COLUMN IF NOT EXISTS `file_name` varchar(255) NOT NULL COMMENT 'Unique filename on server' AFTER `subtopic_id`;
ALTER TABLE `lms_resources` ADD COLUMN IF NOT EXISTS `original_name` varchar(255) NOT NULL COMMENT 'Original filename uploaded by user' AFTER `file_name`;
ALTER TABLE `lms_resources` ADD COLUMN IF NOT EXISTS `file_path` varchar(500) NOT NULL COMMENT 'Relative path to stored file' AFTER `original_name`;
ALTER TABLE `lms_resources` ADD COLUMN IF NOT EXISTS `file_type` varchar(50) NOT NULL COMMENT 'File extension: pdf, docx, pptx, jpg, mp4, etc' AFTER `file_path`;
ALTER TABLE `lms_resources` ADD COLUMN IF NOT EXISTS `file_size` int(11) NOT NULL COMMENT 'File size in bytes' AFTER `file_type`;
ALTER TABLE `lms_resources` ADD COLUMN IF NOT EXISTS `description` varchar(500) DEFAULT NULL COMMENT 'User-provided description' AFTER `file_size`;
ALTER TABLE `lms_resources` ADD COLUMN IF NOT EXISTS `download_count` int(11) DEFAULT 0 COMMENT 'Number of downloads' AFTER `description`;
ALTER TABLE `lms_resources` ADD COLUMN IF NOT EXISTS `created_at` timestamp NULL DEFAULT current_timestamp() AFTER `download_count`;
ALTER TABLE `lms_resources` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `lms_settings`
ALTER TABLE `lms_settings` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `lms_settings` ADD COLUMN IF NOT EXISTS `setting_key` varchar(255) NOT NULL AFTER `id`;
ALTER TABLE `lms_settings` ADD COLUMN IF NOT EXISTS `setting_value` longtext DEFAULT NULL COMMENT 'JSON, boolean, or string value' AFTER `setting_key`;
ALTER TABLE `lms_settings` ADD COLUMN IF NOT EXISTS `setting_type` varchar(50) DEFAULT 'string' COMMENT 'string, json, boolean, integer' AFTER `setting_value`;
ALTER TABLE `lms_settings` ADD COLUMN IF NOT EXISTS `description` varchar(255) DEFAULT NULL AFTER `setting_type`;
ALTER TABLE `lms_settings` ADD COLUMN IF NOT EXISTS `created_at` timestamp NULL DEFAULT current_timestamp() AFTER `description`;
ALTER TABLE `lms_settings` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `lms_student_progress`
ALTER TABLE `lms_student_progress` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `lms_student_progress` ADD COLUMN IF NOT EXISTS `student_id` int(11) NOT NULL COMMENT 'FK to students table' AFTER `id`;
ALTER TABLE `lms_student_progress` ADD COLUMN IF NOT EXISTS `topic_id` int(11) NOT NULL COMMENT 'FK to lms_topics' AFTER `student_id`;
ALTER TABLE `lms_student_progress` ADD COLUMN IF NOT EXISTS `subtopic_id` int(11) DEFAULT NULL COMMENT 'FK to lms_subtopics' AFTER `topic_id`;
ALTER TABLE `lms_student_progress` ADD COLUMN IF NOT EXISTS `status` varchar(50) DEFAULT 'not_started' COMMENT 'not_started, in_progress, completed, archived' AFTER `subtopic_id`;
ALTER TABLE `lms_student_progress` ADD COLUMN IF NOT EXISTS `completion_percentage` int(11) DEFAULT 0 COMMENT 'Percentage of content completed (0-100)' AFTER `status`;
ALTER TABLE `lms_student_progress` ADD COLUMN IF NOT EXISTS `time_spent_seconds` int(11) DEFAULT 0 COMMENT 'Total time spent in seconds' AFTER `completion_percentage`;
ALTER TABLE `lms_student_progress` ADD COLUMN IF NOT EXISTS `last_accessed` timestamp NULL DEFAULT NULL COMMENT 'Last time student accessed this topic' AFTER `time_spent_seconds`;
ALTER TABLE `lms_student_progress` ADD COLUMN IF NOT EXISTS `completed_at` timestamp NULL DEFAULT NULL COMMENT 'When student completed the topic' AFTER `last_accessed`;
ALTER TABLE `lms_student_progress` ADD COLUMN IF NOT EXISTS `notes` text DEFAULT NULL COMMENT 'Student notes or highlights' AFTER `completed_at`;
ALTER TABLE `lms_student_progress` ADD COLUMN IF NOT EXISTS `created_at` timestamp NULL DEFAULT current_timestamp() AFTER `notes`;
ALTER TABLE `lms_student_progress` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `lms_subtopics`
ALTER TABLE `lms_subtopics` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `lms_subtopics` ADD COLUMN IF NOT EXISTS `topic_id` int(11) NOT NULL COMMENT 'FK to lms_topics' AFTER `id`;
ALTER TABLE `lms_subtopics` ADD COLUMN IF NOT EXISTS `teacher_id` int(11) NOT NULL COMMENT 'FK to teachers table (denormalized for query performance)' AFTER `topic_id`;
ALTER TABLE `lms_subtopics` ADD COLUMN IF NOT EXISTS `title` varchar(255) NOT NULL AFTER `teacher_id`;
ALTER TABLE `lms_subtopics` ADD COLUMN IF NOT EXISTS `description` varchar(500) DEFAULT NULL COMMENT 'Brief description of subtopic' AFTER `title`;
ALTER TABLE `lms_subtopics` ADD COLUMN IF NOT EXISTS `subtopic_number` int(11) NOT NULL COMMENT 'Order/sequence number within the topic' AFTER `description`;
ALTER TABLE `lms_subtopics` ADD COLUMN IF NOT EXISTS `notes` longtext DEFAULT NULL COMMENT 'Rich text lesson notes for this subtopic' AFTER `subtopic_number`;
ALTER TABLE `lms_subtopics` ADD COLUMN IF NOT EXISTS `view_count` int(11) DEFAULT 0 AFTER `notes`;
ALTER TABLE `lms_subtopics` ADD COLUMN IF NOT EXISTS `created_at` timestamp NULL DEFAULT current_timestamp() AFTER `view_count`;
ALTER TABLE `lms_subtopics` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `lms_topics`
ALTER TABLE `lms_topics` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `lms_topics` ADD COLUMN IF NOT EXISTS `teacher_id` int(11) NOT NULL COMMENT 'FK to teachers table' AFTER `id`;
ALTER TABLE `lms_topics` ADD COLUMN IF NOT EXISTS `title` varchar(255) NOT NULL AFTER `teacher_id`;
ALTER TABLE `lms_topics` ADD COLUMN IF NOT EXISTS `description` longtext DEFAULT NULL COMMENT 'Topic description/overview' AFTER `title`;
ALTER TABLE `lms_topics` ADD COLUMN IF NOT EXISTS `content` longtext DEFAULT NULL COMMENT 'Rich text content of the topic' AFTER `description`;
ALTER TABLE `lms_topics` ADD COLUMN IF NOT EXISTS `status` varchar(50) DEFAULT 'draft' COMMENT 'draft, incomplete, published, completed, archived' AFTER `content`;
ALTER TABLE `lms_topics` ADD COLUMN IF NOT EXISTS `subject_id` int(11) DEFAULT NULL COMMENT 'Optional: FK to subjects table' AFTER `status`;
ALTER TABLE `lms_topics` ADD COLUMN IF NOT EXISTS `notes` longtext DEFAULT NULL COMMENT 'Teacher private notes for this topic' AFTER `subject_id`;
ALTER TABLE `lms_topics` ADD COLUMN IF NOT EXISTS `view_count` int(11) DEFAULT 0 COMMENT 'Number of times viewed by students' AFTER `notes`;
ALTER TABLE `lms_topics` ADD COLUMN IF NOT EXISTS `created_at` timestamp NULL DEFAULT current_timestamp() AFTER `view_count`;
ALTER TABLE `lms_topics` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;
ALTER TABLE `lms_topics` ADD COLUMN IF NOT EXISTS `topic_number` int(11) DEFAULT 0 COMMENT 'Order/sequence number for topic ordering' AFTER `updated_at`;

-- Columns for `lms_topic_classes`
ALTER TABLE `lms_topic_classes` ADD COLUMN IF NOT EXISTS `topic_id` int(11) NOT NULL FIRST;
ALTER TABLE `lms_topic_classes` ADD COLUMN IF NOT EXISTS `class_id` int(11) NOT NULL AFTER `topic_id`;

-- Columns for `mentor_remark`
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `class_id` int(11) NOT NULL AFTER `id`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `session_id` int(11) NOT NULL AFTER `class_id`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `student_id` int(11) NOT NULL AFTER `session_id`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `mentor_id` int(11) DEFAULT NULL AFTER `student_id`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `mentor_name` varchar(255) DEFAULT NULL AFTER `mentor_id`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `academics` text DEFAULT NULL AFTER `mentor_name`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `social` text DEFAULT NULL AFTER `academics`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `spirit` text DEFAULT NULL AFTER `social`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `hygiene` text DEFAULT NULL AFTER `spirit`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `general` text DEFAULT NULL AFTER `hygiene`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `remark` text DEFAULT NULL AFTER `general`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `academics2` text DEFAULT NULL AFTER `remark`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `social2` text DEFAULT NULL AFTER `academics2`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `spirit2` text DEFAULT NULL AFTER `social2`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `hygiene2` text DEFAULT NULL AFTER `spirit2`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `general2` text DEFAULT NULL AFTER `hygiene2`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `remark2` text DEFAULT NULL AFTER `general2`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `remark2`;
ALTER TABLE `mentor_remark` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `messages`
ALTER TABLE `messages` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `messages` ADD COLUMN IF NOT EXISTS `sender_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `messages` ADD COLUMN IF NOT EXISTS `receiver_id` bigint(20) UNSIGNED NOT NULL AFTER `sender_id`;
ALTER TABLE `messages` ADD COLUMN IF NOT EXISTS `message` text NOT NULL AFTER `receiver_id`;
ALTER TABLE `messages` ADD COLUMN IF NOT EXISTS `is_read` tinyint(1) DEFAULT 0 AFTER `message`;
ALTER TABLE `messages` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `is_read`;

-- Columns for `notifications`
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `title` varchar(200) NOT NULL AFTER `id`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `body` text NOT NULL AFTER `title`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `target_type` enum('all','class','role','specific_user') DEFAULT 'all' COMMENT 'Who this notification targets' AFTER `body`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `target_class_id` bigint(20) UNSIGNED DEFAULT NULL COMMENT 'If target_type=class' AFTER `target_type`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `target_role` enum('admin','teacher','parent','guardian','student') DEFAULT NULL COMMENT 'If target_type=role' AFTER `target_class_id`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `target_user_id` bigint(20) UNSIGNED DEFAULT NULL COMMENT 'If target_type=specific_user' AFTER `target_role`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `notification_type` enum('announcement','event','urgent','info','reminder') DEFAULT 'info' AFTER `target_user_id`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `data_type` varchar(50) DEFAULT NULL COMMENT 'announcement, assignment, fee, etc.' AFTER `notification_type`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `data_id` bigint(20) UNSIGNED DEFAULT NULL COMMENT 'ID of related announcement/assignment/etc' AFTER `data_type`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `priority` enum('low','normal','high') DEFAULT 'normal' AFTER `data_id`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `delivery_status` enum('pending','sent','failed','delivered') DEFAULT 'pending' AFTER `priority`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `scheduled_at` datetime DEFAULT NULL COMMENT 'If NULL, send immediately' AFTER `delivery_status`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `sent_at` datetime DEFAULT NULL AFTER `scheduled_at`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `failure_reason` text DEFAULT NULL AFTER `sent_at`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `created_by` bigint(20) UNSIGNED DEFAULT NULL AFTER `failure_reason`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `created_by`;
ALTER TABLE `notifications` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `notification_devices`
ALTER TABLE `notification_devices` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `notification_devices` ADD COLUMN IF NOT EXISTS `notification_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `notification_devices` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `notification_id`;
ALTER TABLE `notification_devices` ADD COLUMN IF NOT EXISTS `fcm_token` varchar(500) NOT NULL AFTER `student_id`;
ALTER TABLE `notification_devices` ADD COLUMN IF NOT EXISTS `firebase_message_id` varchar(255) DEFAULT NULL AFTER `fcm_token`;
ALTER TABLE `notification_devices` ADD COLUMN IF NOT EXISTS `status` enum('sent','failed','invalid_token') NOT NULL DEFAULT 'sent' AFTER `firebase_message_id`;
ALTER TABLE `notification_devices` ADD COLUMN IF NOT EXISTS `error_message` text DEFAULT NULL AFTER `status`;
ALTER TABLE `notification_devices` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `error_message`;

-- Columns for `notification_reads`
ALTER TABLE `notification_reads` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `notification_reads` ADD COLUMN IF NOT EXISTS `notification_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `notification_reads` ADD COLUMN IF NOT EXISTS `user_id` bigint(20) UNSIGNED NOT NULL AFTER `notification_id`;
ALTER TABLE `notification_reads` ADD COLUMN IF NOT EXISTS `read_at` datetime DEFAULT NULL AFTER `user_id`;
ALTER TABLE `notification_reads` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `read_at`;

-- Columns for `other_payments`
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `payment_account_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `id`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `payment_account_id`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `class_id` bigint(20) UNSIGNED NOT NULL AFTER `student_id`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `payment_item_id` bigint(20) UNSIGNED NOT NULL AFTER `class_id`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `payment_item_name` varchar(150) NOT NULL AFTER `payment_item_id`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `session_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `payment_item_name`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `term` varchar(20) DEFAULT NULL AFTER `session_id`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `amount_due` decimal(12,2) NOT NULL AFTER `term`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `amount_paid` decimal(12,2) NOT NULL AFTER `amount_due`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `payment_date` date NOT NULL AFTER `amount_paid`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `payment_method` varchar(50) DEFAULT NULL AFTER `payment_date`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `receipt_number` varchar(100) NOT NULL AFTER `payment_method`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `request_id` varchar(80) DEFAULT NULL AFTER `receipt_number`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `reference` varchar(150) DEFAULT NULL AFTER `request_id`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `notes` text DEFAULT NULL AFTER `reference`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `recorded_by` bigint(20) UNSIGNED NOT NULL AFTER `notes`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `status` enum('completed','reversed') NOT NULL DEFAULT 'completed' AFTER `recorded_by`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `reversed_by` bigint(20) UNSIGNED DEFAULT NULL AFTER `status`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `reversed_at` datetime DEFAULT NULL AFTER `reversed_by`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `reversal_reason` text DEFAULT NULL AFTER `reversed_at`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `created_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `reversal_reason`;
ALTER TABLE `other_payments` ADD COLUMN IF NOT EXISTS `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `other_payment_items`
ALTER TABLE `other_payment_items` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `other_payment_items` ADD COLUMN IF NOT EXISTS `name` varchar(150) NOT NULL AFTER `id`;
ALTER TABLE `other_payment_items` ADD COLUMN IF NOT EXISTS `description` text DEFAULT NULL AFTER `name`;
ALTER TABLE `other_payment_items` ADD COLUMN IF NOT EXISTS `default_amount` decimal(12,2) NOT NULL DEFAULT 0.00 AFTER `description`;
ALTER TABLE `other_payment_items` ADD COLUMN IF NOT EXISTS `allow_amount_override` tinyint(1) NOT NULL DEFAULT 0 AFTER `default_amount`;
ALTER TABLE `other_payment_items` ADD COLUMN IF NOT EXISTS `allow_partial_payment` tinyint(1) NOT NULL DEFAULT 0 AFTER `allow_amount_override`;
ALTER TABLE `other_payment_items` ADD COLUMN IF NOT EXISTS `allow_discounts` tinyint(1) NOT NULL DEFAULT 1 AFTER `allow_partial_payment`;
ALTER TABLE `other_payment_items` ADD COLUMN IF NOT EXISTS `session_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `allow_discounts`;
ALTER TABLE `other_payment_items` ADD COLUMN IF NOT EXISTS `term` varchar(20) DEFAULT NULL AFTER `session_id`;
ALTER TABLE `other_payment_items` ADD COLUMN IF NOT EXISTS `is_active` tinyint(1) NOT NULL DEFAULT 1 AFTER `term`;
ALTER TABLE `other_payment_items` ADD COLUMN IF NOT EXISTS `created_by` bigint(20) UNSIGNED NOT NULL AFTER `is_active`;
ALTER TABLE `other_payment_items` ADD COLUMN IF NOT EXISTS `created_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `created_by`;
ALTER TABLE `other_payment_items` ADD COLUMN IF NOT EXISTS `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `parent_student`
ALTER TABLE `parent_student` ADD COLUMN IF NOT EXISTS `parent_id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `parent_student` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `parent_id`;
ALTER TABLE `parent_student` ADD COLUMN IF NOT EXISTS `relationship` varchar(50) DEFAULT 'Parent' AFTER `student_id`;
ALTER TABLE `parent_student` ADD COLUMN IF NOT EXISTS `is_primary` tinyint(1) DEFAULT 1 AFTER `relationship`;
ALTER TABLE `parent_student` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `is_primary`;

-- Columns for `results`
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `id` int(11) NOT NULL FIRST;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `ids` bigint(20) UNSIGNED NOT NULL DEFAULT 0 AFTER `id`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `ids`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `class_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `student_id`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `session_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `class_id`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `subject_id` bigint(20) UNSIGNED NOT NULL AFTER `session_id`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `subject_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL AFTER `subject_id`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `wk1` decimal(5,2) NOT NULL DEFAULT 0.00 AFTER `subject_name`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `wk2` decimal(5,2) NOT NULL DEFAULT 0.00 AFTER `wk1`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `wk3` decimal(5,2) NOT NULL DEFAULT 0.00 AFTER `wk2`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `wk4` decimal(5,2) NOT NULL DEFAULT 0.00 AFTER `wk3`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `remark_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `wk4`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `teacher_comment` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL AFTER `remark_id`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `ca1` decimal(5,2) DEFAULT 0.00 AFTER `teacher_comment`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `wk6` decimal(5,2) NOT NULL DEFAULT 0.00 AFTER `ca1`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `wk7` decimal(5,2) NOT NULL DEFAULT 0.00 AFTER `wk6`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `wk8` decimal(5,2) NOT NULL DEFAULT 0.00 AFTER `wk7`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `wk9` decimal(5,2) NOT NULL DEFAULT 0.00 AFTER `wk8`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `ca2` decimal(5,2) DEFAULT 0.00 AFTER `wk9`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `st1` decimal(5,2) NOT NULL DEFAULT 0.00 AFTER `ca2`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `st2` decimal(5,2) NOT NULL DEFAULT 0.00 AFTER `st1`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `cat` decimal(5,2) NOT NULL DEFAULT 0.00 AFTER `st2`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `exam` decimal(5,2) DEFAULT 0.00 AFTER `cat`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `total` decimal(5,2) DEFAULT NULL AFTER `exam`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `grade` char(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL AFTER `total`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `position_in_class` int(11) DEFAULT NULL AFTER `grade`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `position_in_class`;
ALTER TABLE `results` ADD COLUMN IF NOT EXISTS `pl` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL AFTER `created_at`;

-- Columns for `school_api_sessions`
ALTER TABLE `school_api_sessions` ADD COLUMN IF NOT EXISTS `token_hash` char(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL FIRST;
ALTER TABLE `school_api_sessions` ADD COLUMN IF NOT EXISTS `user_id` bigint(20) UNSIGNED NOT NULL AFTER `token_hash`;
ALTER TABLE `school_api_sessions` ADD COLUMN IF NOT EXISTS `expires_at` datetime NOT NULL AFTER `user_id`;
ALTER TABLE `school_api_sessions` ADD COLUMN IF NOT EXISTS `created_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `expires_at`;

-- Columns for `school_info`
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `vice_principal` varchar(100) DEFAULT NULL COMMENT 'Name of Vice Principal (if applicable)' AFTER `id`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `school_name` varchar(200) NOT NULL AFTER `vice_principal`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `motto` varchar(255) DEFAULT NULL AFTER `school_name`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `address` varchar(255) DEFAULT NULL AFTER `motto`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `city` varchar(100) DEFAULT NULL AFTER `address`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `state` varchar(100) DEFAULT NULL AFTER `city`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `postal_code` varchar(20) DEFAULT NULL AFTER `state`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `country` varchar(100) DEFAULT NULL AFTER `postal_code`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `school_email` varchar(150) DEFAULT NULL AFTER `country`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `phone` varchar(200) DEFAULT NULL AFTER `school_email`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `complaints` varchar(300) DEFAULT NULL AFTER `phone`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `website` varchar(255) DEFAULT NULL AFTER `complaints`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `logo_url` varchar(255) DEFAULT NULL AFTER `website`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `favicon_url` varchar(255) DEFAULT NULL AFTER `logo_url`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `created_at` timestamp NULL DEFAULT current_timestamp() AFTER `favicon_url`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `principal_primary` varchar(100) DEFAULT NULL COMMENT 'Name of Primary School Principal' AFTER `updated_at`;
ALTER TABLE `school_info` ADD COLUMN IF NOT EXISTS `principal_secondary` varchar(100) DEFAULT NULL COMMENT 'Name of Secondary School Principal' AFTER `principal_primary`;

-- Columns for `sections`
ALTER TABLE `sections` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `sections` ADD COLUMN IF NOT EXISTS `name` varchar(255) NOT NULL COMMENT 'Section name e.g., Nursery Academy' AFTER `id`;
ALTER TABLE `sections` ADD COLUMN IF NOT EXISTS `stage` varchar(100) NOT NULL COMMENT 'Nursery, Primary, or Secondary' AFTER `name`;
ALTER TABLE `sections` ADD COLUMN IF NOT EXISTS `coordinator` varchar(255) DEFAULT NULL COMMENT 'Lead coordinator/tutor name' AFTER `stage`;
ALTER TABLE `sections` ADD COLUMN IF NOT EXISTS `campus` varchar(100) DEFAULT NULL COMMENT 'Campus location e.g., Main, Annex' AFTER `coordinator`;
ALTER TABLE `sections` ADD COLUMN IF NOT EXISTS `status` tinyint(1) DEFAULT 1 COMMENT '1 = Active, 0 = Inactive' AFTER `campus`;
ALTER TABLE `sections` ADD COLUMN IF NOT EXISTS `created_at` timestamp NULL DEFAULT current_timestamp() AFTER `status`;
ALTER TABLE `sections` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `students`
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `full_name` varchar(100) NOT NULL AFTER `id`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `username` varchar(255) DEFAULT NULL AFTER `full_name`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `gender` enum('M','F','Other') DEFAULT NULL AFTER `username`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `class_id` bigint(20) UNSIGNED NOT NULL AFTER `gender`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `session` varchar(40) NOT NULL AFTER `class_id`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `admission_number` varchar(30) NOT NULL AFTER `session`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `mentor_id` int(11) DEFAULT NULL AFTER `admission_number`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `mentor` varchar(255) DEFAULT NULL AFTER `mentor_id`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `parent_code` varchar(6) DEFAULT NULL COMMENT '5-6 digit numeric code for parent login' AFTER `mentor`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `parent_code`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `date_of_birth` date DEFAULT NULL AFTER `updated_at`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `photo_url` varchar(255) DEFAULT NULL AFTER `date_of_birth`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `blood_group` varchar(5) DEFAULT NULL AFTER `photo_url`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `allergies` text DEFAULT NULL AFTER `blood_group`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `emergency_contact_name` varchar(100) DEFAULT NULL AFTER `allergies`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `emergency_contact_phone` varchar(20) DEFAULT NULL AFTER `emergency_contact_name`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `fcm_token` varchar(300) DEFAULT NULL AFTER `emergency_contact_phone`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `fcm_token_updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `fcm_token`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `regno` int(11) NOT NULL AFTER `fcm_token_updated_at`;
ALTER TABLE `students` ADD COLUMN IF NOT EXISTS `section` varchar(20) DEFAULT NULL AFTER `regno`;

-- Columns for `student_other_payment_accounts`
ALTER TABLE `student_other_payment_accounts` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `student_other_payment_accounts` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `student_other_payment_accounts` ADD COLUMN IF NOT EXISTS `class_id` bigint(20) UNSIGNED NOT NULL AFTER `student_id`;
ALTER TABLE `student_other_payment_accounts` ADD COLUMN IF NOT EXISTS `payment_item_id` bigint(20) UNSIGNED NOT NULL AFTER `class_id`;
ALTER TABLE `student_other_payment_accounts` ADD COLUMN IF NOT EXISTS `payment_item_name` varchar(150) NOT NULL AFTER `payment_item_id`;
ALTER TABLE `student_other_payment_accounts` ADD COLUMN IF NOT EXISTS `session_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `payment_item_name`;
ALTER TABLE `student_other_payment_accounts` ADD COLUMN IF NOT EXISTS `term` varchar(20) DEFAULT NULL AFTER `session_id`;
ALTER TABLE `student_other_payment_accounts` ADD COLUMN IF NOT EXISTS `amount_due` decimal(12,2) NOT NULL AFTER `term`;
ALTER TABLE `student_other_payment_accounts` ADD COLUMN IF NOT EXISTS `status` enum('unpaid','partial','paid','cancelled') NOT NULL DEFAULT 'unpaid' AFTER `amount_due`;
ALTER TABLE `student_other_payment_accounts` ADD COLUMN IF NOT EXISTS `created_by` bigint(20) UNSIGNED NOT NULL AFTER `status`;
ALTER TABLE `student_other_payment_accounts` ADD COLUMN IF NOT EXISTS `created_at` datetime NOT NULL DEFAULT current_timestamp() AFTER `created_by`;
ALTER TABLE `student_other_payment_accounts` ADD COLUMN IF NOT EXISTS `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `student_remarks`
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `student_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `session_id` bigint(20) UNSIGNED NOT NULL AFTER `student_id`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `teacher_remark` text DEFAULT NULL AFTER `session_id`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `principal_remark` text DEFAULT NULL AFTER `teacher_remark`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `class_teacher_remark` text DEFAULT NULL AFTER `principal_remark`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `created_by` bigint(20) UNSIGNED DEFAULT NULL AFTER `class_teacher_remark`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `approved_by` bigint(20) UNSIGNED DEFAULT NULL AFTER `created_by`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `approved_by`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `a` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'AttendanceSS (0-5)' AFTER `updated_at`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `b` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Attentiveness in class (0-5)' AFTER `a`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `c` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Craft, drawing, painting (0-5)' AFTER `b`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `d` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Cooperation spirit (0-5)' AFTER `c`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `e` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Fluency/Elocution (0-5)' AFTER `d`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `f` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Handwriting (0-5)' AFTER `e`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `g` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Honesty (0-5)' AFTER `f`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `h` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Leadership ability (0-5)' AFTER `g`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `i` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Music skills (0-5)' AFTER `h`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `j` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Neatness (0-5)' AFTER `i`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `k` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Organizational ability (0-5)' AFTER `j`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `l` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Politeness (0-5)' AFTER `k`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `m` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Promptness in assignment (0-5)' AFTER `l`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `n` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Punctuality (0-5)' AFTER `m`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `o` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Relationship with staff (0-5)' AFTER `n`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `p` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Relationship with students (0-5)' AFTER `o`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `q` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Reliability (0-5)' AFTER `p`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `r` tinyint(3) UNSIGNED DEFAULT NULL COMMENT 'Responsibility (0-5)' AFTER `q`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `s` int(11) DEFAULT NULL COMMENT 'Self-control' AFTER `r`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `t` int(11) DEFAULT NULL COMMENT 'willingness to learn' AFTER `s`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `u` int(11) DEFAULT NULL AFTER `t`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `v` int(11) DEFAULT NULL AFTER `u`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `resumption_date` date DEFAULT NULL COMMENT 'Date students resume for this term' AFTER `v`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `school_closing_date` date DEFAULT NULL COMMENT 'Date school closes for this term' AFTER `resumption_date`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `mentor` varchar(300) DEFAULT NULL AFTER `school_closing_date`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `teacher_comment` varchar(500) DEFAULT NULL AFTER `mentor`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `first_term_comment` varchar(500) DEFAULT NULL AFTER `teacher_comment`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `second_term_comment` varchar(500) DEFAULT NULL AFTER `first_term_comment`;
ALTER TABLE `student_remarks` ADD COLUMN IF NOT EXISTS `abs` int(11) NOT NULL AFTER `second_term_comment`;

-- Columns for `subjects`
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `class` varchar(255) DEFAULT NULL AFTER `id`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `class_id` bigint(20) UNSIGNED NOT NULL AFTER `class`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `subject_name` varchar(100) NOT NULL AFTER `class_id`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `subject_code` varchar(20) DEFAULT NULL AFTER `subject_name`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `teacher` varchar(50) DEFAULT NULL AFTER `subject_code`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `teacher_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `teacher`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `credit_hours` decimal(3,1) DEFAULT 1.0 AFTER `teacher_id`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `is_compulsory` tinyint(1) DEFAULT 1 AFTER `credit_hours`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `description` text DEFAULT NULL AFTER `is_compulsory`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `description`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `academic_session_id` bigint(20) UNSIGNED DEFAULT NULL COMMENT 'Links to academic_sessions.id for the term/year this subject applies to' AFTER `updated_at`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `status` tinyint(1) UNSIGNED NOT NULL DEFAULT 1 COMMENT '1 = active (offered), 2 = inactive (not offered / deactivated)' AFTER `academic_session_id`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `assoc_teacher` varchar(500) DEFAULT NULL AFTER `status`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `grade_type` varchar(10) DEFAULT 'letters' AFTER `assoc_teacher`;
ALTER TABLE `subjects` ADD COLUMN IF NOT EXISTS `play_class_category` varchar(50) DEFAULT NULL AFTER `grade_type`;

-- Columns for `timetable`
ALTER TABLE `timetable` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `timetable` ADD COLUMN IF NOT EXISTS `class_id` bigint(20) UNSIGNED NOT NULL AFTER `id`;
ALTER TABLE `timetable` ADD COLUMN IF NOT EXISTS `day_of_week` varchar(20) NOT NULL AFTER `class_id`;
ALTER TABLE `timetable` ADD COLUMN IF NOT EXISTS `period_number` tinyint(3) UNSIGNED DEFAULT NULL AFTER `day_of_week`;
ALTER TABLE `timetable` ADD COLUMN IF NOT EXISTS `subject_id` bigint(20) UNSIGNED NOT NULL AFTER `period_number`;
ALTER TABLE `timetable` ADD COLUMN IF NOT EXISTS `start_time` time NOT NULL AFTER `subject_id`;
ALTER TABLE `timetable` ADD COLUMN IF NOT EXISTS `end_time` time NOT NULL AFTER `start_time`;
ALTER TABLE `timetable` ADD COLUMN IF NOT EXISTS `venue` varchar(100) DEFAULT NULL AFTER `end_time`;
ALTER TABLE `timetable` ADD COLUMN IF NOT EXISTS `teacher_id` bigint(20) UNSIGNED DEFAULT NULL AFTER `venue`;
ALTER TABLE `timetable` ADD COLUMN IF NOT EXISTS `room_number` varchar(20) DEFAULT NULL AFTER `teacher_id`;
ALTER TABLE `timetable` ADD COLUMN IF NOT EXISTS `session_year` varchar(20) DEFAULT '2025/2026' AFTER `room_number`;
ALTER TABLE `timetable` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `session_year`;
ALTER TABLE `timetable` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;

-- Columns for `users`
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `id` bigint(20) UNSIGNED NOT NULL FIRST;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `email` varchar(150) DEFAULT NULL AFTER `id`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `phone` varchar(20) DEFAULT NULL AFTER `email`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `second_phone` varchar(20) DEFAULT NULL AFTER `phone`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `address` text DEFAULT NULL AFTER `second_phone`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `date_of_birth` date DEFAULT NULL AFTER `address`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `password_hash` varchar(2550) NOT NULL AFTER `date_of_birth`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `full_name` varchar(100) NOT NULL AFTER `password_hash`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `gender` enum('Male','Female') DEFAULT NULL AFTER `full_name`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `role` enum('parent','guardian','teacher','admin','secretary','bursar','principal','vice_principal','librarian','hos','inventory') NOT NULL DEFAULT 'parent' AFTER `gender`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `mentor` varchar(255) DEFAULT NULL AFTER `role`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `assigned_class` varchar(100) DEFAULT NULL AFTER `mentor`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `teacher_role` varchar(200) DEFAULT NULL AFTER `assigned_class`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `photo` varchar(255) DEFAULT NULL AFTER `teacher_role`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `signature` varchar(255) DEFAULT NULL AFTER `photo`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `otp_code` varchar(6) DEFAULT NULL AFTER `signature`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `otp_expires_at` datetime DEFAULT NULL AFTER `otp_code`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `last_login` datetime DEFAULT NULL AFTER `otp_expires_at`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `is_active` tinyint(1) DEFAULT 1 AFTER `last_login`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `created_at` timestamp NOT NULL DEFAULT current_timestamp() AFTER `is_active`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() AFTER `created_at`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `fcm_token_updated_at` varchar(300) NOT NULL AFTER `updated_at`;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `fcm_token` varchar(300) NOT NULL AFTER `fcm_token_updated_at`;

-- ============================================================================
-- 3) ENSURE INDEXES / PRIMARY KEYS EXIST
-- ============================================================================

ALTER TABLE `academic_sessions`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uk_session_term` (`session_year`,`term`),
  ADD KEY IF NOT EXISTS `idx_is_current` (`is_current`);

ALTER TABLE `activity_participation`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `idx_activity` (`activity_id`),
  ADD KEY IF NOT EXISTS `idx_participation_student` (`student_id`);

ALTER TABLE `announcements`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `target_class_id` (`target_class_id`),
  ADD KEY IF NOT EXISTS `created_by` (`created_by`);

ALTER TABLE `app_settings`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `setting_key` (`setting_key`);

ALTER TABLE `assignments`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `subject_id` (`subject_id`),
  ADD KEY IF NOT EXISTS `teacher_id` (`teacher_id`),
  ADD KEY IF NOT EXISTS `idx_status` (`assigned_date`),
  ADD KEY IF NOT EXISTS `idx_assignments_class` (`class_id`),
  ADD KEY IF NOT EXISTS `idx_assignments_due` (`due_date`);

ALTER TABLE `assignment_submissions`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `assignment_id` (`assignment_id`),
  ADD KEY IF NOT EXISTS `graded_by` (`graded_by`),
  ADD KEY IF NOT EXISTS `idx_submission_date` (`submission_date`),
  ADD KEY IF NOT EXISTS `idx_submissions_student` (`student_id`),
  ADD KEY IF NOT EXISTS `idx_submissions_date` (`submission_date`);

ALTER TABLE `attendance`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uk_student_date` (`student_id`,`attendance_date`),
  ADD KEY IF NOT EXISTS `idx_location_type` (`location_type`),
  ADD KEY IF NOT EXISTS `idx_created_at` (`created_at`),
  ADD KEY IF NOT EXISTS `idx_attendance_session_term` (`session_year`,`term`);

ALTER TABLE `bursary_activity`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `action` (`action`,`record_id`),
  ADD KEY IF NOT EXISTS `fk_bursary_activity_performed_by` (`performed_by_id`);

ALTER TABLE `bursary_discounts`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `is_active` (`is_active`,`student_id`,`applies_to`),
  ADD KEY IF NOT EXISTS `session_id` (`session_id`,`class_id`),
  ADD KEY IF NOT EXISTS `fk_bursary_discounts_student` (`student_id`),
  ADD KEY IF NOT EXISTS `fk_bursary_discounts_class` (`class_id`),
  ADD KEY IF NOT EXISTS `fk_bursary_discounts_created_by` (`created_by`),
  ADD KEY IF NOT EXISTS `fk_bursary_discount_item` (`other_payment_item_id`);

ALTER TABLE `bursary_family_payments`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `receipt_number` (`receipt_number`),
  ADD KEY IF NOT EXISTS `idx_bursary_family_payments_session` (`session_id`,`id`),
  ADD KEY IF NOT EXISTS `fk_bursary_family_payment_received_by` (`received_by_id`);

ALTER TABLE `bursary_family_payment_lines`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_family_payment_payment` (`payment_id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_family_payment_student` (`family_payment_id`,`student_id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_family_payment_fee` (`family_payment_id`,`fee_id`),
  ADD KEY IF NOT EXISTS `fk_family_payment_line_student` (`student_id`),
  ADD KEY IF NOT EXISTS `fk_family_payment_line_fee` (`fee_id`);

ALTER TABLE `bursary_requests`
  ADD PRIMARY KEY IF NOT EXISTS (`actor_id`,`request_id`);

ALTER TABLE `cbt_answers`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `unique_student_question` (`student_id`,`question_id`),
  ADD KEY IF NOT EXISTS `test_id` (`test_id`),
  ADD KEY IF NOT EXISTS `question_id` (`question_id`),
  ADD KEY IF NOT EXISTS `student_id` (`student_id`),
  ADD KEY IF NOT EXISTS `idx_cbt_answers_test_student` (`test_id`,`student_id`),
  ADD KEY IF NOT EXISTS `idx_question_order` (`question_order`);

ALTER TABLE `cbt_contexts`
  ADD PRIMARY KEY IF NOT EXISTS (`id`);

ALTER TABLE `cbt_questions`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `test_id` (`test_id`),
  ADD KEY IF NOT EXISTS `order_index` (`order_index`),
  ADD KEY IF NOT EXISTS `idx_cbt_questions_test_order` (`test_id`,`order_index`);

ALTER TABLE `cbt_results`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `unique_test_student` (`test_id`,`student_id`),
  ADD KEY IF NOT EXISTS `student_id` (`student_id`),
  ADD KEY IF NOT EXISTS `status` (`status`),
  ADD KEY IF NOT EXISTS `idx_cbt_results_test_status` (`test_id`,`status`);

ALTER TABLE `cbt_tests`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `teacher_id` (`teacher_id`),
  ADD KEY IF NOT EXISTS `subject_id` (`subject_id`),
  ADD KEY IF NOT EXISTS `class_id` (`class_id`),
  ADD KEY IF NOT EXISTS `session_id` (`session_id`),
  ADD KEY IF NOT EXISTS `status` (`status`),
  ADD KEY IF NOT EXISTS `student_id` (`student_id`),
  ADD KEY IF NOT EXISTS `idx_cbt_tests_teacher_status` (`teacher_id`,`status`),
  ADD KEY IF NOT EXISTS `idx_cbt_tests_class_subject` (`class_id`,`subject_id`),
  ADD KEY IF NOT EXISTS `idx_cbt_tests_start_date` (`start_date`),
  ADD KEY IF NOT EXISTS `idx_cbt_tests_end_date` (`end_date`),
  ADD KEY IF NOT EXISTS `idx_teacher_subject_session` (`teacher_id`,`subject_id`,`session_id`),
  ADD KEY IF NOT EXISTS `idx_sync_status` (`sync_status`),
  ADD KEY IF NOT EXISTS `idx_sync_source` (`sync_source`),
  ADD KEY IF NOT EXISTS `idx_local_modified_at` (`local_modified_at`);

ALTER TABLE `cbt_test_assignments`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `unique_test_student_assignment` (`test_id`,`student_id`),
  ADD KEY IF NOT EXISTS `student_id` (`student_id`),
  ADD KEY IF NOT EXISTS `status` (`status`),
  ADD KEY IF NOT EXISTS `idx_cbt_assignments_test_status` (`test_id`,`status`),
  ADD KEY IF NOT EXISTS `idx_sync_source` (`sync_source`);

ALTER TABLE `cbt_test_classes`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `test_class_unique` (`test_id`,`class_id`);

ALTER TABLE `classes`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uk_class_section` (`class_name`,`section`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_classes_name_section` (`class_name`,`section`),
  ADD KEY IF NOT EXISTS `fk_classes_teacher` (`class_teacher_id`),
  ADD KEY IF NOT EXISTS `section_id` (`section_id`);

ALTER TABLE `discipline_records`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `reported_by` (`reported_by`),
  ADD KEY IF NOT EXISTS `idx_incident_date` (`incident_date`),
  ADD KEY IF NOT EXISTS `idx_discipline_student` (`student_id`);

ALTER TABLE `events`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `target_class_id` (`target_class_id`),
  ADD KEY IF NOT EXISTS `created_by` (`created_by`),
  ADD KEY IF NOT EXISTS `idx_event_type` (`event_type`),
  ADD KEY IF NOT EXISTS `idx_events_date` (`event_date`),
  ADD KEY IF NOT EXISTS `idx_events_type` (`event_type`),
  ADD KEY IF NOT EXISTS `idx_events_session_term` (`session_year`,`term`);

ALTER TABLE `extracurricular_activities`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `coordinator_id` (`coordinator_id`),
  ADD KEY IF NOT EXISTS `idx_is_active` (`is_active`),
  ADD KEY IF NOT EXISTS `idx_activities_type` (`activity_type`);

ALTER TABLE `fees`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `student_id` (`student_id`);

ALTER TABLE `feess`
  ADD UNIQUE KEY IF NOT EXISTS `regno` (`regno`);

ALTER TABLE `fee_charge_details`
  ADD PRIMARY KEY IF NOT EXISTS (`fee_id`),
  ADD KEY IF NOT EXISTS `charge_kind` (`charge_kind`,`category`),
  ADD KEY IF NOT EXISTS `fk_fee_charge_details_class` (`class_id`),
  ADD KEY IF NOT EXISTS `fk_fee_charge_details_created_by` (`created_by`);

ALTER TABLE `fee_discounts`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `fee_id` (`fee_id`),
  ADD KEY IF NOT EXISTS `fk_fee_discounts_discount` (`discount_id`),
  ADD KEY IF NOT EXISTS `fk_fee_discounts_student` (`student_id`),
  ADD KEY IF NOT EXISTS `fk_fee_discounts_applied_by` (`applied_by`);

ALTER TABLE `fee_items`
  ADD PRIMARY KEY IF NOT EXISTS (`id`);

ALTER TABLE `fee_payments`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_fee_payments_settlement_reference` (`settlement_reference`,`session_id`),
  ADD KEY IF NOT EXISTS `idx_fee_payments_student_session` (`student_id`,`session_id`),
  ADD KEY IF NOT EXISTS `idx_fee_payments_student` (`student_id`);

ALTER TABLE `fee_payment_allocations`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_fee_payment_allocations` (`payment_id`,`fee_id`),
  ADD KEY IF NOT EXISTS `idx_fee_payment_allocations_fee` (`fee_id`);

ALTER TABLE `fee_payment_details`
  ADD PRIMARY KEY IF NOT EXISTS (`payment_id`),
  ADD KEY IF NOT EXISTS `fk_fee_payment_details_fee` (`fee_id`),
  ADD KEY IF NOT EXISTS `fk_fee_payment_details_received_by` (`received_by_id`);

ALTER TABLE `grading_keys`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `unique_range` (`min_score`,`max_score`);

ALTER TABLE `grading_playclass`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `grade` (`grade`);

ALTER TABLE `inventory_categories`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_inventory_category_name` (`name`);

ALTER TABLE `inventory_issuances`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_inventory_issuances_issued_by_request` (`issued_by`,`request_id`),
  ADD KEY IF NOT EXISTS `date` (`date`,`id`),
  ADD KEY IF NOT EXISTS `student_id` (`student_id`);

ALTER TABLE `inventory_issuance_items`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_inventory_issuance_items_issuance_item` (`issuance_id`,`inventory_item_id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_inventory_issuance_items_movement` (`movement_id`),
  ADD KEY IF NOT EXISTS `fk_inventory_issuance_items_item` (`inventory_item_id`);

ALTER TABLE `inventory_items`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_inventory_item_sku` (`sku`),
  ADD KEY IF NOT EXISTS `idx_inventory_item_category` (`category_id`),
  ADD KEY IF NOT EXISTS `idx_inventory_item_active` (`is_active`);

ALTER TABLE `inventory_movements`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_inventory_movement_request` (`performed_by_id`,`request_id`),
  ADD KEY IF NOT EXISTS `idx_inventory_movement_date` (`date`,`id`),
  ADD KEY IF NOT EXISTS `idx_inventory_movement_item` (`item_id`,`id`),
  ADD KEY IF NOT EXISTS `idx_inventory_movement_type` (`type`,`date`),
  ADD KEY IF NOT EXISTS `idx_inventory_movement_supplier` (`supplier_id`,`date`);

ALTER TABLE `inventory_suppliers`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_inventory_supplier_name` (`name`);

ALTER TABLE `library_books`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `book_code` (`book_code`),
  ADD UNIQUE KEY IF NOT EXISTS `isbn` (`isbn`),
  ADD KEY IF NOT EXISTS `idx_category` (`category`),
  ADD KEY IF NOT EXISTS `idx_library_books_title` (`book_title`);

ALTER TABLE `library_borrowing`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `book_id` (`book_id`),
  ADD KEY IF NOT EXISTS `librarian_id` (`librarian_id`),
  ADD KEY IF NOT EXISTS `idx_due_date` (`due_date`),
  ADD KEY IF NOT EXISTS `idx_borrowing_student` (`student_id`),
  ADD KEY IF NOT EXISTS `idx_borrowing_status` (`status`);

ALTER TABLE `lms_activity_log`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `idx_user_id` (`user_id`),
  ADD KEY IF NOT EXISTS `idx_action` (`action`),
  ADD KEY IF NOT EXISTS `idx_created_at` (`created_at`);

ALTER TABLE `lms_announcements`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `topic_id` (`topic_id`),
  ADD KEY IF NOT EXISTS `idx_teacher_id` (`teacher_id`),
  ADD KEY IF NOT EXISTS `idx_is_pinned` (`is_pinned`),
  ADD KEY IF NOT EXISTS `idx_created_at` (`created_at`),
  ADD KEY IF NOT EXISTS `idx_visibility` (`visibility`);

ALTER TABLE `lms_feedback`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `subtopic_id` (`subtopic_id`),
  ADD KEY IF NOT EXISTS `idx_topic_id` (`topic_id`),
  ADD KEY IF NOT EXISTS `idx_comment_type` (`comment_type`),
  ADD KEY IF NOT EXISTS `idx_is_resolved` (`is_resolved`),
  ADD KEY IF NOT EXISTS `idx_created_at` (`created_at`);

ALTER TABLE `lms_resources`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `idx_topic_id` (`topic_id`),
  ADD KEY IF NOT EXISTS `idx_subtopic_id` (`subtopic_id`),
  ADD KEY IF NOT EXISTS `idx_teacher_id` (`teacher_id`),
  ADD KEY IF NOT EXISTS `idx_file_type` (`file_type`),
  ADD KEY IF NOT EXISTS `idx_created_at` (`created_at`);

ALTER TABLE `lms_settings`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `setting_key` (`setting_key`),
  ADD KEY IF NOT EXISTS `idx_key` (`setting_key`);

ALTER TABLE `lms_student_progress`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `unique_student_topic` (`student_id`,`topic_id`),
  ADD KEY IF NOT EXISTS `topic_id` (`topic_id`),
  ADD KEY IF NOT EXISTS `subtopic_id` (`subtopic_id`),
  ADD KEY IF NOT EXISTS `idx_student_id` (`student_id`),
  ADD KEY IF NOT EXISTS `idx_status` (`status`),
  ADD KEY IF NOT EXISTS `idx_completed_at` (`completed_at`);

ALTER TABLE `lms_subtopics`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `unique_subtopic_order` (`topic_id`,`subtopic_number`),
  ADD KEY IF NOT EXISTS `idx_topic_id` (`topic_id`),
  ADD KEY IF NOT EXISTS `idx_teacher_id` (`teacher_id`),
  ADD KEY IF NOT EXISTS `idx_created_at` (`created_at`);

ALTER TABLE `lms_topics`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `idx_teacher_id` (`teacher_id`),
  ADD KEY IF NOT EXISTS `idx_status` (`status`),
  ADD KEY IF NOT EXISTS `idx_created_at` (`created_at`),
  ADD KEY IF NOT EXISTS `idx_topic_number` (`teacher_id`,`topic_number`);

ALTER TABLE `lms_topic_classes`
  ADD PRIMARY KEY IF NOT EXISTS (`topic_id`,`class_id`),
  ADD KEY IF NOT EXISTS `idx_topic_class` (`topic_id`,`class_id`);

ALTER TABLE `mentor_remark`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `unique_mentor_remark` (`class_id`,`session_id`,`student_id`,`mentor_id`),
  ADD KEY IF NOT EXISTS `idx_mentor_remark_student` (`student_id`),
  ADD KEY IF NOT EXISTS `idx_mentor_remark_mentor` (`mentor_id`),
  ADD KEY IF NOT EXISTS `idx_mentor_remark_class_session` (`class_id`,`session_id`);

ALTER TABLE `messages`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `sender_id` (`sender_id`),
  ADD KEY IF NOT EXISTS `receiver_id` (`receiver_id`);

ALTER TABLE `notifications`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `idx_target_type` (`target_type`),
  ADD KEY IF NOT EXISTS `idx_target_class_id` (`target_class_id`),
  ADD KEY IF NOT EXISTS `idx_delivery_status` (`delivery_status`),
  ADD KEY IF NOT EXISTS `idx_created_at` (`created_at`),
  ADD KEY IF NOT EXISTS `idx_scheduled_at` (`scheduled_at`),
  ADD KEY IF NOT EXISTS `idx_data` (`data_type`,`data_id`);

ALTER TABLE `notification_devices`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `idx_notification` (`notification_id`),
  ADD KEY IF NOT EXISTS `idx_student` (`student_id`),
  ADD KEY IF NOT EXISTS `idx_status` (`status`);

ALTER TABLE `notification_reads`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `unique_notification_user` (`notification_id`,`user_id`),
  ADD KEY IF NOT EXISTS `idx_read_at` (`read_at`),
  ADD KEY IF NOT EXISTS `user_id` (`user_id`);

ALTER TABLE `other_payments`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_other_payment_receipt` (`receipt_number`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_other_payment_request` (`recorded_by`,`request_id`),
  ADD KEY IF NOT EXISTS `idx_other_payments_student` (`student_id`),
  ADD KEY IF NOT EXISTS `idx_other_payments_class` (`class_id`),
  ADD KEY IF NOT EXISTS `idx_other_payments_item` (`payment_item_id`),
  ADD KEY IF NOT EXISTS `idx_other_payments_date` (`payment_date`),
  ADD KEY IF NOT EXISTS `idx_other_payments_account` (`payment_account_id`);

ALTER TABLE `other_payment_items`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `idx_other_payment_items_active` (`is_active`),
  ADD KEY IF NOT EXISTS `idx_other_payment_items_session` (`session_id`),
  ADD KEY IF NOT EXISTS `idx_other_payment_items_name` (`name`);

ALTER TABLE `parent_student`
  ADD PRIMARY KEY IF NOT EXISTS (`parent_id`,`student_id`),
  ADD KEY IF NOT EXISTS `student_id` (`student_id`);

ALTER TABLE `results`
  ADD PRIMARY KEY IF NOT EXISTS (`id`);

ALTER TABLE `school_api_sessions`
  ADD PRIMARY KEY IF NOT EXISTS (`token_hash`),
  ADD KEY IF NOT EXISTS `idx_api_sessions_user` (`user_id`),
  ADD KEY IF NOT EXISTS `idx_api_sessions_expiry` (`expires_at`);

ALTER TABLE `school_info`
  ADD PRIMARY KEY IF NOT EXISTS (`id`);

ALTER TABLE `sections`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `idx_stage` (`stage`),
  ADD KEY IF NOT EXISTS `idx_campus` (`campus`);

ALTER TABLE `students`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `parent_code` (`parent_code`),
  ADD KEY IF NOT EXISTS `idx_parent_code` (`parent_code`),
  ADD KEY IF NOT EXISTS `admission_number` (`admission_number`) USING BTREE;

ALTER TABLE `student_other_payment_accounts`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `idx_sopa_student` (`student_id`),
  ADD KEY IF NOT EXISTS `idx_sopa_item` (`payment_item_id`),
  ADD KEY IF NOT EXISTS `idx_sopa_class` (`class_id`);

ALTER TABLE `student_remarks`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uk_student_session` (`student_id`,`session_id`),
  ADD KEY IF NOT EXISTS `session_id` (`session_id`),
  ADD KEY IF NOT EXISTS `created_by` (`created_by`),
  ADD KEY IF NOT EXISTS `approved_by` (`approved_by`);

ALTER TABLE `subjects`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `teacher_id` (`teacher_id`),
  ADD KEY IF NOT EXISTS `idx_class` (`class_id`),
  ADD KEY IF NOT EXISTS `idx_subject_name` (`subject_name`),
  ADD KEY IF NOT EXISTS `idx_subjects_class` (`class_id`),
  ADD KEY IF NOT EXISTS `idx_session_status` (`academic_session_id`,`status`),
  ADD KEY IF NOT EXISTS `idx_subjects_class_session_name` (`class_id`,`academic_session_id`,`subject_name`);

ALTER TABLE `timetable`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `subject_id` (`subject_id`),
  ADD KEY IF NOT EXISTS `teacher_id` (`teacher_id`),
  ADD KEY IF NOT EXISTS `idx_class` (`class_id`),
  ADD KEY IF NOT EXISTS `idx_timetable_class` (`class_id`),
  ADD KEY IF NOT EXISTS `idx_timetable_day` (`day_of_week`);

ALTER TABLE `users`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `idx_role_fcm` (`role`),
  ADD KEY IF NOT EXISTS `idx_assigned_class_fcm` (`assigned_class`);

-- ============================================================================
-- 4) RESTORE AUTO_INCREMENT ATTRIBUTES
-- ============================================================================

ALTER TABLE `academic_sessions`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `activity_participation`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `announcements`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `app_settings`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `assignments`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `assignment_submissions`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `attendance`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `bursary_activity`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `bursary_discounts`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `bursary_family_payments`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `bursary_family_payment_lines`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `cbt_answers`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `cbt_questions`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `cbt_results`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `cbt_tests`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `cbt_test_assignments`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `cbt_test_classes`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `classes`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `discipline_records`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `events`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `extracurricular_activities`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `fees`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `fee_discounts`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `fee_items`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `fee_payments`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `fee_payment_allocations`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `grading_keys`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `grading_playclass`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `inventory_categories`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `inventory_issuances`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `inventory_issuance_items`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `inventory_items`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `inventory_movements`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `inventory_suppliers`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `library_books`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `library_borrowing`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `lms_activity_log`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `lms_announcements`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `lms_feedback`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `lms_resources`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `lms_settings`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `lms_student_progress`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `lms_subtopics`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `lms_topics`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `mentor_remark`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `messages`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `notifications`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `notification_devices`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `notification_reads`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `other_payments`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `other_payment_items`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `results`
  MODIFY COLUMN IF EXISTS `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `school_info`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `sections`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `students`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `student_other_payment_accounts`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `student_remarks`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `subjects`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `timetable`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE `users`
  MODIFY COLUMN IF EXISTS `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

-- ============================================================================
-- 5) ENSURE FOREIGN KEYS EXIST
-- ============================================================================

ALTER TABLE `activity_participation`
  ADD CONSTRAINT `activity_participation_ibfk_1` FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `activity_participation_ibfk_2` FOREIGN KEY IF NOT EXISTS (`activity_id`) REFERENCES `extracurricular_activities` (`id`) ON DELETE CASCADE;

ALTER TABLE `announcements`
  ADD CONSTRAINT `announcements_ibfk_1` FOREIGN KEY IF NOT EXISTS (`target_class_id`) REFERENCES `classes` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `announcements_ibfk_2` FOREIGN KEY IF NOT EXISTS (`created_by`) REFERENCES `users` (`id`) ON DELETE CASCADE;

ALTER TABLE `assignments`
  ADD CONSTRAINT `assignments_ibfk_1` FOREIGN KEY IF NOT EXISTS (`class_id`) REFERENCES `classes` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `assignments_ibfk_2` FOREIGN KEY IF NOT EXISTS (`subject_id`) REFERENCES `subjects` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `assignments_ibfk_3` FOREIGN KEY IF NOT EXISTS (`teacher_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;

ALTER TABLE `assignment_submissions`
  ADD CONSTRAINT `assignment_submissions_ibfk_1` FOREIGN KEY IF NOT EXISTS (`assignment_id`) REFERENCES `assignments` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `assignment_submissions_ibfk_2` FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `assignment_submissions_ibfk_3` FOREIGN KEY IF NOT EXISTS (`graded_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

ALTER TABLE `attendance`
  ADD CONSTRAINT `attendance_ibfk_1` FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`) ON DELETE CASCADE;

ALTER TABLE `bursary_activity`
  ADD CONSTRAINT `fk_bursary_activity_performed_by` FOREIGN KEY IF NOT EXISTS (`performed_by_id`) REFERENCES `users` (`id`);

ALTER TABLE `bursary_discounts`
  ADD CONSTRAINT `fk_bursary_discount_item` FOREIGN KEY IF NOT EXISTS (`other_payment_item_id`) REFERENCES `other_payment_items` (`id`),
  ADD CONSTRAINT `fk_bursary_discounts_class` FOREIGN KEY IF NOT EXISTS (`class_id`) REFERENCES `classes` (`id`),
  ADD CONSTRAINT `fk_bursary_discounts_created_by` FOREIGN KEY IF NOT EXISTS (`created_by`) REFERENCES `users` (`id`),
  ADD CONSTRAINT `fk_bursary_discounts_session` FOREIGN KEY IF NOT EXISTS (`session_id`) REFERENCES `academic_sessions` (`id`),
  ADD CONSTRAINT `fk_bursary_discounts_student` FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`);

ALTER TABLE `bursary_family_payments`
  ADD CONSTRAINT `fk_bursary_family_payment_received_by` FOREIGN KEY IF NOT EXISTS (`received_by_id`) REFERENCES `users` (`id`);

ALTER TABLE `bursary_family_payment_lines`
  ADD CONSTRAINT `fk_family_payment_line_family` FOREIGN KEY IF NOT EXISTS (`family_payment_id`) REFERENCES `bursary_family_payments` (`id`),
  ADD CONSTRAINT `fk_family_payment_line_fee` FOREIGN KEY IF NOT EXISTS (`fee_id`) REFERENCES `fees` (`id`),
  ADD CONSTRAINT `fk_family_payment_line_payment` FOREIGN KEY IF NOT EXISTS (`payment_id`) REFERENCES `fee_payments` (`id`),
  ADD CONSTRAINT `fk_family_payment_line_student` FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`);

ALTER TABLE `bursary_requests`
  ADD CONSTRAINT `fk_bursary_requests_actor` FOREIGN KEY IF NOT EXISTS (`actor_id`) REFERENCES `users` (`id`);

ALTER TABLE `cbt_answers`
  ADD CONSTRAINT `fk_cbt_answers_question` FOREIGN KEY IF NOT EXISTS (`question_id`) REFERENCES `cbt_questions` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `fk_cbt_answers_test` FOREIGN KEY IF NOT EXISTS (`test_id`) REFERENCES `cbt_tests` (`id`) ON DELETE CASCADE;

ALTER TABLE `cbt_questions`
  ADD CONSTRAINT `fk_cbt_questions_test` FOREIGN KEY IF NOT EXISTS (`test_id`) REFERENCES `cbt_tests` (`id`) ON DELETE CASCADE;

ALTER TABLE `cbt_results`
  ADD CONSTRAINT `fk_cbt_results_test` FOREIGN KEY IF NOT EXISTS (`test_id`) REFERENCES `cbt_tests` (`id`) ON DELETE CASCADE;

ALTER TABLE `cbt_test_assignments`
  ADD CONSTRAINT `fk_cbt_assignments_test` FOREIGN KEY IF NOT EXISTS (`test_id`) REFERENCES `cbt_tests` (`id`) ON DELETE CASCADE;

ALTER TABLE `cbt_test_classes`
  ADD CONSTRAINT `cbt_test_classes_ibfk_1` FOREIGN KEY IF NOT EXISTS (`test_id`) REFERENCES `cbt_tests` (`id`) ON DELETE CASCADE;

ALTER TABLE `discipline_records`
  ADD CONSTRAINT `discipline_records_ibfk_1` FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `discipline_records_ibfk_2` FOREIGN KEY IF NOT EXISTS (`reported_by`) REFERENCES `users` (`id`);

ALTER TABLE `fee_charge_details`
  ADD CONSTRAINT `fk_fee_charge_details_class` FOREIGN KEY IF NOT EXISTS (`class_id`) REFERENCES `classes` (`id`),
  ADD CONSTRAINT `fk_fee_charge_details_created_by` FOREIGN KEY IF NOT EXISTS (`created_by`) REFERENCES `users` (`id`),
  ADD CONSTRAINT `fk_fee_charge_details_fee` FOREIGN KEY IF NOT EXISTS (`fee_id`) REFERENCES `fees` (`id`);

ALTER TABLE `fee_discounts`
  ADD CONSTRAINT `fk_fee_discounts_applied_by` FOREIGN KEY IF NOT EXISTS (`applied_by`) REFERENCES `users` (`id`),
  ADD CONSTRAINT `fk_fee_discounts_discount` FOREIGN KEY IF NOT EXISTS (`discount_id`) REFERENCES `bursary_discounts` (`id`),
  ADD CONSTRAINT `fk_fee_discounts_fee` FOREIGN KEY IF NOT EXISTS (`fee_id`) REFERENCES `fees` (`id`),
  ADD CONSTRAINT `fk_fee_discounts_student` FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`);

ALTER TABLE `fee_payment_allocations`
  ADD CONSTRAINT `fk_fee_payment_allocations_fee` FOREIGN KEY IF NOT EXISTS (`fee_id`) REFERENCES `fees` (`id`),
  ADD CONSTRAINT `fk_fee_payment_allocations_payment` FOREIGN KEY IF NOT EXISTS (`payment_id`) REFERENCES `fee_payments` (`id`) ON DELETE CASCADE;

ALTER TABLE `fee_payment_details`
  ADD CONSTRAINT `fk_fee_payment_details_fee` FOREIGN KEY IF NOT EXISTS (`fee_id`) REFERENCES `fees` (`id`),
  ADD CONSTRAINT `fk_fee_payment_details_payment` FOREIGN KEY IF NOT EXISTS (`payment_id`) REFERENCES `fee_payments` (`id`),
  ADD CONSTRAINT `fk_fee_payment_details_received_by` FOREIGN KEY IF NOT EXISTS (`received_by_id`) REFERENCES `users` (`id`);

ALTER TABLE `inventory_issuances`
  ADD CONSTRAINT `fk_inventory_issuances_issued_by` FOREIGN KEY IF NOT EXISTS (`issued_by`) REFERENCES `users` (`id`),
  ADD CONSTRAINT `fk_inventory_issuances_student` FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`);

ALTER TABLE `inventory_issuance_items`
  ADD CONSTRAINT `fk_inventory_issuance_items_issuance` FOREIGN KEY IF NOT EXISTS (`issuance_id`) REFERENCES `inventory_issuances` (`id`),
  ADD CONSTRAINT `fk_inventory_issuance_items_item` FOREIGN KEY IF NOT EXISTS (`inventory_item_id`) REFERENCES `inventory_items` (`id`),
  ADD CONSTRAINT `fk_inventory_issuance_items_movement` FOREIGN KEY IF NOT EXISTS (`movement_id`) REFERENCES `inventory_movements` (`id`);

ALTER TABLE `inventory_items`
  ADD CONSTRAINT `fk_inventory_item_category` FOREIGN KEY IF NOT EXISTS (`category_id`) REFERENCES `inventory_categories` (`id`);

ALTER TABLE `inventory_movements`
  ADD CONSTRAINT `fk_inventory_movement_item` FOREIGN KEY IF NOT EXISTS (`item_id`) REFERENCES `inventory_items` (`id`),
  ADD CONSTRAINT `fk_inventory_movement_supplier` FOREIGN KEY IF NOT EXISTS (`supplier_id`) REFERENCES `inventory_suppliers` (`id`);

ALTER TABLE `lms_announcements`
  ADD CONSTRAINT `lms_announcements_ibfk_1` FOREIGN KEY IF NOT EXISTS (`topic_id`) REFERENCES `lms_topics` (`id`) ON DELETE SET NULL;

ALTER TABLE `lms_feedback`
  ADD CONSTRAINT `lms_feedback_ibfk_1` FOREIGN KEY IF NOT EXISTS (`topic_id`) REFERENCES `lms_topics` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `lms_feedback_ibfk_2` FOREIGN KEY IF NOT EXISTS (`subtopic_id`) REFERENCES `lms_subtopics` (`id`) ON DELETE SET NULL;

ALTER TABLE `lms_resources`
  ADD CONSTRAINT `lms_resources_ibfk_1` FOREIGN KEY IF NOT EXISTS (`topic_id`) REFERENCES `lms_topics` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `lms_resources_ibfk_2` FOREIGN KEY IF NOT EXISTS (`subtopic_id`) REFERENCES `lms_subtopics` (`id`) ON DELETE CASCADE;

ALTER TABLE `lms_student_progress`
  ADD CONSTRAINT `lms_student_progress_ibfk_1` FOREIGN KEY IF NOT EXISTS (`topic_id`) REFERENCES `lms_topics` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `lms_student_progress_ibfk_2` FOREIGN KEY IF NOT EXISTS (`subtopic_id`) REFERENCES `lms_subtopics` (`id`) ON DELETE SET NULL;

ALTER TABLE `lms_subtopics`
  ADD CONSTRAINT `lms_subtopics_ibfk_1` FOREIGN KEY IF NOT EXISTS (`topic_id`) REFERENCES `lms_topics` (`id`) ON DELETE CASCADE;

ALTER TABLE `notification_devices`
  ADD CONSTRAINT `fk_nd_notification` FOREIGN KEY IF NOT EXISTS (`notification_id`) REFERENCES `notifications` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `fk_nd_student` FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`) ON DELETE CASCADE;

ALTER TABLE `notification_reads`
  ADD CONSTRAINT `notification_reads_ibfk_1` FOREIGN KEY IF NOT EXISTS (`notification_id`) REFERENCES `notifications` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `notification_reads_ibfk_2` FOREIGN KEY IF NOT EXISTS (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;

ALTER TABLE `other_payments`
  ADD CONSTRAINT `fk_other_payment_class` FOREIGN KEY IF NOT EXISTS (`class_id`) REFERENCES `classes` (`id`),
  ADD CONSTRAINT `fk_other_payment_item` FOREIGN KEY IF NOT EXISTS (`payment_item_id`) REFERENCES `other_payment_items` (`id`),
  ADD CONSTRAINT `fk_other_payment_student` FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`),
  ADD CONSTRAINT `fk_other_payments_account` FOREIGN KEY IF NOT EXISTS (`payment_account_id`) REFERENCES `student_other_payment_accounts` (`id`);

ALTER TABLE `student_other_payment_accounts`
  ADD CONSTRAINT `fk_sopa_class` FOREIGN KEY IF NOT EXISTS (`class_id`) REFERENCES `classes` (`id`),
  ADD CONSTRAINT `fk_sopa_item` FOREIGN KEY IF NOT EXISTS (`payment_item_id`) REFERENCES `other_payment_items` (`id`),
  ADD CONSTRAINT `fk_sopa_student` FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`);

-- Restore original session setting.
SET FOREIGN_KEY_CHECKS = @LEOTRIX_OLD_FOREIGN_KEY_CHECKS;

-- Simple post-run table/column count:
SELECT TABLE_NAME, COUNT(*) AS column_count
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
GROUP BY TABLE_NAME
ORDER BY TABLE_NAME;

-- ============================================================
-- LEOTRIX BURSARY + INVENTORY SCHEMA SYNC
-- Safe/idempotent for MariaDB 11.x
-- Creates missing tables and adds missing columns.
-- Does NOT DROP tables/columns and does NOT DELETE application data.
-- ============================================================

SET @OLD_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS;
SET FOREIGN_KEY_CHECKS = 0;

-- ------------------------------------------------------------
-- fee_payments
-- ------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `fee_payments` (
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `student_id` BIGINT(20) UNSIGNED NOT NULL,
  `session_id` VARCHAR(20) NOT NULL,
  `amount_paid` DECIMAL(12,2) NOT NULL,
  `payment_date` DATE NOT NULL,
  `payment_method` VARCHAR(40) DEFAULT NULL,
  `receipt_number` VARCHAR(100) NOT NULL,
  `settlement_reference` VARCHAR(150) NOT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `fee_payments`
  ADD COLUMN IF NOT EXISTS `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
  ADD COLUMN IF NOT EXISTS `student_id` BIGINT(20) UNSIGNED NOT NULL AFTER `id`,
  ADD COLUMN IF NOT EXISTS `session_id` VARCHAR(20) NOT NULL AFTER `student_id`,
  ADD COLUMN IF NOT EXISTS `amount_paid` DECIMAL(12,2) NOT NULL AFTER `session_id`,
  ADD COLUMN IF NOT EXISTS `payment_date` DATE NOT NULL AFTER `amount_paid`,
  ADD COLUMN IF NOT EXISTS `payment_method` VARCHAR(40) DEFAULT NULL AFTER `payment_date`,
  ADD COLUMN IF NOT EXISTS `receipt_number` VARCHAR(100) NOT NULL AFTER `payment_method`,
  ADD COLUMN IF NOT EXISTS `settlement_reference` VARCHAR(150) NOT NULL AFTER `receipt_number`,
  ADD COLUMN IF NOT EXISTS `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `settlement_reference`;

ALTER TABLE `fee_payments`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_fee_payments_receipt_number` (`receipt_number`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_fee_payments_settlement_reference` (`settlement_reference`,`session_id`),
  ADD KEY IF NOT EXISTS `idx_fee_payments_student_session` (`student_id`,`session_id`),
  ADD KEY IF NOT EXISTS `idx_fee_payments_student` (`student_id`);

-- ------------------------------------------------------------
-- fee_payment_allocations
-- ------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `fee_payment_allocations` (
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `payment_id` BIGINT(20) UNSIGNED NOT NULL,
  `fee_id` BIGINT(20) UNSIGNED NOT NULL,
  `amount_applied` DECIMAL(12,2) NOT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `fee_payment_allocations`
  ADD COLUMN IF NOT EXISTS `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
  ADD COLUMN IF NOT EXISTS `payment_id` BIGINT(20) UNSIGNED NOT NULL AFTER `id`,
  ADD COLUMN IF NOT EXISTS `fee_id` BIGINT(20) UNSIGNED NOT NULL AFTER `payment_id`,
  ADD COLUMN IF NOT EXISTS `amount_applied` DECIMAL(12,2) NOT NULL AFTER `fee_id`,
  ADD COLUMN IF NOT EXISTS `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `amount_applied`;

ALTER TABLE `fee_payment_allocations`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_fee_payment_allocations` (`payment_id`,`fee_id`),
  ADD KEY IF NOT EXISTS `idx_fee_payment_allocations_fee` (`fee_id`);

-- ------------------------------------------------------------
-- bursary_requests
-- ------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `bursary_requests` (
  `actor_id` BIGINT(20) UNSIGNED NOT NULL,
  `request_id` VARCHAR(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
  `request_hash` CHAR(64) NOT NULL,
  `result` LONGTEXT DEFAULT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`actor_id`,`request_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `bursary_requests`
  ADD COLUMN IF NOT EXISTS `actor_id` BIGINT(20) UNSIGNED NOT NULL FIRST,
  ADD COLUMN IF NOT EXISTS `request_id` VARCHAR(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL AFTER `actor_id`,
  ADD COLUMN IF NOT EXISTS `request_hash` CHAR(64) NOT NULL AFTER `request_id`,
  ADD COLUMN IF NOT EXISTS `result` LONGTEXT DEFAULT NULL AFTER `request_hash`,
  ADD COLUMN IF NOT EXISTS `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `result`;

ALTER TABLE `bursary_requests`
  ADD PRIMARY KEY IF NOT EXISTS (`actor_id`,`request_id`);

-- ------------------------------------------------------------
-- bursary_discounts
-- ------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `bursary_discounts` (
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(150) NOT NULL,
  `description` TEXT NOT NULL,
  `kind` ENUM('predefined','custom') NOT NULL,
  `value_type` ENUM('percentage','fixed') NOT NULL,
  `value` DECIMAL(12,2) NOT NULL,
  `student_id` BIGINT(20) UNSIGNED DEFAULT NULL,
  `applies_to` ENUM('normal','other') NOT NULL,
  `session_id` BIGINT(20) UNSIGNED DEFAULT NULL,
  `class_id` BIGINT(20) UNSIGNED DEFAULT NULL,
  `category` VARCHAR(150) NOT NULL DEFAULT '',
  `starts_at` DATETIME DEFAULT NULL,
  `expires_at` DATETIME DEFAULT NULL,
  `is_active` TINYINT NOT NULL DEFAULT 1,
  `version` INT NOT NULL DEFAULT 1,
  `created_by` BIGINT(20) UNSIGNED NOT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `bursary_discounts`
  ADD COLUMN IF NOT EXISTS `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
  ADD COLUMN IF NOT EXISTS `name` VARCHAR(150) NOT NULL AFTER `id`,
  ADD COLUMN IF NOT EXISTS `description` TEXT NOT NULL AFTER `name`,
  ADD COLUMN IF NOT EXISTS `kind` ENUM('predefined','custom') NOT NULL AFTER `description`,
  ADD COLUMN IF NOT EXISTS `value_type` ENUM('percentage','fixed') NOT NULL AFTER `kind`,
  ADD COLUMN IF NOT EXISTS `value` DECIMAL(12,2) NOT NULL AFTER `value_type`,
  ADD COLUMN IF NOT EXISTS `student_id` BIGINT(20) UNSIGNED DEFAULT NULL AFTER `value`,
  ADD COLUMN IF NOT EXISTS `applies_to` ENUM('normal','other') NOT NULL AFTER `student_id`,
  ADD COLUMN IF NOT EXISTS `session_id` BIGINT(20) UNSIGNED DEFAULT NULL AFTER `applies_to`,
  ADD COLUMN IF NOT EXISTS `class_id` BIGINT(20) UNSIGNED DEFAULT NULL AFTER `session_id`,
  ADD COLUMN IF NOT EXISTS `category` VARCHAR(150) NOT NULL DEFAULT '' AFTER `class_id`,
  ADD COLUMN IF NOT EXISTS `starts_at` DATETIME DEFAULT NULL AFTER `category`,
  ADD COLUMN IF NOT EXISTS `expires_at` DATETIME DEFAULT NULL AFTER `starts_at`,
  ADD COLUMN IF NOT EXISTS `is_active` TINYINT NOT NULL DEFAULT 1 AFTER `expires_at`,
  ADD COLUMN IF NOT EXISTS `version` INT NOT NULL DEFAULT 1 AFTER `is_active`,
  ADD COLUMN IF NOT EXISTS `created_by` BIGINT(20) UNSIGNED NOT NULL AFTER `version`,
  ADD COLUMN IF NOT EXISTS `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `created_by`,
  ADD COLUMN IF NOT EXISTS `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `created_at`;

ALTER TABLE `bursary_discounts`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `idx_bursary_discount_active` (`is_active`,`student_id`,`applies_to`),
  ADD KEY IF NOT EXISTS `idx_bursary_discount_session_class` (`session_id`,`class_id`);

-- ------------------------------------------------------------
-- fee_charge_details
-- ------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `fee_charge_details` (
  `fee_id` BIGINT(20) UNSIGNED NOT NULL,
  `charge_kind` ENUM('normal','other') NOT NULL,
  `title` VARCHAR(150) NOT NULL,
  `description` TEXT NOT NULL,
  `category` VARCHAR(150) NOT NULL DEFAULT '',
  `class_id` BIGINT(20) UNSIGNED DEFAULT NULL,
  `student_name` VARCHAR(255) NOT NULL,
  `items_snapshot` LONGTEXT NOT NULL,
  `created_by` BIGINT(20) UNSIGNED NOT NULL,
  `created_by_name` VARCHAR(255) NOT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`fee_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `fee_charge_details`
  ADD COLUMN IF NOT EXISTS `fee_id` BIGINT(20) UNSIGNED NOT NULL FIRST,
  ADD COLUMN IF NOT EXISTS `charge_kind` ENUM('normal','other') NOT NULL AFTER `fee_id`,
  ADD COLUMN IF NOT EXISTS `title` VARCHAR(150) NOT NULL AFTER `charge_kind`,
  ADD COLUMN IF NOT EXISTS `description` TEXT NOT NULL AFTER `title`,
  ADD COLUMN IF NOT EXISTS `category` VARCHAR(150) NOT NULL DEFAULT '' AFTER `description`,
  ADD COLUMN IF NOT EXISTS `class_id` BIGINT(20) UNSIGNED DEFAULT NULL AFTER `category`,
  ADD COLUMN IF NOT EXISTS `student_name` VARCHAR(255) NOT NULL AFTER `class_id`,
  ADD COLUMN IF NOT EXISTS `items_snapshot` LONGTEXT NOT NULL AFTER `student_name`,
  ADD COLUMN IF NOT EXISTS `created_by` BIGINT(20) UNSIGNED NOT NULL AFTER `items_snapshot`,
  ADD COLUMN IF NOT EXISTS `created_by_name` VARCHAR(255) NOT NULL AFTER `created_by`,
  ADD COLUMN IF NOT EXISTS `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `created_by_name`;

ALTER TABLE `fee_charge_details`
  ADD PRIMARY KEY IF NOT EXISTS (`fee_id`),
  ADD KEY IF NOT EXISTS `idx_fee_charge_kind_category` (`charge_kind`,`category`),
  ADD KEY IF NOT EXISTS `idx_fee_charge_class` (`class_id`),
  ADD KEY IF NOT EXISTS `idx_fee_charge_created_by` (`created_by`);

-- ------------------------------------------------------------
-- fee_discounts
-- ------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `fee_discounts` (
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `fee_id` BIGINT(20) UNSIGNED NOT NULL,
  `discount_id` BIGINT(20) UNSIGNED NOT NULL,
  `student_id` BIGINT(20) UNSIGNED NOT NULL,
  `name` VARCHAR(150) NOT NULL,
  `kind` ENUM('predefined','custom') NOT NULL,
  `value_type` ENUM('percentage','fixed') NOT NULL,
  `value` DECIMAL(12,2) NOT NULL,
  `original_amount` DECIMAL(12,2) NOT NULL,
  `eligible_balance` DECIMAL(12,2) NOT NULL,
  `discount_amount` DECIMAL(12,2) NOT NULL,
  `payable_amount` DECIMAL(12,2) NOT NULL,
  `definition_snapshot` LONGTEXT NOT NULL,
  `applied_by` BIGINT(20) UNSIGNED NOT NULL,
  `applied_by_name` VARCHAR(255) NOT NULL,
  `applied_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `fee_discounts`
  ADD COLUMN IF NOT EXISTS `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
  ADD COLUMN IF NOT EXISTS `fee_id` BIGINT(20) UNSIGNED NOT NULL AFTER `id`,
  ADD COLUMN IF NOT EXISTS `discount_id` BIGINT(20) UNSIGNED NOT NULL AFTER `fee_id`,
  ADD COLUMN IF NOT EXISTS `student_id` BIGINT(20) UNSIGNED NOT NULL AFTER `discount_id`,
  ADD COLUMN IF NOT EXISTS `name` VARCHAR(150) NOT NULL AFTER `student_id`,
  ADD COLUMN IF NOT EXISTS `kind` ENUM('predefined','custom') NOT NULL AFTER `name`,
  ADD COLUMN IF NOT EXISTS `value_type` ENUM('percentage','fixed') NOT NULL AFTER `kind`,
  ADD COLUMN IF NOT EXISTS `value` DECIMAL(12,2) NOT NULL AFTER `value_type`,
  ADD COLUMN IF NOT EXISTS `original_amount` DECIMAL(12,2) NOT NULL AFTER `value`,
  ADD COLUMN IF NOT EXISTS `eligible_balance` DECIMAL(12,2) NOT NULL AFTER `original_amount`,
  ADD COLUMN IF NOT EXISTS `discount_amount` DECIMAL(12,2) NOT NULL AFTER `eligible_balance`,
  ADD COLUMN IF NOT EXISTS `payable_amount` DECIMAL(12,2) NOT NULL AFTER `discount_amount`,
  ADD COLUMN IF NOT EXISTS `definition_snapshot` LONGTEXT NOT NULL AFTER `payable_amount`,
  ADD COLUMN IF NOT EXISTS `applied_by` BIGINT(20) UNSIGNED NOT NULL AFTER `definition_snapshot`,
  ADD COLUMN IF NOT EXISTS `applied_by_name` VARCHAR(255) NOT NULL AFTER `applied_by`,
  ADD COLUMN IF NOT EXISTS `applied_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `applied_by_name`;

ALTER TABLE `fee_discounts`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_fee_discounts_fee` (`fee_id`),
  ADD KEY IF NOT EXISTS `idx_fee_discounts_discount` (`discount_id`),
  ADD KEY IF NOT EXISTS `idx_fee_discounts_student` (`student_id`),
  ADD KEY IF NOT EXISTS `idx_fee_discounts_applied_by` (`applied_by`);

-- ------------------------------------------------------------
-- fee_payment_details
-- ------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `fee_payment_details` (
  `payment_id` BIGINT(20) UNSIGNED NOT NULL,
  `fee_id` BIGINT(20) UNSIGNED NOT NULL,
  `title` VARCHAR(150) NOT NULL,
  `charge_kind` ENUM('normal','other') NOT NULL,
  `original_amount` DECIMAL(12,2) NOT NULL,
  `discount_amount` DECIMAL(12,2) NOT NULL,
  `amount_due` DECIMAL(12,2) NOT NULL,
  `balance` DECIMAL(12,2) NOT NULL,
  `received_by_id` BIGINT(20) UNSIGNED NOT NULL,
  `received_by` VARCHAR(255) NOT NULL,
  `student_name` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`payment_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `fee_payment_details`
  ADD COLUMN IF NOT EXISTS `payment_id` BIGINT(20) UNSIGNED NOT NULL FIRST,
  ADD COLUMN IF NOT EXISTS `fee_id` BIGINT(20) UNSIGNED NOT NULL AFTER `payment_id`,
  ADD COLUMN IF NOT EXISTS `title` VARCHAR(150) NOT NULL AFTER `fee_id`,
  ADD COLUMN IF NOT EXISTS `charge_kind` ENUM('normal','other') NOT NULL AFTER `title`,
  ADD COLUMN IF NOT EXISTS `original_amount` DECIMAL(12,2) NOT NULL AFTER `charge_kind`,
  ADD COLUMN IF NOT EXISTS `discount_amount` DECIMAL(12,2) NOT NULL AFTER `original_amount`,
  ADD COLUMN IF NOT EXISTS `amount_due` DECIMAL(12,2) NOT NULL AFTER `discount_amount`,
  ADD COLUMN IF NOT EXISTS `balance` DECIMAL(12,2) NOT NULL AFTER `amount_due`,
  ADD COLUMN IF NOT EXISTS `received_by_id` BIGINT(20) UNSIGNED NOT NULL AFTER `balance`,
  ADD COLUMN IF NOT EXISTS `received_by` VARCHAR(255) NOT NULL AFTER `received_by_id`,
  ADD COLUMN IF NOT EXISTS `student_name` VARCHAR(255) NOT NULL AFTER `received_by`;

ALTER TABLE `fee_payment_details`
  ADD PRIMARY KEY IF NOT EXISTS (`payment_id`),
  ADD KEY IF NOT EXISTS `idx_fee_payment_details_fee` (`fee_id`),
  ADD KEY IF NOT EXISTS `idx_fee_payment_details_received_by` (`received_by_id`);

-- ------------------------------------------------------------
-- bursary_activity
-- ------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `bursary_activity` (
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `action` VARCHAR(40) NOT NULL,
  `record_id` BIGINT(20) NOT NULL,
  `performed_by_id` BIGINT(20) UNSIGNED NOT NULL,
  `performed_by` VARCHAR(255) NOT NULL,
  `snapshot` LONGTEXT NOT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `bursary_activity`
  ADD COLUMN IF NOT EXISTS `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
  ADD COLUMN IF NOT EXISTS `action` VARCHAR(40) NOT NULL AFTER `id`,
  ADD COLUMN IF NOT EXISTS `record_id` BIGINT(20) NOT NULL AFTER `action`,
  ADD COLUMN IF NOT EXISTS `performed_by_id` BIGINT(20) UNSIGNED NOT NULL AFTER `record_id`,
  ADD COLUMN IF NOT EXISTS `performed_by` VARCHAR(255) NOT NULL AFTER `performed_by_id`,
  ADD COLUMN IF NOT EXISTS `snapshot` LONGTEXT NOT NULL AFTER `performed_by`,
  ADD COLUMN IF NOT EXISTS `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `snapshot`;

ALTER TABLE `bursary_activity`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD KEY IF NOT EXISTS `idx_bursary_activity_action_record` (`action`,`record_id`),
  ADD KEY IF NOT EXISTS `idx_bursary_activity_performed_by` (`performed_by_id`);

-- ------------------------------------------------------------
-- inventory_issuances
-- ------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `inventory_issuances` (
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `student_id` BIGINT(20) UNSIGNED DEFAULT NULL,
  `recipient` VARCHAR(255) NOT NULL,
  `department` VARCHAR(150) NOT NULL DEFAULT '',
  `date` DATE NOT NULL,
  `reference` VARCHAR(150) NOT NULL,
  `notes` TEXT NOT NULL,
  `issued_by` BIGINT(20) UNSIGNED NOT NULL,
  `issued_by_name` VARCHAR(255) NOT NULL,
  `request_id` VARCHAR(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
  `request_hash` CHAR(64) NOT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `inventory_issuances`
  ADD COLUMN IF NOT EXISTS `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
  ADD COLUMN IF NOT EXISTS `student_id` BIGINT(20) UNSIGNED DEFAULT NULL AFTER `id`,
  ADD COLUMN IF NOT EXISTS `recipient` VARCHAR(255) NOT NULL AFTER `student_id`,
  ADD COLUMN IF NOT EXISTS `department` VARCHAR(150) NOT NULL DEFAULT '' AFTER `recipient`,
  ADD COLUMN IF NOT EXISTS `date` DATE NOT NULL AFTER `department`,
  ADD COLUMN IF NOT EXISTS `reference` VARCHAR(150) NOT NULL AFTER `date`,
  ADD COLUMN IF NOT EXISTS `notes` TEXT NOT NULL AFTER `reference`,
  ADD COLUMN IF NOT EXISTS `issued_by` BIGINT(20) UNSIGNED NOT NULL AFTER `notes`,
  ADD COLUMN IF NOT EXISTS `issued_by_name` VARCHAR(255) NOT NULL AFTER `issued_by`,
  ADD COLUMN IF NOT EXISTS `request_id` VARCHAR(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL AFTER `issued_by_name`,
  ADD COLUMN IF NOT EXISTS `request_hash` CHAR(64) NOT NULL AFTER `request_id`,
  ADD COLUMN IF NOT EXISTS `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `request_hash`;

ALTER TABLE `inventory_issuances`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_inventory_issuances_issued_by_request` (`issued_by`,`request_id`),
  ADD KEY IF NOT EXISTS `idx_inventory_issuances_date` (`date`,`id`),
  ADD KEY IF NOT EXISTS `idx_inventory_issuances_student` (`student_id`);

-- ------------------------------------------------------------
-- inventory_issuance_items
-- ------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `inventory_issuance_items` (
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `issuance_id` BIGINT(20) UNSIGNED NOT NULL,
  `inventory_item_id` BIGINT(20) UNSIGNED NOT NULL,
  `movement_id` BIGINT(20) UNSIGNED NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `inventory_issuance_items`
  ADD COLUMN IF NOT EXISTS `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
  ADD COLUMN IF NOT EXISTS `issuance_id` BIGINT(20) UNSIGNED NOT NULL AFTER `id`,
  ADD COLUMN IF NOT EXISTS `inventory_item_id` BIGINT(20) UNSIGNED NOT NULL AFTER `issuance_id`,
  ADD COLUMN IF NOT EXISTS `movement_id` BIGINT(20) UNSIGNED NOT NULL AFTER `inventory_item_id`;

ALTER TABLE `inventory_issuance_items`
  ADD PRIMARY KEY IF NOT EXISTS (`id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_inventory_issuance_items_issuance_item` (`issuance_id`,`inventory_item_id`),
  ADD UNIQUE KEY IF NOT EXISTS `uq_inventory_issuance_items_movement` (`movement_id`),
  ADD KEY IF NOT EXISTS `idx_inventory_issuance_items_item` (`inventory_item_id`);

-- ------------------------------------------------------------
-- FOREIGN KEYS
-- IF NOT EXISTS keeps this section re-runnable.
-- ------------------------------------------------------------
ALTER TABLE `fee_payments`
  ADD CONSTRAINT `fk_fee_payments_student`
  FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`) ON DELETE RESTRICT;

ALTER TABLE `fee_payment_allocations`
  ADD CONSTRAINT `fk_fee_payment_allocations_payment`
  FOREIGN KEY IF NOT EXISTS (`payment_id`) REFERENCES `fee_payments` (`id`) ON DELETE RESTRICT,
  ADD CONSTRAINT `fk_fee_payment_allocations_fee`
  FOREIGN KEY IF NOT EXISTS (`fee_id`) REFERENCES `fees` (`id`) ON DELETE RESTRICT;

ALTER TABLE `bursary_requests`
  ADD CONSTRAINT `fk_bursary_requests_actor`
  FOREIGN KEY IF NOT EXISTS (`actor_id`) REFERENCES `users` (`id`) ON DELETE RESTRICT;

ALTER TABLE `bursary_discounts`
  ADD CONSTRAINT `fk_bursary_discounts_student`
  FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`) ON DELETE RESTRICT,
  ADD CONSTRAINT `fk_bursary_discounts_session`
  FOREIGN KEY IF NOT EXISTS (`session_id`) REFERENCES `academic_sessions` (`id`) ON DELETE RESTRICT,
  ADD CONSTRAINT `fk_bursary_discounts_class`
  FOREIGN KEY IF NOT EXISTS (`class_id`) REFERENCES `classes` (`id`) ON DELETE RESTRICT,
  ADD CONSTRAINT `fk_bursary_discounts_created_by`
  FOREIGN KEY IF NOT EXISTS (`created_by`) REFERENCES `users` (`id`) ON DELETE RESTRICT;

ALTER TABLE `fee_charge_details`
  ADD CONSTRAINT `fk_fee_charge_details_fee`
  FOREIGN KEY IF NOT EXISTS (`fee_id`) REFERENCES `fees` (`id`) ON DELETE RESTRICT,
  ADD CONSTRAINT `fk_fee_charge_details_class`
  FOREIGN KEY IF NOT EXISTS (`class_id`) REFERENCES `classes` (`id`) ON DELETE RESTRICT,
  ADD CONSTRAINT `fk_fee_charge_details_created_by`
  FOREIGN KEY IF NOT EXISTS (`created_by`) REFERENCES `users` (`id`) ON DELETE RESTRICT;

ALTER TABLE `fee_discounts`
  ADD CONSTRAINT `fk_fee_discounts_fee`
  FOREIGN KEY IF NOT EXISTS (`fee_id`) REFERENCES `fees` (`id`) ON DELETE RESTRICT,
  ADD CONSTRAINT `fk_fee_discounts_discount`
  FOREIGN KEY IF NOT EXISTS (`discount_id`) REFERENCES `bursary_discounts` (`id`) ON DELETE RESTRICT,
  ADD CONSTRAINT `fk_fee_discounts_student`
  FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`) ON DELETE RESTRICT,
  ADD CONSTRAINT `fk_fee_discounts_applied_by`
  FOREIGN KEY IF NOT EXISTS (`applied_by`) REFERENCES `users` (`id`) ON DELETE RESTRICT;

ALTER TABLE `fee_payment_details`
  ADD CONSTRAINT `fk_fee_payment_details_payment`
  FOREIGN KEY IF NOT EXISTS (`payment_id`) REFERENCES `fee_payments` (`id`) ON DELETE RESTRICT,
  ADD CONSTRAINT `fk_fee_payment_details_fee`
  FOREIGN KEY IF NOT EXISTS (`fee_id`) REFERENCES `fees` (`id`) ON DELETE RESTRICT,
  ADD CONSTRAINT `fk_fee_payment_details_received_by`
  FOREIGN KEY IF NOT EXISTS (`received_by_id`) REFERENCES `users` (`id`) ON DELETE RESTRICT;

ALTER TABLE `bursary_activity`
  ADD CONSTRAINT `fk_bursary_activity_performed_by`
  FOREIGN KEY IF NOT EXISTS (`performed_by_id`) REFERENCES `users` (`id`) ON DELETE RESTRICT;

ALTER TABLE `inventory_issuances`
  ADD CONSTRAINT `fk_inventory_issuances_student`
  FOREIGN KEY IF NOT EXISTS (`student_id`) REFERENCES `students` (`id`) ON DELETE RESTRICT,
  ADD CONSTRAINT `fk_inventory_issuances_issued_by`
  FOREIGN KEY IF NOT EXISTS (`issued_by`) REFERENCES `users` (`id`) ON DELETE RESTRICT;

ALTER TABLE `inventory_issuance_items`
  ADD CONSTRAINT `fk_inventory_issuance_items_issuance`
  FOREIGN KEY IF NOT EXISTS (`issuance_id`) REFERENCES `inventory_issuances` (`id`) ON DELETE RESTRICT,
  ADD CONSTRAINT `fk_inventory_issuance_items_item`
  FOREIGN KEY IF NOT EXISTS (`inventory_item_id`) REFERENCES `inventory_items` (`id`) ON DELETE RESTRICT,
  ADD CONSTRAINT `fk_inventory_issuance_items_movement`
  FOREIGN KEY IF NOT EXISTS (`movement_id`) REFERENCES `inventory_movements` (`id`) ON DELETE RESTRICT;

SET FOREIGN_KEY_CHECKS = @OLD_FOREIGN_KEY_CHECKS;

-- ------------------------------------------------------------
-- Verification
-- ------------------------------------------------------------
SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME IN (
    'fee_payments',
    'fee_payment_allocations',
    'bursary_requests',
    'bursary_discounts',
    'fee_charge_details',
    'fee_discounts',
    'fee_payment_details',
    'bursary_activity',
    'inventory_issuances',
    'inventory_issuance_items'
  )
ORDER BY TABLE_NAME, ORDINAL_POSITION;






ALTER TABLE `users` CHANGE `role` `role` ENUM('parent','guardian','teacher','admin','secretary','bursar','principal','vice_principal','librarian','inventory') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'parent';