{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/xpoz-ai/json-schema/xpoz-ai-operation-state-schema.json",
  "title": "OperationState",
  "x-generated": "2026-10-31",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/xpoz-ai-openapi.json#/components/schemas/OperationState",
  "type": "object",
  "properties": {
    "operationId": {
      "type": "string"
    },
    "status": {
      "type": "string",
      "enum": [
        "running",
        "success",
        "error",
        "no_data",
        "cancelled"
      ]
    },
    "result": {
      "description": "Present once the operation finished successfully. Holds the same envelope the synchronous endpoint would have returned.",
      "anyOf": [
        {
          "$ref": "#/$defs/DataResponse"
        },
        {
          "$ref": "#/$defs/RedditPostWithComments"
        },
        {
          "$ref": "#/$defs/RedditSubredditWithPosts"
        }
      ]
    },
    "error": {
      "type": "string"
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "updatedAt": {
      "type": "string",
      "format": "date-time"
    }
  },
  "required": [
    "operationId",
    "status",
    "createdAt",
    "updatedAt"
  ],
  "$defs": {
    "DataResponse": {
      "type": "object",
      "properties": {
        "results": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": true,
            "description": "Entity shaped by the requested `fields`."
          }
        },
        "count": {
          "type": "integer"
        },
        "dataSource": {
          "$ref": "#/$defs/DataSource"
        },
        "pagination": {
          "$ref": "#/$defs/Pagination"
        },
        "export": {
          "$ref": "#/$defs/ExportInfo"
        },
        "dbQuery": {
          "type": "string"
        }
      },
      "required": [
        "results",
        "count",
        "dataSource"
      ],
      "description": "Standard envelope for stored-data endpoints. `pagination` is present on paged results, `export` when a CSV export was queued, and `dbQuery` echoes the executed query. When nothing matched, `results` is empty and `count` is 0."
    },
    "DataSource": {
      "type": "string",
      "enum": [
        "database",
        "api",
        "hybrid"
      ],
      "description": "Where the results came from."
    },
    "ExportInfo": {
      "type": "object",
      "properties": {
        "dataDumpExportOperationId": {
          "type": "string"
        }
      },
      "required": [
        "dataDumpExportOperationId"
      ]
    },
    "Pagination": {
      "type": "object",
      "properties": {
        "totalRows": {
          "type": "integer"
        },
        "totalPages": {
          "type": "integer"
        },
        "resultsCount": {
          "type": "integer"
        },
        "pageSize": {
          "type": "integer"
        },
        "tableName": {
          "type": "string"
        }
      },
      "required": [
        "resultsCount",
        "pageSize",
        "tableName"
      ]
    },
    "RedditPostWithComments": {
      "type": "object",
      "properties": {
        "post": {
          "type": "object",
          "additionalProperties": true,
          "description": "Entity shaped by the requested `fields`."
        },
        "results": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": true,
            "description": "Entity shaped by the requested `fields`."
          }
        },
        "count": {
          "type": "integer"
        },
        "dataSource": {
          "$ref": "#/$defs/DataSource"
        },
        "pagination": {
          "$ref": "#/$defs/Pagination"
        },
        "dbQuery": {
          "type": "string"
        }
      },
      "required": [
        "post",
        "results",
        "count",
        "dataSource"
      ],
      "description": "A Reddit post plus a page of its comments."
    },
    "RedditSubredditWithPosts": {
      "type": "object",
      "properties": {
        "subreddit": {
          "type": "object",
          "additionalProperties": true,
          "description": "Entity shaped by the requested `fields`."
        },
        "results": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": true,
            "description": "Entity shaped by the requested `fields`."
          }
        },
        "count": {
          "type": "integer"
        },
        "dataSource": {
          "$ref": "#/$defs/DataSource"
        },
        "pagination": {
          "$ref": "#/$defs/Pagination"
        },
        "dbQuery": {
          "type": "string"
        }
      },
      "required": [
        "subreddit",
        "results",
        "count",
        "dataSource"
      ],
      "description": "A subreddit plus a page of its posts."
    }
  }
}