-- ═══════════════════════════════════════════════════════════════════════════
-- rsflow.dspera · Seed data (MVP-0)
-- Seeds the 10 canonical roles + 7 fin_chain_defs + 27 fin_chain_steps
-- copied verbatim from hqdspera, plus one admin user (username: admin,
-- password: admin — must be changed at first login in production).
-- ═══════════════════════════════════════════════════════════════════════════

-- 10 roles ------------------------------------------------------------------

INSERT INTO user_groups (c0d, user_group_name) VALUES
  (1,  'Executive'),
  (2,  'Finance Manager'),
  (3,  'Accountant'),
  (4,  'Project Manager'),
  (5,  'Projects Coordinator'),
  (6,  'Field Supervisor'),
  (7,  'Procurement'),
  (8,  'Stores'),
  (9,  'CRM'),
  (10, 'Administrator');

-- 7 approval chain definitions (verbatim from hqdspera) ---------------------

INSERT INTO fin_chain_defs (c0d, chain_key, title, description, status) VALUES
  (1, 'HR_PAYROLL_APPROVAL',      'HR Payroll Run Approval',       'Monthly payroll batch — HR prepares & computes; FM reviews totals; Exec signs off before posting to cashbook + statutory accruals.', 'ACTIVE'),
  (2, 'HR_LEAVE_APPROVAL',        'HR Leave Request Approval',     'Employee leave request — HR reviews, deducts balance on approval.', 'ACTIVE'),
  (3, 'HR_TERMINATION_APPROVAL',  'HR Termination & Final-Dues',   'Exit lifecycle — HR computes final dues (leave payout + gratuity + other), FM verifies, Exec authorises payout.', 'ACTIVE'),
  (4, 'GOODS_LPO_APPROVAL',       'Goods LPO Batch Approval',      'Procurement submits the auto-drafted LPO batch (one chain run per PO covers every supplier LPO). PC → PM → FM → Exec sign off; on approval, Procurement can Issue each LPO.', 'ACTIVE'),
  (5, 'GOODS_CUSTOMER_INVOICE',   'Goods Customer Invoice',        'PC → PM → Accountant review of a Procurement-drafted customer invoice. Accountant ack allocates sale_no and posts revenue journal.', 'ACTIVE'),
  (6, 'SERVICE_PM_APPROVAL',      'Service PM Plan Approval',      'PC generates Client POs + ticks items for procurement, then submits. Chain: PM check → Procurement sources → PM margin check → FM → Exec. On terminal approval, draft LPOs auto-approve for Procurement to issue.', 'ACTIVE'),
  (9, 'CRM_PROJECT_ONBOARDING',   'CRM Project Onboarding',        'Gate that ensures every project passes a formal CRM step before Ops can act on it.', 'ACTIVE');

-- 27 approval chain steps --------------------------------------------------

INSERT INTO fin_chain_steps (c0d, chain_id, step_no, step_key, step_label, role_required, is_terminal, description, sort_order) VALUES
  (1,  1, 1, 'HR_REVIEW',   'HR Review',       'HUMAN RESOURCES', 0, 'HR confirms employees, totals look right', 1),
  (2,  1, 2, 'FM_REVIEW',   'FM Review',       'FINANCE MANAGER', 0, 'Finance Manager verifies cost-to-company and bank funds', 2),
  (3,  1, 3, 'EXEC',        'Exec Approval',   'EXECUTIVE',       1, 'Final sign-off before payroll posts', 3),
  (4,  2, 1, 'HR_REVIEW',   'HR Review',       'HUMAN RESOURCES', 1, 'HR reviews, checks balance, approves or declines.', 1),
  (5,  3, 1, 'HR_REVIEW',   'HR Review',       'HUMAN RESOURCES', 0, 'HR confirms dues snapshot is correct', 1),
  (6,  3, 2, 'FM_REVIEW',   'FM Review',       'FINANCE MANAGER', 0, 'Finance Manager verifies amounts and bank funds', 2),
  (7,  3, 3, 'EXEC',        'Exec Approval',   'EXECUTIVE',       1, 'Final authorisation before payout posts', 3),
  (8,  4, 1, 'PC_REVIEW',   'PC Review',       'PROJECTS COORDINATOR', 0, 'Project Coordinator verifies LPO batch matches sourcing awards', 1),
  (9,  4, 2, 'PM_APPROVE',  'PM Approval',     'PROJECT MANAGER',      0, 'PM confirms supplier terms acceptable', 2),
  (10, 4, 3, 'FM_REVIEW',   'FM Review',       'FINANCE MANAGER',      0, 'Finance Manager checks totals against approved budget', 3),
  (11, 4, 4, 'EXEC',        'Exec Decision',   'EXECUTIVE',            1, 'Final sign-off before LPOs can be issued to suppliers', 4),
  (12, 5, 1, 'PC_REVIEW',   'PC Review',       'PROJECTS COORDINATOR', 0, 'PC verifies invoice matches deliveries', 1),
  (13, 5, 2, 'PM_REVIEW',   'PM Review',       'PROJECT MANAGER',      0, 'PM approves invoice + margin', 2),
  (14, 5, 3, 'ACCT_ACK',    'Accountant Ack',  'ACCOUNTANT',           1, 'Accountant allocates sale_no + posts revenue journal', 3),
  (15, 6, 1, 'PM_CHECK',    'PM Check',        'PROJECT MANAGER',      0, 'Project Manager reviews scope, packs and dates', 1),
  (16, 6, 2, 'PROC_SOURCE', 'Procurement Sourcing', 'PROCUREMENT',     0, 'Procurement routes each PR item, creates RFQs, awards suppliers, drafts LPOs', 2),
  (17, 6, 3, 'PM_MARGIN',   'PM Margin Check', 'PROJECT MANAGER',      0, 'PM verifies real supplier cost vs client price; approves if margin is acceptable', 3),
  (18, 6, 4, 'FM_APPROVE',  'FM Approve',      'FINANCE MANAGER',      0, 'Finance approves plan totals and budget alignment', 4),
  (19, 6, 5, 'EXEC_AUTH',   'Exec Authorize',  'EXECUTIVE',            1, 'Executive authorisation — auto-approves draft LPOs so Procurement can issue', 5),
  (20, 9, 1, 'PC_ACCEPT',   'PC Accept',       'PROJECTS COORDINATOR', 0, 'Projects Coordinator reviews contract + checklist + snapshot and accepts.', 1),
  (21, 9, 2, 'EXEC_APPROVE','Executive Approve','EXECUTIVE',           1, 'Executive signs off — onboarding APPROVED; Ops gates open.', 2);

