-- Default organizations (skip if already exist)
INSERT INTO orgs (org_id, name, dashboard_name, product_name, custom_domain) VALUES
    ('0194a8b2-1234-7123-8123-123456789abc'::uuid, 'Cybex PTE LTD', 'Reserve Bank of Fiji Procurement Demo', 'Reserve Bank of Fiji Procurement Demo', 'rbf.cybexpte.com'),
    ('0194a8b2-5678-7123-8123-123456789abd'::uuid, 'Karunaratne Lawyers', 'Karunaratne Lawyers', 'Karunaratne Lawyers', 'mycase.cybexpte.com'),
    ('0194a8b2-9abc-7123-8123-123456789abe'::uuid, 'WAF', 'Water Authority of Fiji OHS Demo', 'Water Authority of Fiji OHS Demo', 'waf.cybexpte.com'),
    ('0194a8b2-def0-7123-8123-123456789abf'::uuid, 'Ministry of Employment', 'Ministry of Employment WORKCOM Demo', 'Ministry of Employment WORKCOM Demo', 'employment.cybexpte.com')
ON CONFLICT (org_id) DO NOTHING;

-- Default modules
INSERT INTO modules (module_id, name, description) VALUES
    ('0194a8b2-7777-7123-8123-123456789ac6'::uuid, 'Employees', 'Employee management module'),
    ('0194a8b2-8888-7123-8123-123456789ac7'::uuid, 'Cases', 'Case management module'),
    ('0194a8b2-9999-7123-8123-123456789ac8'::uuid, 'Time Entries & Expenses', 'Time tracking and expense management'),
    ('0194a8b2-aaaa-7123-8123-123456789ac9'::uuid, 'Documents', 'Document management module'),
    ('0194a8b2-bbbb-7123-8123-123456789aca'::uuid, 'Calendar', 'Calendar and scheduling module'),
    ('0194a8b2-cccc-7123-8123-123456789acb'::uuid, 'UserAccessManager', 'User and group access management module'),
    ('0194a8b2-7123-7123-8123-123456789abc'::uuid, 'Time Clocks', 'Time clock management module'),
    ('0194a8b2-7124-7123-8123-123456789abc'::uuid, 'Time Clock Aggregates', 'Time clock aggregates module'),
    ('01940882-1001-7123-8123-123456789001'::uuid, 'Procurement Pulse Board', 'Procurement analytics pulse dashboard module'),
    ('01940882-1002-7123-8123-123456789002'::uuid, 'Supplier Risk Radar', 'Supplier risk analytics dashboard module'),
    ('01940882-1003-7123-8123-123456789003'::uuid, 'Cost Optimization Lab', 'Procurement cost optimisation analytics dashboard'),
    ('01940882-1004-7123-8123-123456789004'::uuid, 'Spend Intelligence Observatory', 'Procurement spend intelligence dashboard'),
    ('01940882-1005-7123-8123-123456789005'::uuid, 'Procurement KPI Cockpit', 'Procurement KPI cockpit dashboard')
ON CONFLICT (module_id) DO NOTHING;

-- Create HR and Employee groups for Cybex PTE LTD (before they are referenced)
INSERT INTO groups (group_id, name, org_id, description)
VALUES
  ('0194a8ba-0000-7123-8123-123456789ad1'::uuid, 'HR', '0194a8b2-1234-7123-8123-123456789abc'::uuid, 'Human Resources Team'),
  ('0194a8ba-0000-7123-8123-123456789ad2'::uuid, 'Employee', '0194a8b2-1234-7123-8123-123456789abc'::uuid, 'General Employees')
ON CONFLICT (group_id) DO NOTHING;


-- Admin user for Cybex PTE LTD
WITH
  super_admin AS (SELECT '0194a8b2-1111-7123-8123-123456789ac0'::uuid AS user_id),
  ins_users AS (
    INSERT INTO users (user_id, full_name, is_superadmin) VALUES
      ((SELECT user_id FROM super_admin), 'Admin', FALSE)
  ),
  ins_passwords AS (
    INSERT INTO user_passwords (user_id, password_hash, algo)
    SELECT user_id, '$2y$10$PXEdpEbxI5c11/tC3UdC6OrZr7V2MsHAatf5C6LRzRV/goxQDPTl.', 'bcrypt' FROM super_admin
  ),
  ins_emails AS (
    INSERT INTO user_emails (user_id, email)
    SELECT user_id, 'admin@cybexpte.com' FROM super_admin
  ),
  ins_memberships AS (
    INSERT INTO user_org_memberships (user_id, org_id, role)
    SELECT user_id, '0194a8b2-1234-7123-8123-123456789abc'::uuid, 'Admin' FROM super_admin
  )
