{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://fuegoapp.dev/schemas/recording-v1.json",
    "title": "Fuego DevTools Recording",
    "description": "A Recording exported by Fuego DevTools: the Firebase operations a page made, with estimated counts and cost. Version 1; additions keep the version and readers ignore fields they do not know.",
    "type": "object",
    "required": [
        "format",
        "version",
        "exportedAt",
        "extension",
        "page",
        "firebase",
        "costSettings",
        "summary",
        "withDocumentContents",
        "operations"
    ],
    "properties": {
        "$schema": { "type": "string" },
        "format": { "const": "fuego-recording" },
        "version": { "const": 1 },
        "exportedAt": { "type": "string", "format": "date-time" },
        "extension": {
            "type": "object",
            "required": ["version", "browser"],
            "properties": {
                "version": { "type": "string" },
                "browser": { "type": "string" }
            }
        },
        "page": {
            "description": "The page recorded, as origin and path only: never its query string or hash, which may carry tokens.",
            "type": "object",
            "required": ["origin", "path"],
            "properties": {
                "origin": { "type": "string" },
                "path": { "type": "string", "pattern": "^/[^?#]*$" }
            }
        },
        "firebase": {
            "description": "What the Recording saw of the page's Firebase. Never the apiKey.",
            "type": "object",
            "required": ["projectIds", "emulator", "databases"],
            "properties": {
                "projectIds": { "type": "array", "items": { "type": "string" } },
                "emulator": { "type": "boolean" },
                "databases": { "type": "array", "items": { "type": "string" } }
            }
        },
        "costSettings": {
            "type": "object",
            "required": ["location", "edition", "gross", "pricesAsOf"],
            "properties": {
                "location": { "type": "string" },
                "edition": { "enum": ["standard", "enterprise"] },
                "gross": { "type": "boolean", "description": "true: the free quota is not taken off." },
                "pricesAsOf": { "type": "string", "format": "date" }
            }
        },
        "summary": {
            "type": "object",
            "required": ["counts", "cost"],
            "properties": {
                "counts": { "$ref": "#/$defs/counts" },
                "cost": {
                    "type": "object",
                    "required": [
                        "currency",
                        "total",
                        "firestore",
                        "rtdb",
                        "storage",
                        "perThousandVisits",
                        "lowerBound",
                        "hidden"
                    ],
                    "properties": {
                        "currency": { "const": "USD" },
                        "total": { "type": "number", "minimum": 0 },
                        "firestore": { "type": "number", "minimum": 0 },
                        "rtdb": { "type": "number", "minimum": 0 },
                        "storage": { "type": "number", "minimum": 0 },
                        "perThousandVisits": { "type": "number", "minimum": 0 },
                        "lowerBound": {
                            "type": "boolean",
                            "description": "The total is \"at least\": the traffic cannot show some costs."
                        },
                        "hidden": { "type": "array", "items": { "type": "string" } }
                    }
                }
            }
        },
        "withDocumentContents": {
            "type": "boolean",
            "description": "false: every document's fields were left out; paths (and so document ids) remain."
        },
        "operations": { "type": "array", "items": { "$ref": "#/$defs/operation" } }
    },
    "$defs": {
        "counts": {
            "type": "object",
            "required": [
                "reads",
                "writes",
                "deletes",
                "realtimeUpdates",
                "rawReads",
                "rtdbBytes",
                "storageA",
                "storageB",
                "storageEgressBytes"
            ],
            "properties": {
                "reads": {
                    "type": "integer",
                    "minimum": 0,
                    "description": "Firestore document reads, as production would bill them."
                },
                "writes": { "type": "integer", "minimum": 0 },
                "deletes": { "type": "integer", "minimum": 0 },
                "realtimeUpdates": {
                    "type": "integer",
                    "minimum": 0,
                    "description": "Listener changes after the first snapshot (Enterprise real-time update units)."
                },
                "rawReads": {
                    "type": "integer",
                    "minimum": 0,
                    "description": "Reads as observed; differs on the emulator, which re-sends whole targets."
                },
                "rtdbBytes": { "type": "integer", "minimum": 0 },
                "storageA": { "type": "integer", "minimum": 0 },
                "storageB": { "type": "integer", "minimum": 0 },
                "storageEgressBytes": { "type": "integer", "minimum": 0 }
            }
        },
        "operation": {
            "type": "object",
            "required": ["time", "kind", "operation", "outcome"],
            "properties": {
                "time": { "type": "integer", "minimum": 0, "description": "Milliseconds since the Recording started." },
                "kind": { "enum": ["firestore", "rtdb", "storage", "auth"] },
                "operation": { "type": "string" },
                "database": {
                    "type": "string",
                    "description": "Firestore database id, Realtime Database namespace or Storage bucket."
                },
                "path": { "type": "string" },
                "query": {
                    "description": "The query as Fuego query JSON (https://fuegoapp.dev/schemas/query-builder.schema.json), when it converts.",
                    "type": "object"
                },
                "live": { "type": "boolean" },
                "counts": { "$ref": "#/$defs/counts" },
                "documents": { "type": "array", "items": { "$ref": "#/$defs/document" } },
                "hidden": { "type": "array", "items": { "type": "string" } },
                "outcome": { "enum": ["pending", "ok", "error"] },
                "status": { "type": "integer" },
                "bytes": {
                    "type": "object",
                    "properties": {
                        "sent": { "type": "integer", "minimum": 0 },
                        "received": { "type": "integer", "minimum": 0 }
                    }
                }
            },
            "if": { "properties": { "kind": { "const": "auth" } } },
            "then": {
                "description": "Auth is exported as events only: no uid, email, claims or token.",
                "properties": { "documents": { "maxItems": 0 }, "path": false, "query": false }
            }
        },
        "document": {
            "type": "object",
            "required": ["path", "change"],
            "properties": {
                "path": { "type": "string" },
                "change": { "enum": ["added", "modified", "removed", "deleted", "read", "missing", "written"] },
                "fields": {
                    "description": "Firestore fields with the protocol's typed values as received, e.g. {\"integerValue\": \"1\"}.",
                    "type": "object"
                },
                "updateTime": { "type": "string" }
            }
        }
    }
}
