-- Default organizations
INSERT INTO orgs (org_id, name, dashboard_name, product_name, custom_domain) VALUES
    ('0194a8b2-1234-7123-8123-123456789abc'::uuid, 'Cybex PTE LTD', 'Cybex ERP', 'Cybex ERP', NULL),
    ('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');

-- 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')
ON CONFLICT (module_id) DO NOTHING;

-- Default program
INSERT INTO programs (program_id, name) VALUES
    ('0194a8b2-5555-7123-8123-123456789ac4'::uuid, 'Case Management'),
    ('0194a8b2-dddd-7123-8123-123456789acd'::uuid, 'Dev Program');

-- Program modules
INSERT INTO program_modules (program_id, module_id) VALUES
    ('0194a8b2-5555-7123-8123-123456789ac4'::uuid, '0194a8b2-8888-7123-8123-123456789ac7'::uuid),
    ('0194a8b2-5555-7123-8123-123456789ac4'::uuid, '0194a8b2-aaaa-7123-8123-123456789ac9'::uuid),
    ('0194a8b2-5555-7123-8123-123456789ac4'::uuid, '0194a8b2-bbbb-7123-8123-123456789aca'::uuid),
    ('0194a8b2-dddd-7123-8123-123456789acd'::uuid, '0194a8b2-cccc-7123-8123-123456789acb'::uuid)
ON CONFLICT DO NOTHING;

-- Program enrollments
INSERT INTO program_enrollments (program_id, org_id) VALUES
    ('0194a8b2-5555-7123-8123-123456789ac4'::uuid, '0194a8b2-1234-7123-8123-123456789abc'::uuid),  -- Cybex PTE LTD - Case Management
    ('0194a8b2-5555-7123-8123-123456789ac4'::uuid, '0194a8b2-5678-7123-8123-123456789abd'::uuid),  -- Karunaratne Lawyers - Case Management
    ('0194a8b2-dddd-7123-8123-123456789acd'::uuid, '0194a8b2-1234-7123-8123-123456789abc'::uuid)   -- Cybex PTE LTD - Dev Program
ON CONFLICT DO NOTHING;

-- Create Managers group for Karunaratne Lawyers in Case Management
INSERT INTO groups (group_id, name, description, org_id) VALUES
    ('0194a8b2-aaaa-7123-8123-123456789ac4'::uuid, 'Managers', 'Case Management Managers Group', '0194a8b2-5678-7123-8123-123456789abd'::uuid)
ON CONFLICT (group_id) DO NOTHING;

-- Create Dev group for Cybex PTE LTD in Dev Program
INSERT INTO groups (group_id, name, description, org_id) VALUES
    ('0194a8b2-eeee-7123-8123-123456789ace'::uuid, 'Dev Team', 'Development Team Group', '0194a8b2-1234-7123-8123-123456789abc'::uuid)
ON CONFLICT (group_id) DO NOTHING;

-- Assign Employees module to Managers group
INSERT INTO group_module_permissions (group_id, module_id, action) VALUES
    ('0194a8b2-aaaa-7123-8123-123456789ac4'::uuid, '0194a8b2-7777-7123-8123-123456789ac6'::uuid, 'allow')
ON CONFLICT DO NOTHING;

-- Assign Time Entries & Expenses module to Managers group
INSERT INTO group_module_permissions (group_id, module_id, action) VALUES
    ('0194a8b2-aaaa-7123-8123-123456789ac4'::uuid, '0194a8b2-9999-7123-8123-123456789ac8'::uuid, 'allow')
ON CONFLICT DO NOTHING;

