{
  "openapi": "3.1.0",
  "info": {
    "title": "Itinerary DB API",
    "version": "0.2.0",
    "description": "Timestamped external fare observations. No airline live shopping or booking is implemented."
  },
  "servers": [
    {
      "url": "/"
    }
  ],
  "components": {
    "securitySchemes": {
      "ApiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key"
      },
      "IngestToken": {
        "type": "apiKey",
        "in": "header",
        "name": "X-Ingest-Token"
      }
    },
    "schemas": {
      "Search": {
        "type": "object",
        "required": [
          "origin",
          "destination",
          "departure_date"
        ],
        "properties": {
          "origin": {
            "type": "string",
            "pattern": "^[A-Za-z]{3}$"
          },
          "destination": {
            "type": "string",
            "pattern": "^[A-Za-z]{3}$"
          },
          "departure_date": {
            "type": "string",
            "format": "date"
          },
          "adults": {
            "type": "integer",
            "minimum": 1,
            "maximum": 9,
            "default": 1
          },
          "cabin": {
            "type": "string",
            "enum": [
              "economy",
              "premium_economy",
              "business",
              "first"
            ],
            "default": "economy"
          },
          "currency": {
            "type": "string",
            "pattern": "^[A-Za-z]{3}$",
            "default": "EUR"
          },
          "market": {
            "type": "string",
            "pattern": "^[A-Za-z]{2}$",
            "default": "DE"
          }
        }
      },
      "OfferInput": {
        "type": "object",
        "required": [
          "source_offer_id",
          "airline",
          "flight_numbers",
          "departure_local_date",
          "departure_at",
          "arrival_at",
          "price_total_minor",
          "baggage",
          "booking_url"
        ],
        "properties": {
          "source_offer_id": {
            "type": "string"
          },
          "airline": {
            "type": "string"
          },
          "flight_numbers": {
            "type": "array",
            "minItems": 1,
            "items": {
              "type": "string"
            }
          },
          "departure_local_date": {
            "type": "string",
            "format": "date"
          },
          "departure_at": {
            "type": "string",
            "format": "date-time"
          },
          "arrival_at": {
            "type": "string",
            "format": "date-time"
          },
          "price_total_minor": {
            "type": "integer",
            "minimum": 1,
            "description": "Observed total for the searched adult party, including mandatory fare taxes and fees, in minor currency units. Optional baggage and seats are excluded unless included in the fare."
          },
          "baggage": {
            "type": "string",
            "enum": [
              "unknown",
              "none",
              "cabin",
              "checked"
            ]
          },
          "booking_url": {
            "type": [
              "string",
              "null"
            ],
            "format": "uri",
            "description": "Source-provided HTTPS handoff when available; not a guarantee of current price or an exact itinerary."
          }
        }
      },
      "Ingest": {
        "type": "object",
        "required": [
          "source_id",
          "search",
          "status",
          "observed_at",
          "offers"
        ],
        "properties": {
          "source_id": {
            "type": "string"
          },
          "search": {
            "$ref": "#/components/schemas/Search"
          },
          "status": {
            "type": "string",
            "enum": [
              "ok",
              "partial",
              "no_offers",
              "error"
            ]
          },
          "observed_at": {
            "type": "string",
            "format": "date-time"
          },
          "offers": {
            "type": "array",
            "maxItems": 100,
            "items": {
              "$ref": "#/components/schemas/OfferInput"
            }
          },
          "error_code": {
            "type": "string",
            "description": "Required for error or partial status."
          }
        }
      },
      "ApiError": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              }
            }
          }
        }
      },
      "SourcePolicy": {
        "type": "object",
        "required": [
          "revision",
          "enabled",
          "downstream_api_use",
          "background_monitoring",
          "cross_tenant_reuse",
          "max_observation_age_seconds",
          "raw_retention_seconds",
          "allowed_capabilities"
        ],
        "properties": {
          "revision": {
            "type": "string",
            "description": "Operator-reviewed policy revision."
          },
          "enabled": {
            "type": "boolean"
          },
          "downstream_api_use": {
            "type": "boolean"
          },
          "background_monitoring": {
            "type": "boolean"
          },
          "cross_tenant_reuse": {
            "type": "boolean"
          },
          "max_observation_age_seconds": {
            "type": "integer",
            "minimum": 1,
            "maximum": 604800
          },
          "raw_retention_seconds": {
            "type": "integer",
            "minimum": 0,
            "maximum": 2592000
          },
          "allowed_capabilities": {
            "type": "array",
            "items": {
              "type": "string",
              "enum": [
                "searchOffers"
              ]
            },
            "uniqueItems": true
          }
        }
      },
      "SyntheticSearch": {
        "allOf": [
          {
            "$ref": "#/components/schemas/Search"
          },
          {
            "type": "object",
            "properties": {
              "source_ids": {
                "type": "array",
                "minItems": 1,
                "maxItems": 3,
                "uniqueItems": true,
                "items": {
                  "type": "string"
                },
                "description": "Approved synthetic source IDs; omitted means all eligible sources, up to three."
              }
            }
          }
        ]
      },
      "SearchHandle": {
        "type": "object",
        "properties": {
          "search_id": {
            "type": "string",
            "format": "uuid"
          },
          "state": {
            "type": "string",
            "enum": [
              "pending",
              "complete",
              "partial",
              "failed"
            ]
          },
          "status_url": {
            "type": "string"
          },
          "requested_sources": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      }
    }
  },
  "paths": {
    "/api/v1/health": {
      "get": {
        "summary": "Service and database health",
        "responses": {
          "200": {
            "description": "Healthy"
          },
          "503": {
            "description": "Unavailable"
          }
        }
      }
    },
    "/api/v1/sources": {
      "get": {
        "summary": "List registered sources",
        "responses": {
          "200": {
            "description": "Source list"
          }
        }
      },
      "post": {
        "summary": "Register a source",
        "security": [
          {
            "IngestToken": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "id",
                  "name",
                  "access_note"
                ],
                "properties": {
                  "id": {
                    "type": "string"
                  },
                  "name": {
                    "type": "string"
                  },
                  "access_note": {
                    "type": "string",
                    "description": "Internal note documenting the supplier access arrangement; not returned publicly."
                  },
                  "adapter_id": {
                    "type": "string",
                    "enum": [
                      "external",
                      "synthetic"
                    ],
                    "default": "external",
                    "description": "Source starts disabled. Synthetic requires an explicit environment flag and is test-only."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Registered disabled; policy review required before use."
          },
          "400": {
            "description": "Invalid input"
          },
          "401": {
            "description": "Unauthorized"
          },
          "409": {
            "description": "Already exists"
          }
        }
      }
    },
    "/api/v1/ingest": {
      "post": {
        "summary": "Record a source search outcome",
        "security": [
          {
            "IngestToken": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Ingest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Recorded"
          },
          "400": {
            "description": "Invalid input"
          },
          "401": {
            "description": "Unauthorized"
          },
          "422": {
            "description": "Source unknown or disabled"
          }
        }
      }
    },
    "/api/v1/search": {
      "post": {
        "summary": "Search recent observations",
        "description": "Returns the latest matching outcome per enabled source. Stale and failed sources remain visible in coverage. No live upstream search is performed. Only approved external sources are included; source policy can reduce the maximum effective age.",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "allOf": [
                  {
                    "$ref": "#/components/schemas/Search"
                  },
                  {
                    "type": "object",
                    "properties": {
                      "max_age_minutes": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 10080,
                        "default": 180
                      }
                    }
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Search result with complete, coverage, and offers fields. complete is false when no sources are registered or any enabled source has stale, missing, or failed observations."
          },
          "400": {
            "description": "Invalid input"
          },
          "401": {
            "description": "Unauthorized"
          },
          "503": {
            "description": "Credential not configured"
          }
        }
      }
    },
    "/api/v1/sources/{sourceId}/policy": {
      "put": {
        "summary": "Replace a source activation policy",
        "description": "Operator action. Does not itself establish supplier rights. Synthetic activation requires ENABLE_SYNTHETIC_SOURCE=1.",
        "security": [
          {
            "IngestToken": []
          }
        ],
        "parameters": [
          {
            "name": "sourceId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SourcePolicy"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Policy updated"
          },
          "400": {
            "description": "Invalid policy"
          },
          "401": {
            "description": "Unauthorized"
          },
          "404": {
            "description": "Source not found"
          },
          "409": {
            "description": "Missing policy or synthetic mode disabled"
          }
        }
      }
    },
    "/api/v1/searches": {
      "post": {
        "summary": "Start a bounded synthetic collection search",
        "description": "Returns a durable search handle. No airline collection is configured. Idempotency is scoped to the configured API key. Departure date must be within the next 365 days.",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 8,
              "maxLength": 120
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SyntheticSearch"
              }
            }
          }
        },
        "responses": {
          "202": {
            "description": "Accepted or replayed handle",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SearchHandle"
                }
              }
            }
          },
          "400": {
            "description": "Invalid input"
          },
          "401": {
            "description": "Unauthorized"
          },
          "409": {
            "description": "Idempotency key reused with different input"
          },
          "422": {
            "description": "No eligible source or synthetic mode disabled"
          },
          "503": {
            "description": "Collection bindings unavailable"
          }
        }
      }
    },
    "/api/v1/searches/{searchId}": {
      "get": {
        "summary": "Read a synthetic search and source outcomes",
        "description": "A completed empty result requires verified no_offers from every requested source. Synthetic offers are marked and are never bookable.",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "parameters": [
          {
            "name": "searchId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Search status, per-source coverage, offers, and zero-billable usage"
          },
          "401": {
            "description": "Unauthorized"
          },
          "404": {
            "description": "Not found or not owned by this API key"
          }
        }
      }
    }
  }
}
