{
  "openapi": "3.1.0",
  "info": {
    "title": "Media Discussions Public Reads",
    "version": "0.1.0-alpha.1",
    "description": "Read-only tools for the alpha pilot. A docs-only host serves documentation, Lexicons, and health. Episode, resolver, and catalog operations require a full app. Check /api/healthz for a deployment's software version."
  },
  "servers": [
    {
      "url": "/",
      "description": "The origin that serves this document. When importing a downloaded copy into a tool that requires an absolute base URL, set the intended deployment origin explicitly."
    }
  ],
  "externalDocs": {
    "description": "Media Discussions interoperability reference",
    "url": "https://media.experiments.verylittlegravitas.com/docs"
  },
  "paths": {
    "/api/healthz": {
      "get": {
        "operationId": "getMediaHealth",
        "summary": "Read the health status and software version in either deployment mode",
        "responses": {
          "200": {
            "description": "Deployment health and version",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["status", "version"],
                  "properties": {
                    "status": { "const": "ok" },
                    "version": { "type": "string", "example": "0.1.0-alpha.1" },
                    "sha": { "type": "string", "description": "Build revision where supplied." }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/docs": {
      "get": {
        "operationId": "getMediaReference",
        "summary": "Read the public interoperability reference",
        "responses": {
          "200": {
            "description": "Public HTML reference",
            "content": { "text/html": { "schema": { "type": "string" } } }
          }
        }
      }
    },
    "/llms.txt": {
      "get": {
        "operationId": "getMediaAgentIndex",
        "summary": "Discover implementation resources",
        "responses": {
          "200": {
            "description": "Plain text discovery index",
            "content": { "text/plain": { "schema": { "type": "string" } } }
          }
        }
      }
    },
    "/docs/agents.md": {
      "get": {
        "operationId": "getMediaAgentGuide",
        "summary": "Read the agent implementation guide",
        "responses": {
          "200": {
            "description": "Agent implementation guide",
            "content": { "text/markdown": { "schema": { "type": "string" } } }
          }
        }
      }
    },
    "/docs/lexicons": {
      "get": {
        "operationId": "listMediaLexicons",
        "summary": "Discover the project's versioned HTTP Lexicon schemas",
        "description": "The index version is the running software version. Each href serves a schema from this reference.",
        "responses": {
          "200": {
            "description": "Lexicon schema index",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["version", "lexicons"],
                  "properties": {
                    "version": { "type": "string" },
                    "lexicons": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "required": ["id", "href"],
                        "properties": {
                          "id": { "type": "string", "example": "com.danhon.atprotomedia.episode" },
                          "href": { "type": "string", "format": "uri-reference", "example": "/docs/lexicons/com.danhon.atprotomedia.episode.json" }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/docs/lexicons/{nsid}.json": {
      "get": {
        "operationId": "getMediaLexicon",
        "summary": "Read a project Lexicon by its indexed NSID",
        "description": "Verify that the returned id matches the index entry. Check DNS and the publisher's repository separately for network Lexicon discovery.",
        "parameters": [
          {
            "name": "nsid",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "pattern": "^com\\.danhon\\.atprotomedia\\.[a-z]+$" },
            "example": "com.danhon.atprotomedia.episode"
          }
        ],
        "responses": {
          "200": {
            "description": "AT Protocol Lexicon document",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["lexicon", "id", "defs"],
                  "properties": {
                    "lexicon": { "const": 1 },
                    "id": { "type": "string" },
                    "defs": { "type": "object", "minProperties": 1, "additionalProperties": { "type": "object" } }
                  }
                }
              }
            }
          },
          "404": { "$ref": "#/components/responses/NotFound" }
        }
      }
    },
    "/api/episodes/{slug}": {
      "get": {
        "operationId": "getMediaEpisode",
        "summary": "Read an episode, its catalog hierarchy, mappings, and curated discussions",
        "description": "Available on the full app. With no catalog identity, portable AT URI fields are null.",
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "pattern": "^[a-z0-9-]+$" },
            "example": "73-yards"
          }
        ],
        "responses": {
          "200": {
            "description": "Episode view",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["episode", "series", "season", "mappings", "discussions"],
                  "properties": {
                    "episode": { "$ref": "#/components/schemas/Episode" },
                    "series": { "$ref": "#/components/schemas/Series" },
                    "season": { "$ref": "#/components/schemas/Season" },
                    "mappings": { "type": "array", "items": { "$ref": "#/components/schemas/Mapping" } },
                    "discussions": { "type": "array", "items": { "$ref": "#/components/schemas/Discussion" } }
                  }
                }
              }
            }
          },
          "404": { "$ref": "#/components/responses/NotFound" }
        }
      }
    },
    "/api/resolve": {
      "get": {
        "operationId": "resolveMediaEpisode",
        "summary": "Resolve a provider or episode-page URL against trusted mappings",
        "description": "Available on the full app. Send the source URL without pre-encoding it if your HTTP client encodes query parameters. The app resolves against the configured claims. Keep the original link, publisher DID, and unresolved or ambiguous state when applicable.",
        "parameters": [
          {
            "name": "url",
            "in": "query",
            "required": true,
            "description": "Absolute HTTP or HTTPS source URL.",
            "schema": { "type": "string", "format": "uri", "maxLength": 2000 },
            "example": "https://www.imdb.com/title/tt28704625/"
          }
        ],
        "responses": {
          "200": {
            "description": "A single trusted match and its original source mapping",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["mapping", "episode"],
                  "properties": {
                    "mapping": { "$ref": "#/components/schemas/Mapping" },
                    "episode": { "$ref": "#/components/schemas/Episode" }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Unresolved or invalid URL on a full app. A docs-only host returns plain text for this route.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["status"],
                  "properties": { "status": { "const": "unresolved" } }
                }
              },
              "text/plain": { "schema": { "type": "string" } }
            }
          },
          "409": {
            "description": "Multiple eligible episode results remain. Keep the claim unresolved for human review.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["status"],
                  "properties": { "status": { "const": "ambiguous" } }
                }
              }
            }
          }
        }
      }
    },
    "/api/catalog": {
      "get": {
        "operationId": "getMediaCatalogManifest",
        "summary": "Read the public manifest from a DID-configured full app",
        "description": "The manifest is a candidate publication. Read the publisher's PDS to confirm publication. Private watch data and permissions are outside this tool surface.",
        "responses": {
          "200": {
            "description": "Candidate records with their collection and repository key",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["records"],
                  "properties": {
                    "records": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/CatalogEntry" }
                    }
                  }
                }
              }
            }
          },
          "404": { "$ref": "#/components/responses/NotFound" },
          "503": {
            "description": "The full app has no configured catalog identity",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["status"],
                  "properties": { "status": { "const": "catalog-identity-unconfigured" } }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "responses": {
      "NotFound": {
        "description": "The resource or route is unavailable on this deployment",
        "content": { "text/plain": { "schema": { "type": "string" } } }
      }
    },
    "schemas": {
      "NullableAtUri": {
        "type": ["string", "null"],
        "pattern": "^at://did:",
        "description": "DID-based record reference. An unconfigured local catalog uses null."
      },
      "Episode": {
        "type": "object",
        "required": ["id", "key", "title", "path", "url", "uri", "seriesUri", "seasonUri", "sources"],
        "properties": {
          "id": { "type": "string", "example": "73-yards" },
          "key": { "type": "string", "example": "doctor-who-2024-73-yards" },
          "title": { "type": "string" },
          "description": { "type": "string" },
          "path": { "type": "string", "example": "/episodes/73-yards" },
          "url": { "type": "string", "format": "uri" },
          "uri": { "$ref": "#/components/schemas/NullableAtUri" },
          "seriesUri": { "$ref": "#/components/schemas/NullableAtUri" },
          "seasonUri": { "$ref": "#/components/schemas/NullableAtUri" },
          "seasonNumber": { "type": "integer", "minimum": 0 },
          "episodeNumber": { "type": "integer", "minimum": 0 },
          "airDate": { "type": "string", "format": "date" },
          "watchUrl": { "type": "string", "format": "uri" },
          "sources": {
            "type": "array",
            "items": {
              "type": "object",
              "required": ["source", "label", "url"],
              "properties": {
                "source": { "type": "string" },
                "label": { "type": "string" },
                "url": { "type": "string", "format": "uri" }
              }
            }
          }
        }
      },
      "Series": {
        "type": "object",
        "required": ["key", "title", "uri"],
        "properties": {
          "key": { "type": "string" },
          "title": { "type": "string" },
          "description": { "type": "string" },
          "uri": { "$ref": "#/components/schemas/NullableAtUri" }
        }
      },
      "Season": {
        "type": "object",
        "required": ["key", "title", "number", "uri", "seriesUri"],
        "properties": {
          "key": { "type": "string" },
          "title": { "type": "string" },
          "number": { "type": "integer", "minimum": 0 },
          "uri": { "$ref": "#/components/schemas/NullableAtUri" },
          "seriesUri": { "$ref": "#/components/schemas/NullableAtUri" }
        }
      },
      "Mapping": {
        "type": "object",
        "required": ["source", "externalId", "url", "episodeId", "episodeUri", "publisherDid"],
        "properties": {
          "source": { "type": "string", "examples": ["bbc", "imdb", "apple-tv", "episode-page"] },
          "externalId": { "type": "string" },
          "url": { "type": "string", "format": "uri" },
          "episodeId": { "type": "string" },
          "episodeUri": { "$ref": "#/components/schemas/NullableAtUri" },
          "publisherDid": { "type": ["string", "null"], "pattern": "^did:" }
        }
      },
      "Discussion": {
        "type": "object",
        "required": ["source", "title", "phase", "spoilers", "url"],
        "properties": {
          "source": { "type": "string" },
          "title": { "type": "string" },
          "phase": { "type": "string" },
          "spoilers": { "type": "string" },
          "url": { "type": "string", "format": "uri" }
        }
      },
      "CatalogEntry": {
        "type": "object",
        "required": ["collection", "rkey", "record"],
        "properties": {
          "collection": {
            "enum": [
              "com.danhon.atprotomedia.series",
              "com.danhon.atprotomedia.season",
              "com.danhon.atprotomedia.episode",
              "com.danhon.atprotomedia.mapping"
            ]
          },
          "rkey": { "type": "string" },
          "record": {
            "type": "object",
            "required": ["$type"],
            "properties": {
              "$type": { "type": "string", "description": "Matches the collection. Load its Lexicon to validate record fields." }
            }
          }
        }
      }
    }
  }
}
