openapi: 3.1.0
info:
  title: Renzy Plumber Booking API
  description: |
    A minimal API for AI assistants to check plumber availability and create bookings.

    ## Typical flow (2 calls)
    1. `GET /api/availability?postcode=SW1A1AA` — check if the postcode is covered and get available slots.
    2. `POST /api/booking` — book one of the returned slots with customer details.

    ## Coverage
    London, UK only. The availability endpoint will tell you if a postcode is unsupported.

    ## Pricing
    - Weekday mornings/afternoons: £120/hr
    - Weekday evenings: £180/hr
    - Saturdays: £180/hr
    - Sundays: £240/hr

    Payment is collected when the job is done — nothing is charged upfront.
  version: "1.0.0"
  contact:
    name: Renzy
    url: https://renzy.co.uk
    email: support@renzy.co.uk

servers:
  - url: https://renzy.co.uk
    description: Production

paths:
  /api/availability:
    get:
      operationId: getAvailability
      summary: Get available booking slots
      description: |
        Returns bookable time slots for the next N days. Always call this first to get
        valid `slot_id` values before creating a booking.

        Slots are filtered to only include those still within the booking cutoff window:
        - Morning (8 AM–12 PM): must book by 9 PM the night before
        - Afternoon (12 PM–5 PM): must book by 10 AM same day
        - Evening (5 PM–9 PM): must book by 7 PM same day (weekdays only)
      parameters:
        - name: postcode
          in: query
          required: true
          description: UK postcode for the job location. Used to verify coverage area.
          schema:
            type: string
            example: SW1A1AA
        - name: days
          in: query
          required: false
          description: Number of days to return slots for (1–21, default 7).
          schema:
            type: integer
            minimum: 1
            maximum: 21
            default: 7
      responses:
        "200":
          description: Availability result (check `supported` before using `slots`)
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: "#/components/schemas/AvailabilitySupported"
                  - $ref: "#/components/schemas/AvailabilityUnsupported"
              examples:
                supported:
                  summary: Postcode is covered
                  value:
                    supported: true
                    slots:
                      - id: "2026-03-23_morning"
                        date: "2026-03-23"
                        day: "Tomorrow"
                        slot: "morning"
                        time: "8 AM – 12 PM"
                        rate: 120
                        currency: "GBP"
                      - id: "2026-03-23_afternoon"
                        date: "2026-03-23"
                        day: "Tomorrow"
                        slot: "afternoon"
                        time: "12 PM – 5 PM"
                        rate: 120
                        currency: "GBP"
                      - id: "2026-03-24_morning"
                        date: "2026-03-24"
                        day: "Wednesday"
                        slot: "morning"
                        time: "8 AM – 12 PM"
                        rate: 120
                        currency: "GBP"
                unsupported:
                  summary: Postcode is not covered
                  value:
                    supported: false
                    message: "We don't currently cover this area. Please call us on 07984 631995 or WhatsApp us."
                    phone: "+447984631995"
        "400":
          description: Missing or invalid query parameters
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
              example:
                error: "postcode is required"

  /api/booking:
    post:
      operationId: createBooking
      summary: Create a booking
      description: |
        Books a plumber for the specified slot. Use a `slot_id` from `GET /api/availability`.

        On success a Slack notification is sent to the team and a booking reference is returned.
        Payment is collected on the day — nothing is charged at this step.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/BookingRequest"
            example:
              slot_id: "2026-03-23_morning"
              name: "Jane Smith"
              phone: "07911 123456"
              address:
                line1: "42 Victoria Street"
                line2: ""
                town: "London"
                postcode: "SW1H 0NW"
              job_description: "Boiler not firing — no hot water or heating since yesterday."
      responses:
        "201":
          description: Booking confirmed
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/BookingConfirmation"
              example:
                confirmed: true
                booking_reference: "BK-2026-03-23-MORNING-M5X2K"
                slot:
                  id: "2026-03-23_morning"
                  date: "2026-03-23"
                  slot: "morning"
                  time: "8 AM – 12 PM"
                  full_label: "Monday 23rd March (8 AM – 12 PM)"
                rate: 120
                currency: "GBP"
                message: "Your booking is confirmed. We'll call you the evening before to confirm your appointment."
        "400":
          description: Validation error (missing/invalid fields)
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
              example:
                error: "job_description is required"
        "409":
          description: Slot no longer available (past cutoff or already taken)
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
              example:
                error: "This slot is no longer available. Please call GET /api/availability to see current availability."
        "422":
          description: Postcode not in a supported area
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
              example:
                error: "Postcode is not in a supported area"
        "500":
          description: Internal error (e.g. Slack notification failed)
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"

