-- ============================================================================
-- ZUPCO FleetOS — Seed data
-- Run AFTER schema.sql: mysql -u root -p zupco_fleetos < seed.sql
-- ============================================================================
USE `zupco_fleetos`;

-- ----------------------------------------------------------------------------
-- Roles
-- ----------------------------------------------------------------------------
INSERT INTO `roles` (`name`, `slug`, `description`, `is_system`) VALUES
('Super Admin',        'super_admin',        'Full unrestricted access to every module', 1),
('Administrator',      'admin',              'Administrative access, excludes system-critical settings', 0),
('Dispatcher',         'dispatcher',         'Dispatch board, live map, routes, vehicles', 0),
('HR Manager',         'hr_manager',         'Personnel, scheduling, attendance, payroll', 0),
('Fleet Maintenance Manager', 'fleet_maintenance', 'Maintenance module', 0),
('Finance Manager',    'finance_manager',    'Finance module', 0),
('Procurement Officer','procurement_officer','Procurement module', 0),
('Security Officer',   'security_officer',   'Security dashboard, audit logs, alerts', 0),
('Viewer',             'viewer',             'Read-only access to Overview dashboard', 0);

-- ----------------------------------------------------------------------------
-- Permissions — module.page.action, generated from the FleetOS navigation tree.
-- Each page gets: view, create, edit, delete, export (export omitted for pages
-- that are inherently read-only aggregates, e.g. dashboards).
-- ----------------------------------------------------------------------------

-- helper pattern used repeatedly below:
--   (module, page, action, slug, description)

INSERT INTO `permissions` (`module`,`page`,`action`,`slug`,`description`) VALUES
-- OVERVIEW
('overview','dashboard','view','overview.dashboard.view','View overview dashboard'),

-- DISPATCHER
('dispatcher','dispatch_board','view','dispatcher.dispatch_board.view','View dispatch board'),
('dispatcher','dispatch_board','create','dispatcher.dispatch_board.create','Create schedules/dispatches'),
('dispatcher','dispatch_board','edit','dispatcher.dispatch_board.edit','Edit dispatches'),
('dispatcher','route_assignments','view','dispatcher.route_assignments.view','View route assignments'),
('dispatcher','route_assignments','edit','dispatcher.route_assignments.edit','Edit route assignments'),
('dispatcher','crew_chat','view','dispatcher.crew_chat.view','View crew chat & calls'),
('dispatcher','crew_chat','create','dispatcher.crew_chat.create','Send crew messages'),
('dispatcher','live_map','view','dispatcher.live_map.view','View live map'),
('dispatcher','playback','view','dispatcher.playback.view','View trip playback/history'),
('dispatcher','vehicles','view','dispatcher.vehicles.view','View vehicles'),
('dispatcher','vehicles','create','dispatcher.vehicles.create','Add vehicles'),
('dispatcher','vehicles','edit','dispatcher.vehicles.edit','Edit vehicles'),
('dispatcher','vehicles','delete','dispatcher.vehicles.delete','Delete vehicles'),
('dispatcher','routes','view','dispatcher.routes.view','View routes'),
('dispatcher','routes','create','dispatcher.routes.create','Create routes'),
('dispatcher','routes','edit','dispatcher.routes.edit','Edit routes'),
('dispatcher','routes','delete','dispatcher.routes.delete','Delete routes'),
('dispatcher','route_analytics','view','dispatcher.route_analytics.view','View route analytics'),
('dispatcher','route_adherence','view','dispatcher.route_adherence.view','View route adherence'),
('dispatcher','speed_monitor','view','dispatcher.speed_monitor.view','View speed monitor'),
('dispatcher','ai_passenger_analytics','view','dispatcher.ai_passenger_analytics.view','View AI passenger analytics'),
('dispatcher','depots','view','dispatcher.depots.view','View depots'),
('dispatcher','depots','create','dispatcher.depots.create','Create depots'),
('dispatcher','depots','edit','dispatcher.depots.edit','Edit depots'),
('dispatcher','depots','delete','dispatcher.depots.delete','Delete depots'),

-- PERSONNEL
('personnel','employees','view','personnel.employees.view','View employees'),
('personnel','employees','create','personnel.employees.create','Add employees'),
('personnel','employees','edit','personnel.employees.edit','Edit employees'),
('personnel','employees','delete','personnel.employees.delete','Delete/deactivate employees'),
('personnel','employees','export','personnel.employees.export','Export employee data'),
('personnel','drivers','view','personnel.drivers.view','View drivers'),
('personnel','drivers','create','personnel.drivers.create','Add drivers'),
('personnel','drivers','edit','personnel.drivers.edit','Edit drivers'),
('personnel','drivers','delete','personnel.drivers.delete','Delete drivers'),
('personnel','conductors','view','personnel.conductors.view','View conductors'),
('personnel','conductors','create','personnel.conductors.create','Add conductors'),
('personnel','conductors','edit','personnel.conductors.edit','Edit conductors'),
('personnel','conductors','delete','personnel.conductors.delete','Delete conductors'),
('personnel','scheduling','view','personnel.scheduling.view','View scheduling & rostering'),
('personnel','scheduling','create','personnel.scheduling.create','Assign shifts'),
('personnel','scheduling','edit','personnel.scheduling.edit','Edit shifts'),
('personnel','scheduling','delete','personnel.scheduling.delete','Remove shift assignments'),
('personnel','attendance','view','personnel.attendance.view','View attendance'),
('personnel','attendance','edit','personnel.attendance.edit','Edit attendance records'),
('personnel','hr_payroll','view','personnel.hr_payroll.view','View HR & payroll'),
('personnel','hr_payroll','create','personnel.hr_payroll.create','Process payroll'),
('personnel','hr_payroll','edit','personnel.hr_payroll.edit','Edit payroll records'),
('personnel','hr_payroll','export','personnel.hr_payroll.export','Export payroll reports'),

