{
  "openapi": "3.1.0",
  "info": {
    "title": "Ayyo-db IMDb Aggregation API",
    "version": "2.0.0",
    "summary": "Universal IMDb search and title detail aggregation API with multi-layer fallbacks.",
    "description": "Lightweight, resilient IMDb metadata aggregation API supporting keyword search with pagination and full title details resolved through a 5-layer fallback pipeline (IMDb title JSON-LD, structural regex, IMDb /reference parser, api.imdbapi.dev, Cinemeta movie/series, and IMDb suggestion fallback).",
    "license": {
      "name": "MIT",
      "identifier": "MIT",
      "url": "https://opensource.org/licenses/MIT"
    }
  },
  "servers": [
    {
      "url": "https://ayyo-db.workers.dev",
      "description": "Cloudflare Workers edge deployment"
    },
    {
      "url": "https://mn-api-imdb.vercel.app",
      "description": "Vercel serverless deployment"
    },
    {
      "url": "http://localhost:8787",
      "description": "Local development server"
    }
  ],
  "tags": [
    {
      "name": "Service",
      "description": "API metadata, health, and documentation contract"
    },
    {
      "name": "Titles",
      "description": "Search IMDb titles and fetch detailed title metadata"
    }
  ],
  "paths": {
    "/api": {
      "get": {
        "operationId": "getApiInfo",
        "summary": "Get API service metadata and documentation contract",
        "tags": ["Service"],
        "responses": {
          "200": {
            "description": "Service metadata document",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiInfo"
                }
              }
            }
          }
        }
      }
    },
    "/api/search": {
      "get": {
        "operationId": "searchOrGetTitle",
        "summary": "Search titles by query (?q=) or retrieve full metadata by IMDb ID (?id=)",
        "description": "Supply either `q` (keyword search) or `id` (IMDb title ID such as `tt10698680`). When both are supplied, `id` takes precedence.",
        "tags": ["Titles"],
        "parameters": [
          {
            "name": "q",
            "in": "query",
            "required": false,
            "description": "Search query string (movie, TV series, etc.).",
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200
            },
            "example": "kgf"
          },
          {
            "name": "id",
            "in": "query",
            "required": false,
            "description": "IMDb title identifier matching `^tt\\d{5,12}$`.",
            "schema": {
              "type": "string",
              "pattern": "^tt\\d{5,12}$"
            },
            "example": "tt10698680"
          },
          {
            "name": "page",
            "in": "query",
            "required": false,
            "description": "1-based results page number (used with `q`).",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "default": 1
            }
          },
          {
            "name": "per_page",
            "in": "query",
            "required": false,
            "description": "Number of items per page (1 to 20, used with `q`).",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 20,
              "default": 10
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Search results or full title detail payload",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    { "$ref": "#/components/schemas/SearchResponse" },
                    { "$ref": "#/components/schemas/TitleDetail" },
                    { "$ref": "#/components/schemas/ErrorResponse" }
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Missing or invalid query parameters",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "500": {
            "description": "Upstream or internal server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "SearchItem": {
        "type": "object",
        "required": ["Title", "Year", "imdbID", "Type", "Poster"],
        "properties": {
          "Title": { "type": "string", "example": "K.G.F: Chapter 2" },
          "Year": { "type": "string", "example": "2022" },
          "imdbID": { "type": "string", "example": "tt10698680" },
          "Type": { "type": "string", "example": "movie" },
          "Poster": { "type": "string", "example": "https://m.media-amazon.com/images/M/example.jpg" }
        }
      },
      "SearchResponse": {
        "type": "object",
        "required": ["Search", "totalResults", "Response", "page", "nextPage"],
        "properties": {
          "Search": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/SearchItem" }
          },
          "totalResults": { "type": "string", "example": "12" },
          "Response": { "type": "string", "enum": ["True", "False"], "example": "True" },
          "page": { "type": "integer", "minimum": 1, "example": 1 },
          "nextPage": {
            "type": ["integer", "null"],
            "minimum": 2,
            "example": 2
          }
        }
      },
      "TitleDetail": {
        "type": "object",
        "required": [
          "Title",
          "Year",
          "Poster",
          "Plot",
          "imdbRating",
          "Director",
          "Writer",
          "Stars",
          "Genres",
          "Runtime",
          "Language",
          "ReleaseDate",
          "imdbID"
        ],
        "properties": {
          "Title": { "type": "string", "example": "K.G.F: Chapter 2" },
          "Year": { "type": "string", "example": "2022" },
          "Poster": { "type": "string", "example": "https://m.media-amazon.com/images/M/example.jpg" },
          "Plot": { "type": "string", "example": "In the blood-soaked Kolar Gold Fields, Rocky's name strikes fear into his foes." },
          "imdbRating": { "type": "string", "example": "8.2" },
          "Director": { "type": "string", "example": "Prashanth Neel" },
          "Writer": { "type": "string", "example": "Prashanth Neel" },
          "Stars": { "type": "string", "example": "Yash, Sanjay Dutt, Raveena Tandon" },
          "Genres": { "type": "string", "example": "Action, Crime, Drama" },
          "Runtime": { "type": "string", "example": "2h 48m" },
          "Language": { "type": "string", "example": "Kannada" },
          "ReleaseDate": { "type": "string", "example": "2022-04-14" },
          "imdbID": { "type": "string", "pattern": "^tt\\d{5,12}$", "example": "tt10698680" }
        }
      },
      "ApiInfo": {
        "type": "object",
        "required": ["name", "version", "basePath", "architecture", "endpoints", "errorModel"],
        "properties": {
          "name": { "type": "string" },
          "version": { "type": "string" },
          "basePath": { "type": "string" },
          "architecture": { "type": "object", "additionalProperties": true },
          "endpoints": {
            "type": "array",
            "items": { "type": "object", "additionalProperties": true }
          },
          "errorModel": { "type": "object", "additionalProperties": true },
          "operationalNotes": {
            "type": "array",
            "items": { "type": "string" }
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": { "type": "string", "example": "Please provide a search query (?q=...) or title ID (?id=tt...)" }
        }
      }
    }
  }
}