-- Additional Karunaratne Lawyers users
WITH
  super_admin AS (SELECT '0194a8b2-1111-7123-8123-123456789ac0'::uuid AS user_id),
  employment_admin AS (SELECT '0194a8b2-2222-7123-8123-123456789ac1'::uuid AS user_id),
  waf_admin AS (SELECT '0194a8b2-3333-7123-8123-123456789ac2'::uuid AS user_id),
  jagath_user AS (SELECT '0194a8b2-4444-7123-8123-123456789ac3'::uuid AS user_id),
  roma_user AS (SELECT '0194a8b2-4445-7123-8123-123456789ac3'::uuid AS user_id),
  mereani_user AS (SELECT '0194a8b2-4446-7123-8123-123456789ac3'::uuid AS user_id),
  admin_user AS (SELECT '0194a8b2-4447-7123-8123-123456789ac3'::uuid AS user_id),
  victor_user AS (SELECT '0194a8b2-4448-7123-8123-123456789ac3'::uuid AS user_id),
  shiral_user AS (SELECT '0194a8b2-4449-7123-8123-123456789ac3'::uuid AS user_id),
  ins_users AS (
    INSERT INTO users (user_id, full_name, is_superadmin) VALUES
      ((SELECT user_id FROM super_admin), 'Bhaskaran Chetty', TRUE),
      ((SELECT user_id FROM employment_admin), 'EmploymentAdmin', FALSE),
      ((SELECT user_id FROM waf_admin), 'WAFAdmin', FALSE),
      ((SELECT user_id FROM jagath_user), 'Jagath', FALSE),
      ((SELECT user_id FROM roma_user), 'Roma S', FALSE),
      ((SELECT user_id FROM mereani_user), 'Mereani', FALSE),
      ((SELECT user_id FROM admin_user), 'Akansha', FALSE),
      ((SELECT user_id FROM victor_user), 'Victor Adrian', FALSE),
      ((SELECT user_id FROM shiral_user), 'Shiral Kumar', 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
    UNION ALL SELECT user_id, '$2y$10$PXEdpEbxI5c11/tC3UdC6OrZr7V2MsHAatf5C6LRzRV/goxQDPTl.', 'bcrypt' FROM employment_admin
    UNION ALL SELECT user_id, '$2y$10$PXEdpEbxI5c11/tC3UdC6OrZr7V2MsHAatf5C6LRzRV/goxQDPTl.', 'bcrypt' FROM waf_admin
    UNION ALL SELECT user_id, '$2y$10$PXEdpEbxI5c11/tC3UdC6OrZr7V2MsHAatf5C6LRzRV/goxQDPTl.', 'bcrypt' FROM jagath_user
    UNION ALL SELECT user_id, '$2y$10$PXEdpEbxI5c11/tC3UdC6OrZr7V2MsHAatf5C6LRzRV/goxQDPTl.', 'bcrypt' FROM roma_user
    UNION ALL SELECT user_id, '$2y$10$PXEdpEbxI5c11/tC3UdC6OrZr7V2MsHAatf5C6LRzRV/goxQDPTl.', 'bcrypt' FROM mereani_user
    UNION ALL SELECT user_id, '$2y$10$PXEdpEbxI5c11/tC3UdC6OrZr7V2MsHAatf5C6LRzRV/goxQDPTl.', 'bcrypt' FROM admin_user
    UNION ALL SELECT user_id, '$2y$10$PXEdpEbxI5c11/tC3UdC6OrZr7V2MsHAatf5C6LRzRV/goxQDPTl.', 'bcrypt' FROM victor_user
    UNION ALL SELECT user_id, '$2y$10$PXEdpEbxI5c11/tC3UdC6OrZr7V2MsHAatf5C6LRzRV/goxQDPTl.', 'bcrypt' FROM shiral_user
  ),
  ins_emails AS (
    INSERT INTO user_emails (user_id, email)
    SELECT user_id, 'admin@cybexpte.com' FROM super_admin
    UNION ALL SELECT user_id, 'employment@cybexpte.com' FROM employment_admin
    UNION ALL SELECT user_id, 'waf@cybexpte.com' FROM waf_admin
    UNION ALL SELECT user_id, 'jagath@karunaratnelawyers.com.fj' FROM jagath_user
    UNION ALL SELECT user_id, 'Roma@karunaratnelawyers.com.fj' FROM roma_user
    UNION ALL SELECT user_id, 'mereani@karunaratnelawyers.com.fj' FROM mereani_user
    UNION ALL SELECT user_id, 'admin@karunaratnelawyers.com.fj' FROM admin_user
    UNION ALL SELECT user_id, 'victorpatrick.adrian@gmail.com' FROM victor_user
    UNION ALL SELECT user_id, 'shiralkumar05@gmail.com' FROM shiral_user
  ),
  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
    UNION ALL SELECT user_id, '0194a8b2-def0-7123-8123-123456789abf'::uuid, 'Admin' FROM employment_admin
    UNION ALL SELECT user_id, '0194a8b2-9abc-7123-8123-123456789abe'::uuid, 'Admin' FROM waf_admin
    UNION ALL SELECT user_id, '0194a8b2-5678-7123-8123-123456789abd'::uuid, 'Admin' FROM jagath_user
    UNION ALL SELECT user_id, '0194a8b2-5678-7123-8123-123456789abd'::uuid, 'Member' FROM roma_user
    UNION ALL SELECT user_id, '0194a8b2-5678-7123-8123-123456789abd'::uuid, 'Member' FROM mereani_user
    UNION ALL SELECT user_id, '0194a8b2-5678-7123-8123-123456789abd'::uuid, 'Admin' FROM admin_user
    UNION ALL SELECT user_id, '0194a8b2-5678-7123-8123-123456789abd'::uuid, 'Member' FROM victor_user
    UNION ALL SELECT user_id, '0194a8b2-5678-7123-8123-123456789abd'::uuid, 'Member' FROM shiral_user
  )
