{
  "db_name": "PostgreSQL",
  "query": "\n        WITH user_balances AS (\n            SELECT\n                u.id as user_id,\n                COALESCE(c.balance, 0) + COALESCE(\n                    (SELECT SUM(\n                        CASE WHEN ct.transaction_type IN ('purchase', 'admin_grant')\n                        THEN ct.amount ELSE -ct.amount END\n                    )\n                    FROM credits_transactions ct\n                    WHERE ct.user_id = u.id\n                    AND ct.seq > COALESCE(c.checkpoint_seq, 0)),\n                    0\n                ) as balance\n            FROM users u\n            LEFT JOIN user_balance_checkpoints c ON c.user_id = u.id\n        )\n        SELECT\n            cm.id as composite_model_id,\n            ak.id as api_key_id,\n            ak.secret as api_key_secret,\n            ak.purpose as api_key_purpose,\n            ak.requests_per_second,\n            ak.burst_size\n        FROM deployed_models cm\n        CROSS JOIN LATERAL (\n            SELECT DISTINCT\n                ak.id,\n                ak.secret,\n                ak.purpose,\n                ak.requests_per_second,\n                ak.burst_size\n            FROM api_keys ak\n            WHERE (\n                -- System user always has access\n                ak.user_id = '00000000-0000-0000-0000-000000000000'\n                -- OR user is in a group assigned to this composite model (via deployment_groups)\n                OR EXISTS (\n                    SELECT 1 FROM user_groups ug\n                    INNER JOIN deployment_groups dg ON ug.group_id = dg.group_id\n                    WHERE dg.deployment_id = cm.id\n                      AND ug.user_id = ak.user_id\n                )\n                -- OR composite model is in public group (nil UUID)\n                OR EXISTS (\n                    SELECT 1 FROM deployment_groups dg\n                    WHERE dg.deployment_id = cm.id\n                      AND dg.group_id = '00000000-0000-0000-0000-000000000000'\n                )\n                -- OR this is a batch API key and composite model is an escalation target\n                OR (\n                    ak.purpose = 'batch'\n                    AND cm.alias = ANY($1::text[])\n                )\n            )\n            -- Require positive balance OR free model (system user always passes)\n            AND (\n                ak.user_id = '00000000-0000-0000-0000-000000000000'\n                OR EXISTS (\n                    SELECT 1 FROM user_balances ub\n                    WHERE ub.user_id = ak.user_id AND ub.balance > 0\n                )\n                OR (\n                    NOT EXISTS (\n                        SELECT 1 FROM model_tariffs mt\n                        WHERE mt.deployed_model_id = cm.id\n                          AND mt.valid_until IS NULL\n                          AND (mt.input_price_per_token > 0 OR mt.output_price_per_token > 0)\n                    )\n                )\n            )\n            AND ak.is_deleted = false\n        ) ak\n        WHERE cm.is_composite = TRUE\n          AND cm.deleted = FALSE\n        ORDER BY cm.id, ak.id\n        ",
  "describe": {
    "columns": [
      {
        "ordinal": 0,
        "name": "composite_model_id",
        "type_info": "Uuid"
      },
      {
        "ordinal": 1,
        "name": "api_key_id",
        "type_info": "Uuid"
      },
      {
        "ordinal": 2,
        "name": "api_key_secret",
        "type_info": "Varchar"
      },
      {
        "ordinal": 3,
        "name": "api_key_purpose",
        "type_info": "Varchar"
      },
      {
        "ordinal": 4,
        "name": "requests_per_second",
        "type_info": "Float4"
      },
      {
        "ordinal": 5,
        "name": "burst_size",
        "type_info": "Int4"
      }
    ],
    "parameters": {
      "Left": [
        "TextArray"
      ]
    },
    "nullable": [
      false,
      false,
      false,
      false,
      true,
      true
    ]
  },
  "hash": "e52d73bedd7c741caad0c19952da031dbde349c8afb49bb25594b24209389d70"
}