components:
  schemas:
    AvailabilitySupported:
      type: object
      required: [supported, slots]
      properties:
        supported:
          type: boolean
          enum: [true]
        slots:
          type: array
          items:
            $ref: "#/components/schemas/Slot"

    AvailabilityUnsupported:
      type: object
      required: [supported, message, phone]
      properties:
        supported:
          type: boolean
          enum: [false]
        message:
          type: string
        phone:
          type: string
          example: "+447984631995"

    Slot:
      type: object
      required: [id, date, day, slot, time, rate, currency]
      properties:
        id:
          type: string
          description: Opaque slot identifier — pass this directly to POST /api/booking.
          pattern: "^\\d{4}-\\d{2}-\\d{2}_(morning|afternoon|evening)$"
          example: "2026-03-23_morning"
        date:
          type: string
          format: date
          description: ISO 8601 date (London timezone).
          example: "2026-03-23"
        day:
          type: string
          description: Human-readable day label ("Today", "Tomorrow", "Wednesday", "Friday 27th", etc.)
          example: "Tomorrow"
        slot:
          type: string
          enum: [morning, afternoon, evening]
        time:
          type: string
          description: Human-readable time window.
          example: "8 AM – 12 PM"
        rate:
          type: integer
          description: Hourly rate in GBP (first hour).
          example: 120
        currency:
          type: string
          enum: [GBP]

    BookingRequest:
      type: object
      required: [slot_id, name, phone, address, job_description]
      properties:
        slot_id:
          type: string
          description: A `slot.id` value from GET /api/availability.
          pattern: "^\\d{4}-\\d{2}-\\d{2}_(morning|afternoon|evening)$"
          example: "2026-03-23_morning"
        name:
          type: string
          description: Customer full name.
          example: "Jane Smith"
        phone:
          type: string
          description: UK phone number (any format). Used for WhatsApp/call-back.
          example: "07911 123456"
        address:
          $ref: "#/components/schemas/Address"
        job_description:
          type: string
          description: Description of the plumbing job. Be specific — helps the engineer prepare.
          example: "Boiler not firing — no hot water or heating since yesterday."

    Address:
      type: object
      required: [line1, postcode]
      properties:
        line1:
          type: string
          example: "42 Victoria Street"
        line2:
          type: string
          example: ""
        town:
          type: string
          example: "London"
        postcode:
          type: string
          description: Full UK postcode. Must be within the supported London coverage area.
          example: "SW1H 0NW"
        summary:
          type: string
          description: Optional pre-formatted single-line address for display/maps. Auto-generated if omitted.
          example: "42 Victoria Street, London, SW1H 0NW"

    BookingConfirmation:
      type: object
      required: [confirmed, booking_reference, slot, rate, currency, message]
      properties:
        confirmed:
          type: boolean
          enum: [true]
        booking_reference:
          type: string
          description: Unique reference for this booking. Share with the customer.
          example: "BK-2026-03-23-MORNING-M5X2K"
        slot:
          type: object
          required: [id, date, slot, time, full_label]
          properties:
            id:
              type: string
              example: "2026-03-23_morning"
            date:
              type: string
              format: date
              example: "2026-03-23"
            slot:
              type: string
              enum: [morning, afternoon, evening]
            time:
              type: string
              example: "8 AM – 12 PM"
            full_label:
              type: string
              description: Full human-readable slot description for display to the customer.
              example: "Monday 23rd March (8 AM – 12 PM)"
        rate:
          type: integer
          description: Confirmed hourly rate in GBP.
          example: 120
        currency:
          type: string
          enum: [GBP]
        message:
          type: string
          description: Confirmation message to show the customer.
          example: "Your booking is confirmed. We'll call you the evening before to confirm your appointment."

    Error:
      type: object
      required: [error]
      properties:
        error:
          type: string