SELECT 1;

-- Add jagath to Managers group (after users are created)
INSERT INTO user_group_memberships (user_id, group_id) VALUES
    ('0194a8b2-4444-7123-8123-123456789ac3'::uuid, '0194a8b2-aaaa-7123-8123-123456789ac4'::uuid)
ON CONFLICT DO NOTHING;

-- Add victor to Managers group (after users are created)
INSERT INTO user_group_memberships (user_id, group_id) VALUES
    ('0194a8b2-4448-7123-8123-123456789ac3'::uuid, '0194a8b2-aaaa-7123-8123-123456789ac4'::uuid)
ON CONFLICT DO NOTHING;

-- Default groups for Case Management program
INSERT INTO program_default_groups (program_id, group_name, group_description, is_auto_create) VALUES
    ('0194a8b2-5555-7123-8123-123456789ac4'::uuid, 'Managers', 'Case Management Managers with full access to employee and time tracking modules', true)
ON CONFLICT (program_id, group_name) DO NOTHING;

-- Default permissions for Managers group
INSERT INTO program_default_group_permissions (program_default_group_id, module_id, permission_type, action) VALUES
    ((SELECT program_default_group_id FROM program_default_groups WHERE program_id = '0194a8b2-5555-7123-8123-123456789ac4'::uuid AND group_name = 'Managers' LIMIT 1),
     '0194a8b2-7777-7123-8123-123456789ac6'::uuid, 'module', 'allow') -- Employees module
ON CONFLICT DO NOTHING;

INSERT INTO program_default_group_permissions (program_default_group_id, module_id, permission_type, action) VALUES
    ((SELECT program_default_group_id FROM program_default_groups WHERE program_id = '0194a8b2-5555-7123-8123-123456789ac4'::uuid AND group_name = 'Managers' LIMIT 1),
     '0194a8b2-9999-7123-8123-123456789ac8'::uuid, 'module', 'allow') -- Time Entries & Expenses module
ON CONFLICT DO NOTHING;

INSERT INTO program_default_group_permissions (program_default_group_id, module_id, permission_type, action) VALUES
    ((SELECT program_default_group_id FROM program_default_groups WHERE program_id = '0194a8b2-5555-7123-8123-123456789ac4'::uuid AND group_name = 'Managers' LIMIT 1),
     '0194a8b2-cccc-7123-8123-123456789acb'::uuid, 'module', 'allow') -- UserAccessManager module
ON CONFLICT DO NOTHING;





-- Seed demo locations
INSERT INTO locations (name, latitude, longitude, radius_m, tenant_id, branch_id) VALUES
  ('Suva Headquarters', -18.1397956843015, 178.42656254768372, 50, 1, 1),
  ('Nadi Office', -17.7533, 177.4435, 50, 1, 1),
  ('Lautoka Office', -17.6167, 177.4667, 50, 1, 1);

-- Ensure the sequence for locations continues from the inserted values
SELECT setval('locations_id_seq', (SELECT MAX(id) FROM locations));

-- Dummy vendor data for Inventory Management
INSERT INTO vendors (vendor_id, vendorName) VALUES
  (uuid_generate_v7(), 'Acme Corp'),
  (uuid_generate_v7(), 'Global Supplies'),
  (uuid_generate_v7(), 'Demo Vendor')
ON CONFLICT (vendor_id) DO NOTHING;

-- Create dev program
INSERT INTO programs (program_id, name, owner_root_org_id, is_private, owner_access) VALUES
    ('0194a8b2-def0-7123-8123-123456789abc'::uuid, 'dev', '0194a8b2-1234-7123-8123-123456789abc'::uuid, true, 'none')
ON CONFLICT (program_id) DO NOTHING;

-- Add Products module to dev program
INSERT INTO program_modules (program_id, module_id, config_json) VALUES
    ('0194a8b2-def0-7123-8123-123456789abc'::uuid, (SELECT module_id FROM modules WHERE name = 'Products' LIMIT 1), NULL)
ON CONFLICT DO NOTHING;

-- Add Restaurant Products module to dev program
INSERT INTO program_modules (program_id, module_id, config_json) VALUES
    ('0194a8b2-def0-7123-8123-123456789abc'::uuid, (SELECT module_id FROM modules WHERE name = 'Restaurant Products' LIMIT 1), NULL)
ON CONFLICT DO NOTHING;

-- Add POS module to dev program
INSERT INTO program_modules (program_id, module_id, config_json) VALUES
    ('0194a8b2-def0-7123-8123-123456789abc'::uuid, '0194a8b2-0015-7123-8123-123456789abc'::uuid, NULL)