-- SECURITY
('security','dashboard','view','security.dashboard.view','View security dashboard'),
('security','users','view','security.users.view','View users'),
('security','users','create','security.users.create','Create users'),
('security','users','edit','security.users.edit','Edit users'),
('security','users','delete','security.users.delete','Delete/deactivate users'),
('security','roles','view','security.roles.view','View roles & permissions'),
('security','roles','create','security.roles.create','Create roles'),
('security','roles','edit','security.roles.edit','Edit role permissions'),
('security','roles','delete','security.roles.delete','Delete roles'),
('security','audit_logs','view','security.audit_logs.view','View audit logs'),
('security','audit_logs','export','security.audit_logs.export','Export audit logs'),
('security','alerts','view','security.alerts.view','View security alerts'),
('security','alerts','edit','security.alerts.edit','Acknowledge/resolve alerts'),

-- MAINTENANCE
('maintenance','dashboard','view','maintenance.dashboard.view','View maintenance dashboard'),
('maintenance','work_orders','view','maintenance.work_orders.view','View work orders'),
('maintenance','work_orders','create','maintenance.work_orders.create','Create work orders'),
('maintenance','work_orders','edit','maintenance.work_orders.edit','Edit work orders'),
('maintenance','work_orders','delete','maintenance.work_orders.delete','Delete work orders'),
('maintenance','preventive','view','maintenance.preventive.view','View preventive maintenance'),
('maintenance','preventive','edit','maintenance.preventive.edit','Edit PM schedules'),
('maintenance','workshop','view','maintenance.workshop.view','View workshop jobs'),
('maintenance','workshop','edit','maintenance.workshop.edit','Update workshop jobs'),
('maintenance','parts','view','maintenance.parts.view','View parts inventory'),
('maintenance','parts','create','maintenance.parts.create','Add parts'),
('maintenance','parts','edit','maintenance.parts.edit','Edit parts'),
('maintenance','reports','view','maintenance.reports.view','View maintenance reports'),
('maintenance','reports','export','maintenance.reports.export','Export maintenance reports'),

-- FINANCE
('finance','revenue','view','finance.revenue.view','View revenue'),
('finance','revenue','create','finance.revenue.create','Record revenue'),
('finance','expenses','view','finance.expenses.view','View expenses'),
('finance','expenses','create','finance.expenses.create','Record expenses'),
('finance','expenses','edit','finance.expenses.edit','Approve/edit expenses'),
('finance','collections','view','finance.collections.view','View collections'),
('finance','collections','create','finance.collections.create','Record collections'),
('finance','reconciliation','view','finance.reconciliation.view','View reconciliation'),
('finance','reconciliation','edit','finance.reconciliation.edit','Perform reconciliation'),
('finance','reports','view','finance.reports.view','View financial reports'),
('finance','reports','export','finance.reports.export','Export financial reports'),

-- PROCUREMENT
('procurement','purchase_requests','view','procurement.purchase_requests.view','View purchase requests'),
('procurement','purchase_requests','create','procurement.purchase_requests.create','Create purchase requests'),
('procurement','purchase_requests','edit','procurement.purchase_requests.edit','Approve/edit purchase requests'),
('procurement','purchase_orders','view','procurement.purchase_orders.view','View purchase orders'),
('procurement','purchase_orders','create','procurement.purchase_orders.create','Create purchase orders'),
('procurement','purchase_orders','edit','procurement.purchase_orders.edit','Edit purchase orders'),
('procurement','suppliers','view','procurement.suppliers.view','View suppliers'),
('procurement','suppliers','create','procurement.suppliers.create','Add suppliers'),
('procurement','suppliers','edit','procurement.suppliers.edit','Edit suppliers'),
('procurement','inventory','view','procurement.inventory.view','View inventory'),
('procurement','inventory','edit','procurement.inventory.edit','Adjust inventory'),

-- PASSENGER
('passenger','dashboard','view','passenger.dashboard.view','View passenger dashboard'),
('passenger','analytics','view','passenger.analytics.view','View passenger analytics'),
('passenger','demand','view','passenger.demand.view','View demand analysis'),
('passenger','feedback','view','passenger.feedback.view','View passenger feedback'),
('passenger','feedback','edit','passenger.feedback.edit','Action passenger feedback'),

