-- Replaces sidebar.php's hardcoded `if ($role === 'admin' || ...)` checks
-- (one per nav item, scattered through the file) with two tables an admin
-- can manage through a settings page: no code changes needed to grant or
-- revoke a role's access to an existing module.
--
-- nav_modules: one row per navigable item -- a standalone link (like
-- "Employees") or a submenu parent (like "Attendance", which groups
-- several children under it, matching the existing collapsible-submenu
-- structure). parent_id groups children under their parent, exactly like
-- the current sidebar's nested <div id="...-sub"> sections.
--
-- nav_module_roles: which roles can see a given module -- a role needs an
-- explicit row here to see it; no rows means nobody sees it (not "show
-- to everyone"), so a genuinely public item (like the My Portal / ESS
-- section) is seeded with a row for every known role instead of relying
-- on an implicit "empty means public" rule that's easy to misread.
--
-- Seeded to match sidebar.php's CURRENT behavior exactly -- running this
-- migration changes nothing about who sees what. It just moves the
-- decision from source code into data an admin can edit later.
--
-- Safe to run more than once.

CREATE TABLE IF NOT EXISTS `nav_modules` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `module_key` VARCHAR(100) NOT NULL,
    `label` VARCHAR(150) NOT NULL,
    `url` VARCHAR(255) DEFAULT NULL,
    `icon_svg` TEXT DEFAULT NULL,
    `parent_id` INT(11) DEFAULT NULL,
    `section_label` VARCHAR(150) DEFAULT NULL,
    `display_order` INT(11) NOT NULL DEFAULT 0,
    `is_active` TINYINT(1) NOT NULL DEFAULT 1,
    `requires_shift_gate` TINYINT(1) NOT NULL DEFAULT 0,
    `created_at` TIMESTAMP NOT NULL DEFAULT current_timestamp(),
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_module_key` (`module_key`),
    KEY `idx_parent` (`parent_id`),
    CONSTRAINT `fk_nav_parent` FOREIGN KEY (`parent_id`) REFERENCES `nav_modules` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `nav_module_roles` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `module_id` INT(11) NOT NULL,
    `role` VARCHAR(50) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_module_role` (`module_id`, `role`),
    CONSTRAINT `fk_nmr_module` FOREIGN KEY (`module_id`) REFERENCES `nav_modules` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
-- Seed: top-level items and submenu parents
-- ---------------------------------------------------------------------
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `section_label`, `display_order`, `requires_shift_gate`) VALUES
    ('dashboard',        'Dashboard',          'dashboard.php',               NULL, 'Core Operations', 10, 0),
    ('employees',        'Employees',          'employees/employees.php',     NULL, 'Core Operations', 20, 0),
    ('attendance',       'Attendance',         NULL,                          NULL, 'Core Operations', 30, 0),
    ('mod',              'Manager on Duty',    'mod/index.php',               NULL, 'Core Operations', 40, 1),
    ('department',       'Department',         NULL,                          NULL, 'Core Operations', 50, 0),
    ('my_portal',        'My Portal',          NULL,                          NULL, 'Employee Hub',     10, 0),
    ('leave_manager',    'Leave Manager',      NULL,                          NULL, 'Leave Operations', 10, 0),
    ('loan_manager',     'Loan Manager',       NULL,                          NULL, 'Loan Operations',  10, 0),
    ('payroll_hub',      'Payroll Hub',        NULL,                          NULL, 'Financial Operations', 10, 0),
    ('card_manager',     'Card Manager',       NULL,                          NULL, 'Card Management',  10, 0),
    ('system_settings',  'System Settings',    'settings.php',                NULL, 'System Administration', 10, 0),
    ('module_access',    'Module Access',      'module_access.php',           NULL, 'System Administration', 20, 0),
    ('performance',      'Performance',        NULL,                          NULL, 'Employee Hub',     20, 0);
-- ---------------------------------------------------------------------
-- Seed: children (attendance submenu)
-- ---------------------------------------------------------------------
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'attendance_daily_logs', 'Daily Logs', 'attendance.php', id, 10 FROM `nav_modules` WHERE `module_key` = 'attendance';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'attendance_historical', 'Historical Matrix', 'attendance/attendance_records.php', id, 20 FROM `nav_modules` WHERE `module_key` = 'attendance';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'attendance_fix_logs', 'Fix Logs', 'attendance/fix-logs.php', id, 30 FROM `nav_modules` WHERE `module_key` = 'attendance';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'attendance_shift_config', 'Shift Configuration', 'shift.php', id, 40 FROM `nav_modules` WHERE `module_key` = 'attendance';

