DO $$
BEGIN
  IF to_regclass('public.apps') IS NULL THEN
    RAISE EXCEPTION 'missing table: apps';
  END IF;
  IF to_regclass('public.tenant_entitlements') IS NULL THEN
    RAISE EXCEPTION 'missing table: tenant_entitlements';
  END IF;
  IF to_regclass('public.tenant_enabled_apps_history') IS NULL THEN
    RAISE EXCEPTION 'missing table: tenant_enabled_apps_history';
  END IF;

  IF NOT EXISTS (
    SELECT 1 FROM tenants WHERE id = '00000000-0000-0000-0000-000000000001'
  ) THEN
    RAISE EXCEPTION 'missing seed tenant Platform';
  END IF;

  IF NOT EXISTS (SELECT 1 FROM permissions WHERE code = 'iam:tenant:enabled_apps:write') THEN
    RAISE EXCEPTION 'missing seed permission iam:tenant:enabled_apps:write';
  END IF;

  IF NOT EXISTS (SELECT 1 FROM apps WHERE id = 'tms') THEN
    RAISE EXCEPTION 'missing seed app tms';
  END IF;

  IF NOT EXISTS (SELECT 1 FROM tenant_entitlements WHERE tenant_id = '11111111-1111-1111-1111-111111111111') THEN
    RAISE EXCEPTION 'missing tenant_entitlements row for Default Corp';
  END IF;
END $$;

