{
  "openapi": "3.0.3",
  "info": {
    "title": "IntoCal API",
    "description": "IntoCal scheduling API. Resource-oriented endpoints for event types, availability, slots, and bookings.",
    "version": "1.0.0",
    "contact": {
      "name": "IntoCal Support",
      "url": "https://intocal.com"
    }
  },
  "servers": [
    {
      "url": "https://api.intocal.com/v1",
      "description": "Production"
    }
  ],
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Optional API key for authenticated access. Public read endpoints work without auth."
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean", "enum": [false] },
          "error": {
            "type": "object",
            "properties": {
              "code": { "type": "string" },
              "message": { "type": "string" },
              "suggested_action": { "type": "string" }
            },
            "required": ["code", "message"]
          }
        }
      },
      "Invitee": {
        "type": "object",
        "properties": {
          "name": { "type": "string", "maxLength": 100 },
          "email": { "type": "string", "format": "email", "maxLength": 255 },
          "phone": { "type": "string", "maxLength": 30 },
          "timezone": { "type": "string" }
        },
        "required": ["name", "email"]
      },
      "Slot": {
        "type": "object",
        "properties": {
          "start": { "type": "string", "format": "date-time" },
          "end": { "type": "string", "format": "date-time" },
          "timezone": { "type": "string" },
          "available": { "type": "boolean" },
          "event_type_id": { "type": "string" },
          "host_id": { "type": "string" }
        }
      },
      "Booking": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "status": { "type": "string", "enum": ["confirmed", "canceled"] },
          "event_type_id": { "type": "string" },
          "host_id": { "type": "string" },
          "start": { "type": "string", "format": "date-time" },
          "end": { "type": "string", "format": "date-time" },
          "timezone": { "type": "string" },
          "invitee": { "$ref": "#/components/schemas/Invitee" },
          "location": {
            "type": "object",
            "properties": {
              "type": { "type": "string" },
              "value": { "type": "string" }
            }
          },
          "metadata": { "type": "object" },
          "cancel_reason": { "type": "string", "nullable": true },
          "canceled_at": { "type": "string", "format": "date-time", "nullable": true },
          "created_at": { "type": "string", "format": "date-time" }
        }
      }
    }
  },
  "security": [{ "BearerAuth": [] }],
  "paths": {
    "/event-types": {
      "get": {
        "operationId": "listEventTypes",
        "summary": "List event types",
        "tags": ["Event Types"],
        "security": [],
        "parameters": [
          { "name": "host_id", "in": "query", "schema": { "type": "string" }, "description": "Host to scope results to. Required for unauthenticated requests; authenticated requests default to the caller's own host." },
          { "name": "is_active", "in": "query", "schema": { "type": "boolean" }, "description": "Filter by active status" }
        ],
        "responses": {
          "200": {
            "description": "List of event types",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "data": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": { "type": "string" },
                          "slug": { "type": "string" },
                          "name": { "type": "string" },
                          "duration_minutes": { "type": "integer" },
                          "is_active": { "type": "boolean" }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/hosts/{host_id}/availability": {
      "get": {
        "operationId": "getHostAvailability",
        "summary": "Get host availability rules",
        "tags": ["Availability"],
        "security": [],
        "parameters": [
          { "name": "host_id", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": {
          "200": {
            "description": "Host availability rules",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "data": {
                      "type": "object",
                      "properties": {
                        "host_id": { "type": "string" },
                        "timezone": { "type": "string" },
                        "weekly_rules": { "type": "array", "items": { "type": "object" } },
                        "date_overrides": { "type": "array", "items": { "type": "object" } }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/slots/query": {
      "post": {
        "operationId": "querySlots",
        "summary": "Query available time slots",
        "tags": ["Slots"],
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["event_type_id", "host_id", "from", "to"],
                "properties": {
                  "event_type_id": { "type": "string" },
                  "host_id": { "type": "string" },
                  "from": { "type": "string", "format": "date-time" },
                  "to": { "type": "string", "format": "date-time" },
                  "timezone": { "type": "string", "default": "UTC" }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Available slots",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "data": {
                      "type": "object",
                      "properties": {
                        "slots": { "type": "array", "items": { "$ref": "#/components/schemas/Slot" } }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/bookings": {
      "post": {
        "operationId": "createBooking",
        "summary": "Create a booking",
        "tags": ["Bookings"],
        "parameters": [
          { "name": "Idempotency-Key", "in": "header", "schema": { "type": "string" }, "description": "Unique key to prevent duplicate bookings" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["event_type_id", "host_id", "slot_start", "invitee"],
                "properties": {
                  "event_type_id": { "type": "string" },
                  "host_id": { "type": "string" },
                  "slot_start": { "type": "string", "format": "date-time" },
                  "timezone": { "type": "string", "default": "UTC" },
                  "invitee": { "$ref": "#/components/schemas/Invitee" },
                  "metadata": { "type": "object" }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Booking created",
            "content": { "application/json": { "schema": { "type": "object", "properties": { "ok": { "type": "boolean" }, "data": { "$ref": "#/components/schemas/Booking" } } } } }
          },
          "409": { "description": "Slot not available", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } }
        }
      }
    },
    "/bookings/{booking_id}": {
      "get": {
        "operationId": "getBooking",
        "summary": "Get a booking",
        "tags": ["Bookings"],
        "parameters": [
          { "name": "booking_id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }
        ],
        "responses": {
          "200": {
            "description": "Booking details",
            "content": { "application/json": { "schema": { "type": "object", "properties": { "ok": { "type": "boolean" }, "data": { "$ref": "#/components/schemas/Booking" } } } } }
          },
          "404": { "description": "Booking not found" }
        }
      }
    },
    "/bookings/{booking_id}/cancel": {
      "post": {
        "operationId": "cancelBooking",
        "summary": "Cancel a booking",
        "tags": ["Bookings"],
        "parameters": [
          { "name": "booking_id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } },
          { "name": "Idempotency-Key", "in": "header", "schema": { "type": "string" } }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "reason": { "type": "string", "maxLength": 1000 },
                  "guest_email": { "type": "string", "description": "Required if not using API key auth" }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Booking canceled" },
          "409": { "description": "Booking already canceled" }
        }
      }
    },
    "/bookings/{booking_id}/reschedule": {
      "post": {
        "operationId": "rescheduleBooking",
        "summary": "Reschedule a booking",
        "tags": ["Bookings"],
        "parameters": [
          { "name": "booking_id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } },
          { "name": "Idempotency-Key", "in": "header", "schema": { "type": "string" } }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["new_slot_start"],
                "properties": {
                  "new_slot_start": { "type": "string", "format": "date-time" },
                  "timezone": { "type": "string", "default": "UTC" },
                  "guest_email": { "type": "string", "description": "Required if not using API key auth" }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Booking rescheduled" },
          "409": { "description": "Slot not available or booking not reschedulable" }
        }
      }
    }
  }
}