ON CONFLICT DO NOTHING;

-- Add InventoryManagement module to dev program
INSERT INTO program_modules (program_id, module_id, config_json) VALUES
    ('0194a8b2-def0-7123-8123-123456789abc'::uuid, '0194a8b2-0016-7123-8123-123456789abc'::uuid, NULL)
ON CONFLICT DO NOTHING;

-- Add Vendor Management module to dev program
-- First, create the Vendor Management module if it does not exist
INSERT INTO modules (module_id, name, description) VALUES
    ('0194a8b2-0017-7123-8123-123456789abc'::uuid, 'Vendor Management', 'Vendor management module')
ON CONFLICT (module_id) DO NOTHING;

INSERT INTO program_modules (program_id, module_id, config_json) VALUES
    ('0194a8b2-def0-7123-8123-123456789abc'::uuid, '0194a8b2-0017-7123-8123-123456789abc'::uuid, NULL)
ON CONFLICT DO NOTHING;

-- Enroll Cybex PTE LTD in dev program
INSERT INTO program_enrollments (program_id, org_id, status) VALUES
    ('0194a8b2-def0-7123-8123-123456789abc'::uuid, '0194a8b2-1234-7123-8123-123456789abc'::uuid, 'active')
ON CONFLICT (program_id, org_id) DO NOTHING;

-- Demo customers for Cybex PTE LTD
INSERT INTO customers (
    customer_type, name, email, phone_number, address, tin, 
    risk_rating, compliance_status, balance, org_id, user_id, 
    isDeleted, updatedAt
) VALUES
    ('individual', 'John Smith', 'john.smith@email.com', '+679 123 4567', '123 Main Street, Suva, Fiji', '123456789', 'low', 'compliant', 0.00, '0194a8b2-1234-7123-8123-123456789abc'::uuid, NULL, FALSE, NOW()),
    ('individual', 'Mary Johnson', 'mary.johnson@email.com', '+679 234 5678', '456 Queen Street, Suva, Fiji', '234567890', 'low', 'compliant', 0.00, '0194a8b2-1234-7123-8123-123456789abc'::uuid, NULL, FALSE, NOW()),
    ('corporate', 'Pacific Traders Ltd', 'info@pacifictraders.com.fj', '+679 345 6789', '789 Kings Road, Suva, Fiji', '345678901', 'medium', 'compliant', 0.00, '0194a8b2-1234-7123-8123-123456789abc'::uuid, NULL, FALSE, NOW()),
    ('corporate', 'Fiji Imports Ltd', 'sales@fijiimports.com.fj', '+679 456 7890', '321 Victoria Parade, Suva, Fiji', '456789012', 'medium', 'compliant', 0.00, '0194a8b2-1234-7123-8123-123456789abc'::uuid, NULL, FALSE, NOW()),
    ('individual', 'Raj Kumar', 'raj.kumar@email.com', '+679 567 8901', '654 Princes Road, Suva, Fiji', '567890123', 'low', 'compliant', 0.00, '0194a8b2-1234-7123-8123-123456789abc'::uuid, NULL, FALSE, NOW()),
    ('government', 'Ministry of Health', 'procurement@health.gov.fj', '+679 678 9012', 'Government Buildings, Suva, Fiji', '678901234', 'low', 'compliant', 0.00, '0194a8b2-1234-7123-8123-123456789abc'::uuid, NULL, FALSE, NOW()),
    ('corporate', 'Suva Supermarket Ltd', 'manager@suvamarket.com.fj', '+679 789 0123', '987 Waimanu Road, Suva, Fiji', '789012345', 'medium', 'compliant', 0.00, '0194a8b2-1234-7123-8123-123456789abc'::uuid, NULL, FALSE, NOW()),
    ('individual', 'Lisa Wong', 'lisa.wong@email.com', '+679 890 1234', '147 Gordon Street, Suva, Fiji', '890123456', 'low', 'compliant', 0.00, '0194a8b2-1234-7123-8123-123456789abc'::uuid, NULL, FALSE, NOW()),
    ('corporate', 'Island Hardware Ltd', 'orders@islandhardware.com.fj', '+679 901 2345', '258 Thomson Street, Suva, Fiji', '901234567', 'medium', 'pending', 0.00, '0194a8b2-1234-7123-8123-123456789abc'::uuid, NULL, FALSE, NOW()),
    ('individual', 'David Prasad', 'david.prasad@email.com', '+679 012 3456', '369 Loftus Street, Suva, Fiji', '012345678', 'low', 'compliant', 0.00, '0194a8b2-1234-7123-8123-123456789abc'::uuid, NULL, FALSE, NOW())
ON CONFLICT DO NOTHING;
