openapi: 3.1.0
info:
  title: StayStyles API
  version: 1.0.0
  description: >
    Owner API v1 (API-key authenticated) and the keyless public booking
    API. See docs/API.md for the auth model, limits, and versioning
    policy. All dates are YYYY-MM-DD in the property's timezone; money
    is in dollars (major units).
servers:
  - url: https://greenvalley-api.kosmacek-kitchen.workers.dev
security: []
components:
  securitySchemes:
    ApiKey:
      type: http
      scheme: bearer
      description: 'Tenant API key: Authorization: Bearer ssk_live_… (Setup → Account → API access). Pro plan.'
  schemas:
    Error:
      type: object
      properties:
        error: { type: string }
      required: [error]
    Page:
      type: object
      properties:
        data: { type: array, items: {} }
        nextCursor: { type: [string, 'null'] }
    QuoteRequest:
      type: object
      properties:
        unitId: { type: string }
        checkIn: { type: string, format: date }
        checkOut: { type: string, format: date }
        guests: { type: integer, default: 1 }
        dogs: { type: integer, default: 0 }
        addonIds: { type: array, items: { type: string } }
        productAddons:
          type: array
          items:
            type: object
            properties:
              productId: { type: string }
              qty: { type: integer }
            required: [productId, qty]
      required: [unitId, checkIn, checkOut]
    Quote:
      type: object
      description: Price breakdown from the booking engine; field set mirrors the engine (nights, lodging, fees, dogFeeTotal, subtotal, tax, total, taxIncluded, dueNow, pricingNote, addons, products).
      properties:
        available: { type: boolean }
        unitId: { type: string }
        checkIn: { type: string, format: date }
        checkOut: { type: string, format: date }
        nights: { type: integer }
        total: { type: number }
        dueNow: { type: number }
        pricingNote: { type: string }