-- Department submenu
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'department_roster', 'Roster Schedules', 'department/department_schedule.php', id, 10 FROM `nav_modules` WHERE `module_key` = 'department';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'department_manage', 'Manage Division', 'department/departments.php', id, 20 FROM `nav_modules` WHERE `module_key` = 'department';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'department_staff_assignment', 'Staff Assignment', 'department/department_settings.php', id, 30 FROM `nav_modules` WHERE `module_key` = 'department';

-- My Portal (ESS) submenu
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'ess_dashboard', 'ESS Dashboard', 'ess/dashboard.php', id, 10 FROM `nav_modules` WHERE `module_key` = 'my_portal';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'ess_schedule', 'My Schedule', 'ess/my_schedule.php', id, 20 FROM `nav_modules` WHERE `module_key` = 'my_portal';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'ess_attendance', 'My Attendance', 'ess/my_attendance.php', id, 30 FROM `nav_modules` WHERE `module_key` = 'my_portal';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'ess_leave', 'Leave Requests', 'ess/my_leave.php', id, 40 FROM `nav_modules` WHERE `module_key` = 'my_portal';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'ess_overtime', 'Overtime Logs', 'ess/my_overtime.php', id, 50 FROM `nav_modules` WHERE `module_key` = 'my_portal';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'ess_payslips', 'My Payslips', 'ess/my_payslips.php', id, 60 FROM `nav_modules` WHERE `module_key` = 'my_portal';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'ess_loans', 'Loan Matrix', 'ess/my_loans.php', id, 70 FROM `nav_modules` WHERE `module_key` = 'my_portal';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'ess_assets', 'Assigned Assets', 'ess/my_assets.php', id, 80 FROM `nav_modules` WHERE `module_key` = 'my_portal';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'ess_appraisals', 'My Appraisals', 'ess/my_appraisals.php', id, 90 FROM `nav_modules` WHERE `module_key` = 'my_portal';

-- Leave Manager submenu
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'leave_application', 'Leave Application', 'leave_managment/index.php', id, 10 FROM `nav_modules` WHERE `module_key` = 'leave_manager';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'leave_approval', 'Leave Approval', 'leave_managment/leave_approval.php', id, 20 FROM `nav_modules` WHERE `module_key` = 'leave_manager';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'leave_balance', 'Leave Balance', 'leave_managment/leave_balance.php', id, 30 FROM `nav_modules` WHERE `module_key` = 'leave_manager';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'leave_policy', 'Leave Policy', 'leave_managment/leave_types.php', id, 40 FROM `nav_modules` WHERE `module_key` = 'leave_manager';

-- Loan Manager submenu
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'loan_request', 'Loan Request', 'loan_management/loan_request.php', id, 10 FROM `nav_modules` WHERE `module_key` = 'loan_manager';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'loan_history', 'Loan History', 'loan_management/loan_history.php', id, 20 FROM `nav_modules` WHERE `module_key` = 'loan_manager';

-- Payroll Hub submenu
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'payroll_cycles', 'Cycle Runs', 'payroll/cycles.php', id, 10 FROM `nav_modules` WHERE `module_key` = 'payroll_hub';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'payroll_employee', 'Payroll', 'payroll/employee_payroll.php', id, 20 FROM `nav_modules` WHERE `module_key` = 'payroll_hub';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'payroll_group', 'Group Payroll', 'payroll/group_payroll.php', id, 30 FROM `nav_modules` WHERE `module_key` = 'payroll_hub';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'payroll_components', 'Components', 'payroll/components.php', id, 40 FROM `nav_modules` WHERE `module_key` = 'payroll_hub';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'payroll_settings', 'Settings', 'payroll/settings.php', id, 50 FROM `nav_modules` WHERE `module_key` = 'payroll_hub';

-- Card Manager submenu
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'card_dashboard', 'Dashboard', 'card_management/index.php', id, 10 FROM `nav_modules` WHERE `module_key` = 'card_manager';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'card_settings', 'Settings', 'card_management/settings.php', id, 20 FROM `nav_modules` WHERE `module_key` = 'card_manager';