-- REPORTS
('reports','reports','view','reports.reports.view','View cross-module reports'),
('reports','reports','export','reports.reports.export','Export reports'),

-- ADMINISTRATION
('administration','general_settings','view','administration.general_settings.view','View general settings'),
('administration','general_settings','edit','administration.general_settings.edit','Edit general settings'),
('administration','company_profile','view','administration.company_profile.view','View company profile'),
('administration','company_profile','edit','administration.company_profile.edit','Edit company profile'),
('administration','notifications','view','administration.notifications.view','View notification settings'),
('administration','notifications','edit','administration.notifications.edit','Edit notification settings'),
('administration','integrations','view','administration.integrations.view','View integrations (Traccar, SMS, etc.)'),
('administration','integrations','edit','administration.integrations.edit','Edit integration credentials'),
('administration','system_logs','view','administration.system_logs.view','View system/error logs'),
('administration','backup','view','administration.backup.view','View backups'),
('administration','backup','create','administration.backup.create','Trigger backups'),

-- AI TOOLS (bonus module, present in reference mockups)
('ai_tools','assistant','view','ai_tools.assistant.view','Use AI assistant');

-- ----------------------------------------------------------------------------
-- Grant Super Admin ALL permissions
-- ----------------------------------------------------------------------------
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT (SELECT `id` FROM `roles` WHERE `slug` = 'super_admin'), `id` FROM `permissions`;

-- Administrator: everything except administration.integrations.edit and backup
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT (SELECT `id` FROM `roles` WHERE `slug` = 'admin'), `id` FROM `permissions`
WHERE `slug` NOT IN ('administration.integrations.edit','administration.backup.create');

-- Dispatcher
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT (SELECT `id` FROM `roles` WHERE `slug` = 'dispatcher'), `id` FROM `permissions`
WHERE `module` IN ('overview','dispatcher') OR `slug` = 'reports.reports.view';

-- HR Manager
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT (SELECT `id` FROM `roles` WHERE `slug` = 'hr_manager'), `id` FROM `permissions`
WHERE `module` IN ('overview','personnel') OR `slug` = 'reports.reports.view';

-- Fleet Maintenance Manager
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT (SELECT `id` FROM `roles` WHERE `slug` = 'fleet_maintenance'), `id` FROM `permissions`
WHERE `module` IN ('overview','maintenance') OR `slug` IN ('dispatcher.vehicles.view','reports.reports.view');

-- Finance Manager
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT (SELECT `id` FROM `roles` WHERE `slug` = 'finance_manager'), `id` FROM `permissions`
WHERE `module` IN ('overview','finance') OR `slug` = 'reports.reports.view';

-- Procurement Officer
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT (SELECT `id` FROM `roles` WHERE `slug` = 'procurement_officer'), `id` FROM `permissions`
WHERE `module` IN ('overview','procurement') OR `slug` = 'reports.reports.view';

-- Security Officer
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT (SELECT `id` FROM `roles` WHERE `slug` = 'security_officer'), `id` FROM `permissions`
WHERE `module` IN ('overview','security');

-- Viewer
INSERT INTO `role_permissions` (`role_id`, `permission_id`)
SELECT (SELECT `id` FROM `roles` WHERE `slug` = 'viewer'), `id` FROM `permissions`
WHERE `slug` = 'overview.dashboard.view';

-- ----------------------------------------------------------------------------
-- Default depot, shifts, super admin user
-- Default password for admin = "Admin@12345" (CHANGE IMMEDIATELY AFTER SETUP)
-- Hash below generated with PHP: password_hash('Admin@12345', PASSWORD_BCRYPT)
-- ----------------------------------------------------------------------------
INSERT INTO `depots` (`name`, `code`, `address`, `status`) VALUES
('Mutare Depot', 'MUT01', 'Mutare CBD, Zimbabwe', 'active');

INSERT INTO `shifts` (`name`, `start_time`, `end_time`) VALUES
('Morning', '05:00:00', '13:00:00'),
('Afternoon', '13:00:00', '21:00:00'),
('Night', '21:00:00', '05:00:00');

INSERT INTO `users` (`username`, `email`, `password_hash`, `role_id`, `must_change_password`, `status`) VALUES
('admin', 'admin@zupco.co.zw', '$2y$10$4LdjcG89iQwclpmx0e31eO01uvSDWp7YEbW0N/Twvp.JeSYoRL7WS', (SELECT `id` FROM `roles` WHERE `slug` = 'super_admin'), 1, 'active');

INSERT INTO `system_settings` (`setting_key`, `setting_value`, `type`) VALUES
('company_name', 'Zimbabwe United Passenger Company', 'string'),
('app_name', 'ZUPCO FleetOS', 'string'),
('default_currency', 'USD', 'string'),
('session_timeout_minutes', '30', 'integer'),
('max_login_attempts', '5', 'integer'),
('lockout_minutes', '15', 'integer');