paths:
  /api/v1/tenant:
    get:
      summary: The property the API key belongs to
      security: [{ ApiKey: [] }]
      responses:
        '200': { description: Tenant summary incl. settings }
        '401': { description: Missing/invalid key, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
  /api/v1/units:
    get:
      summary: List units
      security: [{ ApiKey: [] }]
      parameters:
        - { name: limit, in: query, schema: { type: integer, default: 50, maximum: 200 } }
        - { name: cursor, in: query, schema: { type: string } }
      responses:
        '200': { description: Page of units, content: { application/json: { schema: { $ref: '#/components/schemas/Page' } } } }
  /api/v1/units/{id}:
    get:
      summary: One unit
      security: [{ ApiKey: [] }]
      parameters: [{ name: id, in: path, required: true, schema: { type: string } }]
      responses:
        '200': { description: Unit }
        '404': { description: Not found (including other tenants' units) }
  /api/v1/availability:
    get:
      summary: Per-night availability and price for one unit over [from, to)
      security: [{ ApiKey: [] }]
      parameters:
        - { name: unitId, in: query, required: true, schema: { type: string } }
        - { name: from, in: query, required: true, schema: { type: string, format: date } }
        - { name: to, in: query, required: true, schema: { type: string, format: date } }
      responses:
        '200': { description: '{ unitId, from, to, nights: [{ date, available, price }] }' }
  /api/v1/quote:
    post:
      summary: Exact engine quote for a stay (read scope; books nothing)
      security: [{ ApiKey: [] }]
      requestBody:
        required: true
        content: { application/json: { schema: { $ref: '#/components/schemas/QuoteRequest' } } }
      responses:
        '200': { description: Quote, content: { application/json: { schema: { $ref: '#/components/schemas/Quote' } } } }
  /api/v1/bookings:
    get:
      summary: 'List bookings (filters: status, unitId, from, to)'
      security: [{ ApiKey: [] }]
      parameters:
        - { name: status, in: query, schema: { type: string, enum: [pending_payment, confirmed, paid, canceled] } }
        - { name: unitId, in: query, schema: { type: string } }
        - { name: from, in: query, schema: { type: string, format: date } }
        - { name: to, in: query, schema: { type: string, format: date } }
        - { name: limit, in: query, schema: { type: integer, default: 50, maximum: 200 } }
        - { name: cursor, in: query, schema: { type: string } }
      responses:
        '200': { description: Page of bookings (Stripe ids stripped), content: { application/json: { schema: { $ref: '#/components/schemas/Page' } } } }
  /api/v1/bookings/{id}:
    get:
      summary: One booking
      security: [{ ApiKey: [] }]
      parameters: [{ name: id, in: path, required: true, schema: { type: string } }]
      responses:
        '200': { description: Booking }
        '404': { description: Not found }
  /api/v1/bookings/manual:
    post:
      summary: Create a manual (off-website) booking — write scope. Card payment links are refused here; use paymentMethod none/cash/check.
      security: [{ ApiKey: [] }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                unitId: { type: string }
                checkIn: { type: string, format: date }
                checkOut: { type: string, format: date }
                guestName: { type: string }
                email: { type: string }
                phone: { type: string }
                notes: { type: string }
                guests: { type: integer, default: 1 }
                dogs: { type: integer, default: 0 }
                addonIds: { type: array, items: { type: string } }
                paymentMethod: { type: string, enum: [none, cash, check], default: none }
                paymentAmount: { type: number }
              required: [unitId, checkIn, checkOut, guestName]
      responses:
        '200': { description: Created booking }
  /api/v1/bookings/{id}/cancel:
    post:
      summary: Plain-cancel a booking (frees dates, restores stock, emails guest + admins; moves no money) — write scope
      security: [{ ApiKey: [] }]
      parameters: [{ name: id, in: path, required: true, schema: { type: string } }]
      responses:
        '200': { description: '{ ok: true } or { ok: true, alreadyCanceled: true }' }
  /api/v1/blocks:
    get:
      summary: 'List calendar blocks (filters: unitId, from, to)'
      security: [{ ApiKey: [] }]
      parameters:
        - { name: unitId, in: query, schema: { type: string } }
        - { name: from, in: query, schema: { type: string, format: date } }
        - { name: to, in: query, schema: { type: string, format: date } }
        - { name: limit, in: query, schema: { type: integer, default: 50, maximum: 200 } }
        - { name: cursor, in: query, schema: { type: string } }
      responses:
        '200': { description: Page of blocks, content: { application/json: { schema: { $ref: '#/components/schemas/Page' } } } }
    post:
      summary: Create a calendar block — write scope
      security: [{ ApiKey: [] }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                unitId: { type: string }
                start: { type: string, format: date }
                end: { type: string, format: date, description: Day after the last blocked night }
                summary: { type: string }
                quantity: { type: integer }
              required: [unitId, start, end]
      responses:
        '200': { description: Created block }
  /api/v1/blocks/{id}:
    delete:
      summary: Delete a calendar block — write scope
      security: [{ ApiKey: [] }]
      parameters: [{ name: id, in: path, required: true, schema: { type: string } }]
      responses:
        '200': { description: '{ ok: true }' }
  /api/v1/guests:
    get:
      summary: List guest records
      security: [{ ApiKey: [] }]
      parameters:
        - { name: limit, in: query, schema: { type: integer, default: 50, maximum: 200 } }
        - { name: cursor, in: query, schema: { type: string } }
      responses:
        '200': { description: Page of guests, content: { application/json: { schema: { $ref: '#/components/schemas/Page' } } } }
  /api/v1/products:
    get:
      summary: List store products
      security: [{ ApiKey: [] }]
      parameters:
        - { name: limit, in: query, schema: { type: integer, default: 50, maximum: 200 } }
        - { name: cursor, in: query, schema: { type: string } }
      responses:
        '200': { description: Page of products, content: { application/json: { schema: { $ref: '#/components/schemas/Page' } } } }
  /api/v1/reports/summary:
    get:
      summary: Occupancy and revenue summary for a window (paid/confirmed bookings; revenue prorated)
      security: [{ ApiKey: [] }]
      parameters:
        - { name: from, in: query, required: true, schema: { type: string, format: date } }
        - { name: to, in: query, required: true, schema: { type: string, format: date } }
      responses:
        '200': { description: Summary }
  /public/property:
    get:
      summary: Public property profile (branding, policies, bookable units) — keyless, CORS-open
      parameters: [{ name: tenant, in: query, required: true, schema: { type: string }, description: Property slug }]
      responses:
        '200': { description: Property profile }
        '403': { description: Public API is a Pro feature for this property }
        '503': { description: Booking unavailable (billing lapsed) }
  /public/search:
    post:
      summary: Availability + exact engine quotes across the property's units — keyless
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                tenant: { type: string }
                unitId: { type: string, description: Optional — limit to one unit }
                checkIn: { type: string, format: date }
                checkOut: { type: string, format: date }
                guests: { type: integer, default: 1 }
              required: [tenant, checkIn, checkOut]
      responses:
        '200': { description: '{ tenant, checkIn, checkOut, guests, results: [{ unitId, name, available, ...quote }] }' }
  /public/checkout:
    post:
      summary: Start a booking; returns the Stripe-hosted checkoutUrl on the property's own Stripe account — keyless
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                tenant: { type: string }
                unitId: { type: string }
                checkIn: { type: string, format: date }
                checkOut: { type: string, format: date }
                guests: { type: integer, default: 1 }
                guestName: { type: string }
                email: { type: string, format: email }
                phone: { type: string }
                notes: { type: string }
              required: [tenant, unitId, checkIn, checkOut, guestName, email]
      responses:
        '200': { description: '{ checkoutUrl, url, bookingId }' }
  /public/booking-status:
    get:
      summary: Booking status with the guest email as proof — keyless
      parameters:
        - { name: tenant, in: query, required: true, schema: { type: string } }
        - { name: bookingId, in: query, required: true, schema: { type: string } }
        - { name: email, in: query, required: true, schema: { type: string, format: email } }
      responses:
        '200': { description: Minimal booking summary (id, status, unit, dates, guests, total, balanceDue) }
        '404': { description: Not found (or email does not match) }