-- Default admin user -------------------------------------------------------
-- Password is 'admin' hashed with PHP password_hash(). Change on first login.

INSERT INTO users (c0d, username, password, full_name, email, user_group_id, user_group_name, status) VALUES
  (1, 'admin', '$2y$10$J1D4FJtpdxZiv5ZsmNvVM.tpsxTm4eXEkqMKMqBByW6A6PCC1EUF.', 'System Administrator', 'admin@rsflow.local', 10, 'Administrator', 'ACTIVE');

-- Seed users for the 9 other roles (all password 'admin' for MVP-0 testing) --

INSERT INTO users (username, password, full_name, user_group_id, user_group_name) VALUES
  ('exec',       '$2y$10$J1D4FJtpdxZiv5ZsmNvVM.tpsxTm4eXEkqMKMqBByW6A6PCC1EUF.', 'Executive Test',           1, 'Executive'),
  ('finance',    '$2y$10$J1D4FJtpdxZiv5ZsmNvVM.tpsxTm4eXEkqMKMqBByW6A6PCC1EUF.', 'Finance Manager Test',     2, 'Finance Manager'),
  ('acct',       '$2y$10$J1D4FJtpdxZiv5ZsmNvVM.tpsxTm4eXEkqMKMqBByW6A6PCC1EUF.', 'Accountant Test',          3, 'Accountant'),
  ('pm',         '$2y$10$J1D4FJtpdxZiv5ZsmNvVM.tpsxTm4eXEkqMKMqBByW6A6PCC1EUF.', 'Project Manager Test',     4, 'Project Manager'),
  ('pc',         '$2y$10$J1D4FJtpdxZiv5ZsmNvVM.tpsxTm4eXEkqMKMqBByW6A6PCC1EUF.', 'Projects Coordinator Test',5, 'Projects Coordinator'),
  ('fs',         '$2y$10$J1D4FJtpdxZiv5ZsmNvVM.tpsxTm4eXEkqMKMqBByW6A6PCC1EUF.', 'Field Supervisor Test',    6, 'Field Supervisor'),
  ('procurement','$2y$10$J1D4FJtpdxZiv5ZsmNvVM.tpsxTm4eXEkqMKMqBByW6A6PCC1EUF.', 'Procurement Test',         7, 'Procurement'),
  ('stores',     '$2y$10$J1D4FJtpdxZiv5ZsmNvVM.tpsxTm4eXEkqMKMqBByW6A6PCC1EUF.', 'Stores Test',              8, 'Stores'),
  ('crm',        '$2y$10$J1D4FJtpdxZiv5ZsmNvVM.tpsxTm4eXEkqMKMqBByW6A6PCC1EUF.', 'CRM Test',                 9, 'CRM');

-- Baseline perms: everyone can hit Dashboard/index --------------------------

INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed)
SELECT c0d, 'Dashboard', 'index' FROM user_groups;

INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed)
SELECT c0d, 'Logout', 'index' FROM user_groups;