-- ---------------------------------------------------------------------
-- Seed: role access -- matches sidebar.php's hardcoded checks exactly
-- ---------------------------------------------------------------------
INSERT INTO `nav_module_roles` (`module_id`, `role`)
SELECT id, r.role FROM `nav_modules`
CROSS JOIN (SELECT 'admin' AS role UNION SELECT 'superadmin' UNION SELECT 'hr') r
WHERE `module_key` IN ('dashboard', 'employees', 'attendance', 'attendance_historical', 'attendance_fix_logs', 'attendance_shift_config', 'leave_manager', 'leave_application', 'leave_approval', 'leave_balance', 'leave_policy', 'payroll_hub', 'payroll_cycles', 'payroll_employee', 'payroll_group', 'payroll_components', 'payroll_settings', 'card_manager', 'card_dashboard', 'card_settings');

INSERT INTO `nav_module_roles` (`module_id`, `role`)
SELECT id, r.role FROM `nav_modules`
CROSS JOIN (SELECT 'admin' AS role UNION SELECT 'superadmin') r
WHERE `module_key` IN ('attendance_daily_logs', 'department_manage', 'department_staff_assignment', 'loan_manager', 'loan_request', 'loan_history');

INSERT INTO `nav_module_roles` (`module_id`, `role`)
SELECT id, r.role FROM `nav_modules`
CROSS JOIN (SELECT 'admin' AS role UNION SELECT 'superadmin' UNION SELECT 'manager' UNION SELECT 'supervisor' UNION SELECT 'hr' UNION SELECT 'gm' UNION SELECT 'staff' UNION SELECT 'accounts' UNION SELECT 'audit' UNION SELECT 'opm') r
WHERE `module_key` = 'mod';

INSERT INTO `nav_module_roles` (`module_id`, `role`)
SELECT id, r.role FROM `nav_modules`
CROSS JOIN (SELECT 'admin' AS role UNION SELECT 'superadmin' UNION SELECT 'manager' UNION SELECT 'supervisor' UNION SELECT 'hr') r
WHERE `module_key` IN ('department', 'department_roster');

INSERT INTO `nav_module_roles` (`module_id`, `role`)
SELECT id, r.role FROM `nav_modules`
CROSS JOIN (SELECT 'admin' AS role UNION SELECT 'superadmin' UNION SELECT 'manager' UNION SELECT 'supervisor' UNION SELECT 'hr' UNION SELECT 'gm' UNION SELECT 'staff') r
WHERE `module_key` IN ('my_portal', 'ess_dashboard', 'ess_schedule', 'ess_attendance', 'ess_leave', 'ess_overtime', 'ess_payslips', 'ess_loans', 'ess_assets', 'ess_appraisals');

INSERT INTO `nav_module_roles` (`module_id`, `role`)
SELECT id, 'superadmin' FROM `nav_modules` WHERE `module_key` IN ('system_settings', 'module_access');