SELECT 1;

-- Bhaskaran Chetty for Cybex PTE LTD
WITH
  bhaskaran_user AS (SELECT '0194a8b2-4451-7123-8123-123456789ac3'::uuid AS user_id),
  ins_users AS (
    INSERT INTO users (user_id, full_name, is_superadmin) VALUES
      ((SELECT user_id FROM bhaskaran_user), 'Bhaskaran Chetty', FALSE)
  ),
  ins_passwords AS (
    INSERT INTO user_passwords (user_id, password_hash, algo)
    SELECT user_id, '$2y$10$PXEdpEbxI5c11/tC3UdC6OrZr7V2MsHAatf5C6LRzRV/goxQDPTl.', 'bcrypt' FROM bhaskaran_user
  ),
  ins_emails AS (
    INSERT INTO user_emails (user_id, email)
    SELECT user_id, 'bhaskaran@cybexpte.com' FROM bhaskaran_user
  ),
  ins_memberships AS (
    INSERT INTO user_org_memberships (user_id, org_id, role)
    SELECT user_id, '0194a8b2-1234-7123-8123-123456789abc'::uuid, 'Member' FROM bhaskaran_user
  )
SELECT 1;

-- Add admin to Dev Team group (Cybex)
INSERT INTO user_group_memberships (user_id, group_id)
SELECT ue.user_id, g.group_id
FROM user_emails ue
JOIN groups g ON g.name = 'Dev Team' AND g.org_id = '0194a8b2-1234-7123-8123-123456789abc'::uuid
WHERE ue.email = 'admin@cybexpte.com'
ON CONFLICT DO NOTHING;

-- Create 4 users: 1 for HR group (Bhaskaran already created) and 3 for Employee group
WITH
  emp_user1 AS (SELECT '0194a8b2-5003-7123-8123-123456789ac3'::uuid AS user_id),
  emp_user2 AS (SELECT '0194a8b2-5004-7123-8123-123456789ac3'::uuid AS user_id),
  emp_user3 AS (SELECT '0194a8b2-5005-7123-8123-123456789ac3'::uuid AS user_id),
  hr_user2 AS (SELECT '0194a8b2-5002-7123-8123-123456789ac3'::uuid AS user_id),
  ins_users AS (
    INSERT INTO users (user_id, full_name, is_superadmin) VALUES
      ((SELECT user_id FROM hr_user2), 'Michael Chen', FALSE),
      ((SELECT user_id FROM emp_user1), 'Kevin Chand', FALSE),
      ((SELECT user_id FROM emp_user2), 'David Thompson', FALSE),
      ((SELECT user_id FROM emp_user3), 'Lisa Anderson', FALSE)
    ON CONFLICT DO NOTHING
  ),
  ins_passwords AS (
    INSERT INTO user_passwords (user_id, password_hash, algo)
    SELECT user_id, '$2y$10$PXEdpEbxI5c11/tC3UdC6OrZr7V2MsHAatf5C6LRzRV/goxQDPTl.', 'bcrypt' FROM hr_user2
    UNION ALL SELECT user_id, '$2y$10$PXEdpEbxI5c11/tC3UdC6OrZr7V2MsHAatf5C6LRzRV/goxQDPTl.', 'bcrypt' FROM emp_user1
    UNION ALL SELECT user_id, '$2y$10$PXEdpEbxI5c11/tC3UdC6OrZr7V2MsHAatf5C6LRzRV/goxQDPTl.', 'bcrypt' FROM emp_user2
    UNION ALL SELECT user_id, '$2y$10$PXEdpEbxI5c11/tC3UdC6OrZr7V2MsHAatf5C6LRzRV/goxQDPTl.', 'bcrypt' FROM emp_user3
    ON CONFLICT DO NOTHING
  ),
  ins_emails AS (
    INSERT INTO user_emails (user_id, email)
    SELECT user_id, 'michael.chen@cybexpte.com' FROM hr_user2
    UNION ALL SELECT user_id, 'kevin@cybexpte.com' FROM emp_user1
    UNION ALL SELECT user_id, 'david.thompson@cybexpte.com' FROM emp_user2
    UNION ALL SELECT user_id, 'lisa.anderson@cybexpte.com' FROM emp_user3
    ON CONFLICT DO NOTHING
  ),
  ins_memberships AS (
    INSERT INTO user_org_memberships (user_id, org_id, role)
    SELECT user_id, '0194a8b2-1234-7123-8123-123456789abc'::uuid, 'Member' FROM hr_user2
    UNION ALL SELECT user_id, '0194a8b2-1234-7123-8123-123456789abc'::uuid, 'Member' FROM emp_user1
    UNION ALL SELECT user_id, '0194a8b2-1234-7123-8123-123456789abc'::uuid, 'Member' FROM emp_user2
    UNION ALL SELECT user_id, '0194a8b2-1234-7123-8123-123456789abc'::uuid, 'Member' FROM emp_user3
    ON CONFLICT DO NOTHING
  )
