-- Shared DB SAFE indexes only
-- No foreign keys. No NOT NULL tightening. No drops.

ALTER TABLE users
  ADD INDEX IF NOT EXISTS idx_users_company_id (company_id),
  ADD INDEX IF NOT EXISTS idx_users_company_staff_code (company_id, staff_code),
  ADD INDEX IF NOT EXISTS idx_users_company_active (company_id, is_active);

ALTER TABLE work_sessions
  ADD INDEX IF NOT EXISTS idx_ws_company_date (company_id, session_date),
  ADD INDEX IF NOT EXISTS idx_ws_company_user_date (company_id, user_id, session_date),
  ADD INDEX IF NOT EXISTS idx_ws_company_status (company_id, session_status);

ALTER TABLE work_session_events
  ADD INDEX IF NOT EXISTS idx_wse_company_session (company_id, work_session_id),
  ADD INDEX IF NOT EXISTS idx_wse_company_user_time (company_id, user_id, event_time);

ALTER TABLE api_sessions
  ADD INDEX IF NOT EXISTS idx_api_sessions_user_company (user_id, company_id),
  ADD INDEX IF NOT EXISTS idx_api_sessions_company (company_id);

ALTER TABLE worksites
  ADD INDEX IF NOT EXISTS idx_worksites_company (company_id);

ALTER TABLE user_worksites
  ADD INDEX IF NOT EXISTS idx_user_worksites_company_user (company_id, user_id),
  ADD INDEX IF NOT EXISTS idx_user_worksites_company_worksite (company_id, worksite_id);

ALTER TABLE location_logs
  ADD INDEX IF NOT EXISTS idx_location_logs_company_user_time (company_id, user_id, created_at);

ALTER TABLE location_stops
  ADD INDEX IF NOT EXISTS idx_location_stops_company_user_time (company_id, user_id, start_time);

ALTER TABLE admin_audit_log
  ADD INDEX IF NOT EXISTS idx_admin_audit_company_user_time (company_id, admin_user_id, created_at);