-- ---------------------------------------------------------------------
-- Icons -- only top-level items/submenu parents show one in the current
-- design (children inside a submenu are plain text links), copied
-- verbatim from sidebar.php's existing <svg> markup so the visual result
-- is identical after the switch to data-driven rendering.
-- ---------------------------------------------------------------------
UPDATE `nav_modules` SET `icon_svg` = 'M3.75 6A2.25 2.25 0 016 3.75h2.25A2.25 2.25 0 0110.5 6v2.25a2.25 2.25 0 01-2.25 2.25H6a2.25 2.25 0 01-2.25-2.25V6zM3.75 15.75A2.25 2.25 0 016 13.5h2.25a2.25 2.25 0 012.25 2.25V18a2.25 2.25 0 01-2.25 2.25H6A2.25 2.25 0 013.75 18v-2.25zM13.5 6a2.25 2.25 0 012.25-2.25H18A2.25 2.25 0 0120.25 6v2.25A2.25 2.25 0 0118 10.5h-2.25a2.25 2.25 0 01-2.25-2.25V6zM13.5 15.75a2.25 2.25 0 012.25-2.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-2.25A2.25 2.25 0 0113.5 18v-2.25z' WHERE `module_key` = 'dashboard';
UPDATE `nav_modules` SET `icon_svg` = 'M15 19.128a9.38 9.38 0 002.625.372 9.337 9.337 0 004.121-.952 4.125 4.125 0 00-7.533-2.493M15 19.128v-.003c0-1.113-.285-2.16-.786-3.07M15 19.128v.106A12.318 12.318 0 018.624 21c-2.331 0-4.512-.645-6.374-1.766l-.001-.109a6.375 6.375 0 0111.964-3.07M12 6.375a3.375 3.375 0 11-6.75 0 3.375 3.375 0 016.75 0zm8.25 2.25a2.625 2.625 0 11-5.25 0 2.625 2.625 0 015.25 0z' WHERE `module_key` = 'employees';
UPDATE `nav_modules` SET `icon_svg` = 'M12 6v6h4.5m4.5 0a9 9 0 11-18 0 9 9 0 0118 0z' WHERE `module_key` = 'attendance';
UPDATE `nav_modules` SET `icon_svg` = 'M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z' WHERE `module_key` = 'mod';
UPDATE `nav_modules` SET `icon_svg` = 'M3.75 21h16.5M4.5 3h15M5.25 3v18m13.5-18v18M9 6.75h1.5s0 0 0 0M13.5 6.75h1.5M9 10.5h1.5M13.5 10.5h1.5M9 14.25h1.5M13.5 14.25h1.5M9 18h6v3H9v-3z' WHERE `module_key` = 'department';
UPDATE `nav_modules` SET `icon_svg` = 'M17.982 18.725A7.488 7.488 0 0012 15.75a7.488 7.488 0 00-5.982 2.975m11.963 0a9 9 0 10-11.963 0m11.963 0A8.966 8.966 0 0112 21a8.966 8.966 0 01-5.982-2.275M15 9.75a3 3 0 11-6 0 3 3 0 016 0z' WHERE `module_key` = 'my_portal';
UPDATE `nav_modules` SET `icon_svg` = 'M6.75 3v2.25M17.25 3v2.25M3 18.75V7.5a2.25 2.25 0 012.25-2.25h13.5A2.25 2.25 0 0121 7.5v11.25m-18 0A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75m-18 0v-7.5A2.25 2.25 0 015.25 9h13.5A2.25 2.25 0 0121 11.25v7.5' WHERE `module_key` = 'leave_manager';
UPDATE `nav_modules` SET `icon_svg` = 'M2.25 18.75a60.07 60.07 0 0115.797 2.101c.727.198 1.453-.342 1.453-1.096V18.75M3.75 4.5h16.5a1.5 1.5 0 011.5 1.5v9a1.5 1.5 0 01-1.5 1.5H3.75a1.5 1.5 0 01-1.5-1.5v-9a1.5 1.5 0 011.5-1.5zm10.5 6a2.25 2.25 0 11-4.5 0 2.25 2.25 0 014.5 0z' WHERE `module_key` = 'loan_manager';
UPDATE `nav_modules` SET `icon_svg` = 'M21 12a9 9 0 11-18 0 9 9 0 0118 0zM12 7.5v9m3-6H9.75a1.5 1.5 0 000 3h4.5a1.5 1.5 0 010 3H9' WHERE `module_key` = 'payroll_hub';
UPDATE `nav_modules` SET `icon_svg` = 'M2.25 8.25h19.5M2.25 9h19.5pt18.75A2.25 2.25 0 0021 6.75H3A2.25 2.25 0 00.75 9v9A2.25 2.25 0 003 20.25h18A2.25 2.25 0 0023.25 18V9M6 15h.008v.008H6V15zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zm3 0h.008v.008H9.375V15zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z' WHERE `module_key` = 'card_manager';
UPDATE `nav_modules` SET `icon_svg` = 'M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.325.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 011.37.49l1.296 2.247a1.125 1.125 0 01-.26 1.431l-1.003.827c-.293.241-.438.613-.43.992a7.723 7.723 0 010 .255c-.008.378.137.75.43.991l1.004.827c.424.35.534.955.26 1.43l-1.298 2.247a1.125 1.125 0 01-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.47 6.47 0 01-.22.128c-.331.183-.581.495-.644.869l-.213 1.281c-.09.543-.56.94-1.11.94h-2.594c-.55 0-1.019-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 01-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 01-1.369-.49l-1.297-2.247a1.125 1.125 0 01.26-1.431l1.004-.827c.292-.24.437-.613.43-.991a6.932 6.932 0 010-.255c.007-.378-.138-.75-.43-.992l-1.004-.827a1.125 1.125 0 01-.26-1.43l1.297-2.247a1.125 1.125 0 011.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.086.22-.128.332-.183.582-.495.644-.869l.214-1.281zM12 15a3 3 0 100-6 3 3 0 000 6z' WHERE `module_key` = 'system_settings';
UPDATE `nav_modules` SET `icon_svg` = 'M9 12h3.75M9 15h3.75M9 18h3.75m3 .75H18a2.25 2.25 0 002.25-2.25V6.108c0-1.135-.845-2.098-1.976-2.192a48.424 48.424 0 00-1.123-.08m-5.801 0c-.065.21-.1.433-.1.664 0 .414.336.75.75.75h4.5a.75.75 0 00.75-.75 2.25 2.25 0 00-.1-.664m-5.8 0A2.251 2.251 0 0113.5 2.25H15c1.012 0 1.867.668 2.15 1.586m-5.8 0c-.376.023-.75.05-1.124.08C9.095 4.01 8.25 4.973 8.25 6.108V8.25m0 0H4.875c-.621 0-1.125.504-1.125 1.125v11.25c0 .621.504 1.125 1.125 1.125h9.75c.621 0 1.125-.504 1.125-1.125V9.375c0-.621-.504-1.125-1.125-1.125H8.25zM6.75 12h.008v.008H6.75V12zm0 3h.008v.008H6.75V15zm0 3h.008v.008H6.75V18z' WHERE `module_key` = 'module_access';

