{
  "openapi": "3.1.0",
  "info": {
    "title": "Visitar.com Booking Request API",
    "version": "1.0.0",
    "description": "Creates booking requests for independent hotels. A pending request is not a confirmed booking. Only the hotel can confirm.",
    "license": { "name": "Proprietary", "identifier": "LicenseRef-Proprietary" }
  },
  "servers": [
    { "url": "https://www.visitar.com" }
  ],
  "security": [],
  "paths": {
    "/api/v1/requests": {
      "post": {
        "operationId": "createBookingRequest",
        "summary": "Send a booking request to a hotel",
        "description": "Authorises the deposit but does not capture it. The initial reservation status is always ReservationPending.",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": { "type": "string", "minLength": 8, "maxLength": 255 }
          },
          {
            "name": "User-Agent",
            "in": "header",
            "required": true,
            "description": "Identify the calling agent honestly.",
            "schema": { "type": "string", "minLength": 4 }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CreateRequest" }
            }
          }
        },
        "responses": {
          "201": {
            "description": "The deposit is authorised and the request has been sent. This is not a confirmed booking.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/RequestStatus" } } }
          },
          "200": {
            "description": "Idempotent replay. Returns the original request.",
            "headers": {
              "Idempotent-Replayed": { "schema": { "type": "string", "const": "true" } }
            },
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/RequestStatus" } } }
          },
          "402": {
            "description": "The payment method needs customer action before a pending request can be created."
          },
          "422": { "$ref": "#/components/responses/ValidationError" },
          "429": { "description": "Rate limit exceeded." }
        }
      }
    },
    "/api/v1/requests/{id}": {
      "get": {
        "operationId": "getBookingRequestStatus",
        "summary": "Read the current status of a request",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "responses": {
          "200": {
            "description": "Current honest request status.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/RequestStatus" } } }
          },
          "404": { "description": "Request not found." }
        }
      }
    },
    "/api/v1/hotels": {
      "get": {
        "operationId": "listHotels",
        "summary": "List request-enabled hotels",
        "parameters": [
          { "name": "city", "in": "query", "schema": { "type": "string" } },
          { "name": "barrio", "in": "query", "schema": { "type": "string" } },
          { "name": "page", "in": "query", "schema": { "type": "integer", "minimum": 1, "default": 1 } },
          { "name": "per_page", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 50, "default": 20 } }
        ],
        "responses": {
          "200": {
            "description": "Paginated hotel index.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["items", "pagination"],
                  "properties": {
                    "items": { "type": "array", "items": { "$ref": "#/components/schemas/Hotel" } },
                    "pagination": { "$ref": "#/components/schemas/Pagination" }
                  }
                }
              }
            }
          },
          "400": { "description": "Invalid filter or pagination value." }
        }
      }
    },
    "/api/v1/hotels/{slug}": {
      "get": {
        "operationId": "getHotel",
        "summary": "Read a structured hotel record and request terms",
        "parameters": [
          { "name": "slug", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": {
          "200": {
            "description": "Hotel record.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Hotel" } } }
          },
          "404": { "description": "Hotel not found." }
        }
      }
    }
  },
  "webhooks": {
    "requestStatusChanged": {
      "post": {
        "operationId": "receiveRequestStatusChanged",
        "summary": "Status change callback",
        "description": "Sent to callback_url on every state change. X-Visitar-Signature is sha256=<HMAC-SHA256 of the raw body>. Delivery is retried three times with backoff.",
        "parameters": [
          {
            "name": "X-Visitar-Signature",
            "in": "header",
            "required": true,
            "schema": { "type": "string", "pattern": "^sha256=[a-f0-9]{64}$" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": { "schema": { "$ref": "#/components/schemas/RequestRecord" } }
          }
        },
        "responses": {
          "200": { "description": "Callback accepted." }
        }
      }
    }
  },
  "components": {
    "responses": {
      "ValidationError": {
        "description": "The request failed shared form/API validation.",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "required": ["error", "message", "fields"],
              "properties": {
                "error": { "const": "validation_error" },
                "message": { "type": "string" },
                "fields": { "type": "object", "additionalProperties": { "type": "string" } }
              }
            }
          }
        }
      }
    },
    "schemas": {
      "CreateRequest": {
        "type": "object",
        "additionalProperties": false,
        "required": ["hotel_id", "checkin", "checkout", "adults", "children", "rooms", "guest", "locale", "allow_rebroadcast", "payment"],
        "properties": {
          "hotel_id": { "type": "string", "example": "rossio-garcia-hotel" },
          "checkin": { "type": "string", "format": "date" },
          "checkout": { "type": "string", "format": "date" },
          "adults": { "type": "integer", "minimum": 1, "maximum": 12 },
          "children": { "type": "integer", "minimum": 0, "maximum": 8 },
          "children_ages": {
            "type": "array",
            "items": { "type": "integer", "minimum": 0, "maximum": 17 }
          },
          "rooms": { "type": "integer", "minimum": 1, "maximum": 5 },
          "guest": { "$ref": "#/components/schemas/Guest" },
          "message": { "type": ["string", "null"], "maxLength": 2000 },
          "locale": { "type": "string", "enum": ["es", "pt", "en"] },
          "allow_rebroadcast": { "type": "boolean" },
          "callback_url": { "type": ["string", "null"], "format": "uri", "pattern": "^https://" },
          "payment": {
            "type": "object",
            "additionalProperties": false,
            "required": ["stripe_payment_method_id"],
            "properties": {
              "stripe_payment_method_id": { "type": "string", "pattern": "^pm_" }
            }
          }
        }
      },
      "Guest": {
        "type": "object",
        "additionalProperties": false,
        "required": ["name", "email", "phone"],
        "properties": {
          "name": { "type": "string", "minLength": 1, "maxLength": 200 },
          "email": { "type": "string", "format": "email", "maxLength": 320 },
          "phone": { "type": "string", "minLength": 7, "maxLength": 40 }
        }
      },
      "RequestStatus": {
        "type": "object",
        "required": ["request_id", "status", "reservation_status", "expires_at", "deposit", "message"],
        "properties": {
          "request_id": { "type": "string", "format": "uuid" },
          "status": { "type": "string", "enum": ["pending", "confirmed", "declined", "expired", "cancelled"] },
          "reservation_status": {
            "type": "string",
            "enum": [
              "https://schema.org/ReservationPending",
              "https://schema.org/ReservationConfirmed",
              "https://schema.org/ReservationCancelled"
            ]
          },
          "expires_at": { "type": "string", "format": "date-time" },
          "deposit": { "$ref": "#/components/schemas/Deposit" },
          "message": { "type": "string" }
        }
      },
      "RequestRecord": {
        "allOf": [
          { "$ref": "#/components/schemas/RequestStatus" },
          {
            "type": "object",
            "properties": {
              "hotel_id": { "type": "string" },
              "stay": { "type": "object" },
              "guest": { "$ref": "#/components/schemas/Guest" },
              "source": { "type": "string", "enum": ["web", "api", "mcp"] },
              "callback_url": { "type": ["string", "null"] },
              "created_at": { "type": "string", "format": "date-time" },
              "responded_at": { "type": ["string", "null"], "format": "date-time" },
              "response_seconds": { "type": ["integer", "null"] }
            }
          }
        ]
      },
      "Deposit": {
        "type": "object",
        "required": ["amount", "currency", "refundable"],
        "properties": {
          "amount": { "type": "integer", "description": "Minor currency units." },
          "currency": { "type": "string", "minLength": 3, "maxLength": 3 },
          "refundable": { "type": "boolean" }
        }
      },
      "Hotel": {
        "type": "object",
        "required": ["id", "slug", "name", "address", "capacity", "room_types", "deposit_policy", "cancellation_terms", "expected_response_hours", "barrio", "city", "sameAs"],
        "properties": {
          "id": { "type": "string" },
          "slug": { "type": "string" },
          "name": { "type": "string" },
          "address": { "type": "object" },
          "geo": { "type": ["object", "null"] },
          "capacity": { "type": ["integer", "null"] },
          "room_types": { "type": "array", "items": { "type": "object" } },
          "prices_from": { "type": ["object", "null"] },
          "deposit_policy": { "$ref": "#/components/schemas/Deposit" },
          "cancellation_terms": {
            "type": "object",
            "required": ["balance_due_at", "cancellation_deadline_hours", "transferable_to_new_date"],
            "properties": {
              "balance_due_at": { "type": "string" },
              "cancellation_deadline_hours": { "type": ["integer", "null"] },
              "transferable_to_new_date": { "type": "boolean" }
            }
          },
          "expected_response_hours": { "type": "integer" },
          "barrio": { "type": "string" },
          "city": { "type": "string" },
          "sameAs": { "type": "array", "items": { "type": "string", "format": "uri" } },
          "registry": { "type": ["string", "null"] },
          "request_enabled": { "type": "boolean" }
        }
      },
      "Pagination": {
        "type": "object",
        "required": ["page", "per_page", "total", "pages"],
        "properties": {
          "page": { "type": "integer" },
          "per_page": { "type": "integer" },
          "total": { "type": "integer" },
          "pages": { "type": "integer" }
        }
      }
    }
  }
}
