-- Seed the system organization (must exist before FK constraints on other tables are applied)
INSERT INTO organizations (
  organization_id,
  name,
  slug,
  plan_tier,
  max_agents,
  max_tokens_per_month,
  status,
  created_at,
  updated_at
) VALUES (
  'org_system',
  'System',
  'system',
  'enterprise',
  999999,
  999999999,
  'active',
  NOW(),
  NOW()
) ON CONFLICT (organization_id) DO NOTHING;

-- Backfill any existing rows that did not get the DEFAULT applied
UPDATE agents
  SET organization_id = 'org_system'
  WHERE organization_id IS NULL;

UPDATE credentials
  SET organization_id = 'org_system'
  WHERE organization_id IS NULL;

UPDATE audit_events
  SET organization_id = 'org_system'
  WHERE organization_id IS NULL;