-- Performance / Appraisal submenu
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'perf_dashboard', 'Performance Dashboard', 'performance/appraisal_dashboard.php', id, 10 FROM `nav_modules` WHERE `module_key` = 'performance';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'perf_my_appraisal', 'My Self-Appraisal', 'performance/employee_appraisal.php', id, 20 FROM `nav_modules` WHERE `module_key` = 'performance';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'perf_my_history', 'My Appraisal History', 'performance/employee_appraisal_history.php', id, 30 FROM `nav_modules` WHERE `module_key` = 'performance';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'perf_manager_review', 'Manager Reviews', 'performance/manager_review.php', id, 40 FROM `nav_modules` WHERE `module_key` = 'performance';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'perf_hr_review', 'HR Reviews', 'performance/hr_review.php', id, 50 FROM `nav_modules` WHERE `module_key` = 'performance';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'perf_periods', 'Appraisal Periods', 'performance/appraisal_periods.php', id, 60 FROM `nav_modules` WHERE `module_key` = 'performance';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'perf_kpi_setup', 'KPI Setup', 'performance/kpi_setup.php', id, 70 FROM `nav_modules` WHERE `module_key` = 'performance';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'perf_create', 'Start New Appraisals', 'performance/create_appraisal.php', id, 80 FROM `nav_modules` WHERE `module_key` = 'performance';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'perf_reports', 'Performance Reports', 'performance/appraisal_reports.php', id, 90 FROM `nav_modules` WHERE `module_key` = 'performance';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'perf_promotions', 'Promotion Recommendations', 'performance/promotion_recommendations.php', id, 100 FROM `nav_modules` WHERE `module_key` = 'performance';

-- Role access -- the parent shows to anyone who has access to at least one
-- child (every role has the self-appraisal/history child, so it's granted
-- broadly, same pattern as My Portal).
INSERT INTO `nav_module_roles` (`module_id`, `role`)
SELECT id, r.role FROM `nav_modules`
CROSS JOIN (SELECT 'admin' AS role UNION SELECT 'superadmin' UNION SELECT 'manager' UNION SELECT 'supervisor' UNION SELECT 'hr' UNION SELECT 'gm' UNION SELECT 'staff') r
WHERE `module_key` IN ('performance', 'perf_my_appraisal', 'perf_my_history');

INSERT INTO `nav_module_roles` (`module_id`, `role`)
SELECT id, r.role FROM `nav_modules`
CROSS JOIN (SELECT 'admin' AS role UNION SELECT 'superadmin' UNION SELECT 'hr') r
WHERE `module_key` IN ('perf_periods', 'perf_kpi_setup', 'perf_create');

INSERT INTO `nav_module_roles` (`module_id`, `role`)
SELECT id, r.role FROM `nav_modules`
CROSS JOIN (SELECT 'admin' AS role UNION SELECT 'superadmin' UNION SELECT 'hr' UNION SELECT 'gm') r
WHERE `module_key` IN ('perf_dashboard', 'perf_reports', 'perf_promotions');

