-- 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', 'Agricultural Marketing Authority Demo', 'Agricultural Marketing Authority Demo', 'ama.cybexpte.com'),
    ('0194a8b2-5678-7123-8123-123456789abd'::uuid, 'Karunaratne Lawyers', 'Karunaratne Lawyers', 'Karunaratne Lawyers', 'mycase.cybexpte.com')
    ON CONFLICT (org_id) DO NOTHING;

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

-- Create Dev group for Cybex PTE LTD 
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;

-- Create General Groups to manage access
INSERT INTO groups (group_id, name, description, org_id) VALUES
  ('0194a8ba-d0d0-7123-8123-123456789ad8'::uuid, 'POSManagers', 'Point of Sale managers group', NULL), -- POS Managers
  ('0194a8ba-dede-7123-8123-123456789ad9'::uuid, 'ProcurementManagers', 'Procurement managers group (migrated from program)', NULL), --Procurement Mnaagers
  ('0194a8ba-f1f1-7123-8123-123456789adc'::uuid, 'MiniMart', 'MiniMart group', NULL), -- MiniMart
  ('0194a8ba-fefe-7123-8123-123456789add'::uuid, 'CaseManagement', 'Case managers group', NULL)  -- Inventory Managers
ON CONFLICT (group_id) DO NOTHING;

-- Grant MiniMart group access to POS, Products, Inventory, Quotations, Invoices, Auto Orders, and Customer Management modules
INSERT INTO group_module_permissions (group_id, module_id, action) VALUES
  ('0194a8ba-f1f1-7123-8123-123456789adc'::uuid, '0194a8b2-0015-7123-8123-123456789abc'::uuid, 'allow'), -- POS
  ('0194a8ba-f1f1-7123-8123-123456789adc'::uuid, '0194a8b2-0016-7123-8123-123456789abc'::uuid, 'allow'), -- InventoryManagement
  ('0194a8ba-f1f1-7123-8123-123456789adc'::uuid, '0194a8b2-c3f1-7123-8123-123456789ab1'::uuid, 'allow'), -- Products
  ('0194a8ba-f1f1-7123-8123-123456789adc'::uuid, '0194a8b2-a1b2-7123-8123-123456789abc'::uuid, 'allow'), -- Auto Orders
  ('0194a8ba-f1f1-7123-8123-123456789adc'::uuid, '0194a8b2-0fff-7123-8123-123456789afe'::uuid, 'allow'), -- Customer Management
  ('0194a8ba-f1f1-7123-8123-123456789adc'::uuid, '0194a8b2-0019-7123-8123-123456789abc'::uuid, 'allow'), -- Finance Management
  ('0194a8ba-f1f1-7123-8123-123456789adc'::uuid, '0194a8b2-0022-7123-8123-123456789abc'::uuid, 'allow'), -- POS Reports
  ('0194a8ba-f1f1-7123-8123-123456789adc'::uuid, '019b2958-e995-7d25-93f9-57548a2a0a2d'::uuid, 'allow'), -- Quotations
  ('0194a8ba-f1f1-7123-8123-123456789adc'::uuid, '019b2958-e9a1-7a80-b816-d5dac5c8d815'::uuid, 'allow')  -- Invoices
ON CONFLICT DO NOTHING;

INSERT INTO group_module_permissions (group_id, module_id, action) VALUES
  ('0194a8ba-fefe-7123-8123-123456789add'::uuid, '0194a8b2-bbbb-7123-8123-123456789aca'::uuid, 'allow'),  -- Cases
  ('0194a8ba-fefe-7123-8123-123456789add'::uuid, '0194a8b2-aaaa-7123-8123-123456789ac9'::uuid, 'allow'),  -- Clients
  ('0194a8ba-fefe-7123-8123-123456789add'::uuid, '0194a8b2-9999-7123-8123-123456789ac8'::uuid, 'allow'),  -- Time Entries & Expenses
  ('0194a8ba-fefe-7123-8123-123456789add'::uuid, '0194a8b2-8888-7123-8123-123456789ac7'::uuid, 'allow'),  -- Case Management
  ('0194a8ba-fefe-7123-8123-123456789add'::uuid, '01943a8b-2345-7890-abcd-123456789def'::uuid, 'allow'),  -- Task Manager
  ('0194a8ba-fefe-7123-8123-123456789add'::uuid, '0194a8b2-cccc-7123-8123-123456789acb'::uuid, 'allow')   -- User Access Manager
ON CONFLICT DO NOTHING;

INSERT INTO users(user_id, full_name) VALUES
  ('0194a8b2-1111-7123-8123-123456789ac0'::uuid, 'Cybex Admin'),
  ('0194a8b2-2222-7123-8123-123456789ac1'::uuid, 'Karunaratne Admin');

INSERT INTO user_emails (user_id, email)  VALUES
  ('0194a8b2-1111-7123-8123-123456789ac0'::uuid, 'admin@cybexpte.com'),
  ('0194a8b2-2222-7123-8123-123456789ac1'::uuid, 'jagath@cybexpte.com');

INSERT INTO user_org_memberships (user_id, org_id) VALUES
  ('0194a8b2-1111-7123-8123-123456789ac0'::uuid, '0194a8b2-1234-7123-8123-123456789abc'::uuid), -- Cybex Admin
  ('0194a8b2-2222-7123-8123-123456789ac1'::uuid, '0194a8b2-5678-7123-8123-123456789abd'::uuid)  -- Karunaratne Admin
  ON CONFLICT DO NOTHING;

-- Assign Cybex Admin to Dev Team group
INSERT INTO user_group_memberships (user_id, group_id) VALUES
  ('0194a8b2-1111-7123-8123-123456789ac0'::uuid, '0194a8b2-eeee-7123-8123-123456789ace'::uuid), -- Cybex Admin -> Dev Team
  ('0194a8b2-2222-7123-8123-123456789ac1'::uuid, '0194a8ba-fefe-7123-8123-123456789add'::uuid)
  ON CONFLICT DO NOTHING;