-- ============================================================
-- Demo login per INTERNAL role — for testing role-based portal views.
-- Password for ALL of these:  ChangeMe@2026   (change later in Users admin)
-- Emails follow  <role_code>@medmecs.local   (e.g. supervisor@medmecs.local)
--
-- Run AFTER schema.sql and seed-additional-roles.sql.
-- Idempotent — safe to re-run.
--   phpMyAdmin: select your database, paste this, click Go.
--   CLI:        mysql -u <user> -p <database> < sql/seed-role-logins.sql
-- (sysadmin is intentionally omitted — admin@medmecs.local already exists.)
-- ============================================================

-- bcrypt of ChangeMe@2026 (verified with PHP password_verify)
INSERT INTO `users` (`name`,`email`,`password`,`user_type`,`status`,`must_change_password`,`password_changed_at`,`created_at`,`updated_at`) VALUES
  ('Developer (demo)',         'developer@medmecs.local',         '$2y$10$IzgpzS33QqNFvOCMl5gDxu3QS1yv3kQnJjrc/.1IcOS5DpOt9OFgq','internal','active',0,NOW(),NOW(),NOW()),
  ('Vertical Manager (demo)',  'vertical_manager@medmecs.local',  '$2y$10$IzgpzS33QqNFvOCMl5gDxu3QS1yv3kQnJjrc/.1IcOS5DpOt9OFgq','internal','active',0,NOW(),NOW(),NOW()),
  ('Client Manager (demo)',    'client_manager@medmecs.local',    '$2y$10$IzgpzS33QqNFvOCMl5gDxu3QS1yv3kQnJjrc/.1IcOS5DpOt9OFgq','internal','active',0,NOW(),NOW(),NOW()),
  ('Process Lead (demo)',      'process_lead@medmecs.local',      '$2y$10$IzgpzS33QqNFvOCMl5gDxu3QS1yv3kQnJjrc/.1IcOS5DpOt9OFgq','internal','active',0,NOW(),NOW(),NOW()),
  ('Analyst (demo)',           'analyst@medmecs.local',           '$2y$10$IzgpzS33QqNFvOCMl5gDxu3QS1yv3kQnJjrc/.1IcOS5DpOt9OFgq','internal','active',0,NOW(),NOW(),NOW()),
  ('Auditor (demo)',           'auditor@medmecs.local',           '$2y$10$IzgpzS33QqNFvOCMl5gDxu3QS1yv3kQnJjrc/.1IcOS5DpOt9OFgq','internal','active',0,NOW(),NOW(),NOW()),
  ('Report Controller (demo)', 'report_controller@medmecs.local', '$2y$10$IzgpzS33QqNFvOCMl5gDxu3QS1yv3kQnJjrc/.1IcOS5DpOt9OFgq','internal','active',0,NOW(),NOW(),NOW()),
  ('Manager (demo)',           'manager@medmecs.local',           '$2y$10$IzgpzS33QqNFvOCMl5gDxu3QS1yv3kQnJjrc/.1IcOS5DpOt9OFgq','internal','active',0,NOW(),NOW(),NOW()),
  ('Supervisor (demo)',        'supervisor@medmecs.local',        '$2y$10$IzgpzS33QqNFvOCMl5gDxu3QS1yv3kQnJjrc/.1IcOS5DpOt9OFgq','internal','active',0,NOW(),NOW(),NOW()),
  ('User (demo)',              'user@medmecs.local',              '$2y$10$IzgpzS33QqNFvOCMl5gDxu3QS1yv3kQnJjrc/.1IcOS5DpOt9OFgq','internal','active',0,NOW(),NOW(),NOW())
ON DUPLICATE KEY UPDATE `name`=VALUES(`name`),`password`=VALUES(`password`),`status`='active',`must_change_password`=0,`updated_at`=NOW();

-- Give each demo login EXACTLY its one role (email prefix == role code).
INSERT IGNORE INTO `user_roles` (`user_id`,`role_id`)
  SELECT u.id, r.id
    FROM `users` u
    JOIN `roles` r ON r.code = SUBSTRING_INDEX(u.email, '@', 1)
   WHERE u.email IN (
     'developer@medmecs.local','vertical_manager@medmecs.local','client_manager@medmecs.local',
     'process_lead@medmecs.local','analyst@medmecs.local','auditor@medmecs.local',
     'report_controller@medmecs.local','manager@medmecs.local','supervisor@medmecs.local','user@medmecs.local'
   );