INSERT INTO `nav_module_roles` (`module_id`, `role`)
SELECT id, r.role FROM `nav_modules`
CROSS JOIN (SELECT 'admin' AS role UNION SELECT 'superadmin' UNION SELECT 'manager') r
WHERE `module_key` = 'perf_manager_review';

INSERT INTO `nav_module_roles` (`module_id`, `role`)
SELECT id, r.role FROM `nav_modules`
CROSS JOIN (SELECT 'admin' AS role UNION SELECT 'superadmin' UNION SELECT 'hr') r
WHERE `module_key` = 'perf_hr_review';

-- Icon for the Performance parent menu item (a simple bar-chart/trend glyph)
UPDATE `nav_modules` SET `icon_svg` = 'M3 13.125C3 12.504 3.504 12 4.125 12h2.25c.621 0 1.125.504 1.125 1.125v6.75C7.5 20.496 6.996 21 6.375 21h-2.25A1.125 1.125 0 013 19.875v-6.75zM9.75 8.625c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125v11.25c0 .621-.504 1.125-1.125 1.125h-2.25a1.125 1.125 0 01-1.125-1.125V8.625zM16.5 4.125c0-.621.504-1.125 1.125-1.125h2.25C20.496 3 21 3.504 21 4.125v15.75c0 .621-.504 1.125-1.125 1.125h-2.25a1.125 1.125 0 01-1.125-1.125V4.125z' WHERE `module_key` = 'performance';

-- Disciplinary module
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `section_label`, `display_order`)
SELECT 'disciplinary', 'Disciplinary', NULL, NULL, 'Employee Hub', 30;

INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'disc_dashboard', 'Dashboard', 'disciplinary/disciplinary_dashboard.php', id, 10 FROM `nav_modules` WHERE `module_key` = 'disciplinary';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'disc_cases', 'All Cases', 'disciplinary/disciplinary_cases.php', id, 20 FROM `nav_modules` WHERE `module_key` = 'disciplinary';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'disc_issue', 'Issue New Case', 'disciplinary/issue_query.php', id, 30 FROM `nav_modules` WHERE `module_key` = 'disciplinary';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'disc_history', 'Case History', 'disciplinary/disciplinary_history.php', id, 40 FROM `nav_modules` WHERE `module_key` = 'disciplinary';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'disc_report', 'Printable Report', 'disciplinary/disciplinary_report.php', id, 50 FROM `nav_modules` WHERE `module_key` = 'disciplinary';
INSERT INTO `nav_modules` (`module_key`, `label`, `url`, `parent_id`, `display_order`)
SELECT 'disc_my_records', 'My Records', 'disciplinary/my_disciplinary_records.php', id, 60 FROM `nav_modules` WHERE `module_key` = 'disciplinary';

-- Management-facing pages: hr/admin/superadmin/manager (matches each
-- page's own access gate)
INSERT INTO `nav_module_roles` (`module_id`, `role`)
SELECT id, r.role FROM `nav_modules`
CROSS JOIN (SELECT 'admin' AS role UNION SELECT 'superadmin' UNION SELECT 'hr' UNION SELECT 'manager') r
WHERE `module_key` IN ('disciplinary', 'disc_dashboard', 'disc_cases', 'disc_issue', 'disc_history', 'disc_report');

-- My Records is self-service -- every role can see their own record
INSERT INTO `nav_module_roles` (`module_id`, `role`)
SELECT id, r.role FROM `nav_modules`
CROSS JOIN (SELECT 'admin' AS role UNION SELECT 'superadmin' UNION SELECT 'manager' UNION SELECT 'supervisor' UNION SELECT 'hr' UNION SELECT 'gm' UNION SELECT 'staff') r
WHERE `module_key` = 'disc_my_records';

-- Icon for the Disciplinary parent menu item (a scale-of-justice glyph)
UPDATE `nav_modules` SET `icon_svg` = 'M12 3v17.25m0-17.25c-1.472 0-2.882.265-4.185.75M12 3c1.472 0 2.882.265 4.185.75M18.75 8.25l-1.591 4.5A2.25 2.25 0 0115.056 14.25h-.112a2.25 2.25 0 01-2.103-1.5L11.25 8.25m7.5 0h-7.5m7.5 0h1.5m-9 0h-1.5m1.5 0l-1.591 4.5a2.25 2.25 0 01-2.103 1.5h-.112a2.25 2.25 0 01-2.103-1.5L3.75 8.25m0 0h1.5m15 12h-15' WHERE `module_key` = 'disciplinary';