SELECT 1;

-- Add HR users to HR group (Bhaskaran and Michael)
INSERT INTO user_group_memberships (user_id, group_id)
SELECT ue.user_id, '0194a8ba-0000-7123-8123-123456789ad1'::uuid
FROM user_emails ue
WHERE ue.email IN ('bhaskaran@cybexpte.com', 'michael.chen@cybexpte.com')
ON CONFLICT DO NOTHING;

-- Add Employee users to Employee group
INSERT INTO user_group_memberships (user_id, group_id)
SELECT ue.user_id, '0194a8ba-0000-7123-8123-123456789ad2'::uuid
FROM user_emails ue
WHERE ue.email IN ('kevin@cybexpte.com', 'david.thompson@cybexpte.com', 'lisa.anderson@cybexpte.com')
ON CONFLICT DO NOTHING;

-- Grant HR group access to HR-specific modules
INSERT INTO group_module_permissions (group_id, module_id, action) VALUES
  ('0194a8ba-0000-7123-8123-123456789ad1'::uuid, '0194a8b2-0120-7123-8123-123456789abc'::uuid, 'allow'),  -- Roster
  ('0194a8ba-0000-7123-8123-123456789ad1'::uuid, '442d22f0-a2c9-4b29-a741-ce90111ccfa4'::uuid, 'allow'),  -- Leave Management
  ('0194a8ba-0000-7123-8123-123456789ad1'::uuid, '0194a8b2-7123-7123-8123-123456789abc'::uuid, 'allow'),  -- Time Clocks
  ('0194a8ba-0000-7123-8123-123456789ad1'::uuid, '018e3f0a-12d3-7c45-b89a-2f3c4e1d5b6c'::uuid, 'allow'),  -- Payroll
  ('0194a8ba-0000-7123-8123-123456789ad1'::uuid, '0194a8b2-cccc-7123-8123-123456789acb'::uuid, 'allow'),  -- UserAccessManager
  ('0194a8ba-0000-7123-8123-123456789ad1'::uuid, '019a8e0a-c0e5-7999-9d58-51fadd75895b'::uuid, 'allow'),  -- BiometricDevices
  ('0194a8ba-0000-7123-8123-123456789ad1'::uuid, '019a8e0a-c0f1-7690-a532-1d8575ae6252'::uuid, 'allow'),  -- BiometricDeviceLogs
  ('0194a8ba-0000-7123-8123-123456789ad1'::uuid, '0194a8b2-8888-7123-8123-123456789ac6'::uuid, 'allow')   -- User Profile
ON CONFLICT DO NOTHING;

-- Grant Employee group access to employee self-service modules
INSERT INTO group_module_permissions (group_id, module_id, action) VALUES
  ('0194a8ba-0000-7123-8123-123456789ad2'::uuid, '442d22f0-a2c9-4b29-a741-ce90111ccfa5'::uuid, 'allow'),  -- My Leave
  ('0194a8ba-0000-7123-8123-123456789ad2'::uuid, '0194a8b2-7123-7123-8123-123456789abc'::uuid, 'allow'),  -- Time Clocks
  ('0194a8ba-0000-7123-8123-123456789ad2'::uuid, '0194a8b2-8888-7123-8123-123456789ac6'::uuid, 'allow')   -- User Profile
ON CONFLICT DO NOTHING;
