openapi: 3.0.3
info:
  title: Reservations Service
  description: |
    Manages adventure trip reservations for NovaTrek Adventures.
    Handles booking creation, modification, cancellation, and status tracking.
  version: 2.4.1
  contact:
    name: NovaTrek Platform Team
    email: platform@novatrek.example.com

servers:
  - url: https://api.novatrek.example.com/reservations/v2

tags:
  - name: Reservations
    description: CRUD operations for trip reservations
  - name: Reservation Status
    description: Status tracking and transitions
  - name: Reservation Search
    description: Search and filtering operations

paths:
  /reservations:
    get:
      tags: [Reservation Search]
      summary: Search reservations
      description: Returns a paginated list of reservations matching the given criteria.
      operationId: searchReservations
      parameters:
        - name: guest_id
          in: query
          schema:
            type: string
            format: uuid
          description: Filter by guest ID
        - name: trip_id
          in: query
          schema:
            type: string
            format: uuid
          description: Filter by trip/adventure ID
        - name: status
          in: query
          schema:
            $ref: '#/components/schemas/ReservationStatus'
          description: Filter by reservation status
        - name: start_date
          in: query
          schema:
            type: string
            format: date
          description: Filter reservations starting on or after this date
        - name: end_date
          in: query
          schema:
            type: string
            format: date
          description: Filter reservations starting on or before this date
        - name: page
          in: query
          schema:
            type: integer
            default: 0
        - name: size
          in: query
          schema:
            type: integer
            default: 20
            maximum: 100
      responses:
        '200':
          description: Paginated list of reservations
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ReservationPage'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '500':
          $ref: '#/components/responses/InternalServerError'

    post:
      tags: [Reservations]
      summary: Create a new reservation
      description: |
        Creates a new adventure reservation. Validates guest eligibility,
        trip availability, and gear requirements before confirming.
      operationId: createReservation
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateReservationRequest'
      responses:
        '201':
          description: Reservation created successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Reservation'
        '400':
          $ref: '#/components/responses/BadRequest'
        '409':
          description: Conflict - trip fully booked or guest has overlapping reservation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '422':
          description: Guest does not meet eligibility requirements for this trip
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EligibilityErrorResponse'

  /reservations/{reservation_id}:
    parameters:
      - $ref: '#/components/parameters/ReservationId'

    get:
      tags: [Reservations]
      summary: Get reservation details
      operationId: getReservation
      responses:
        '200':
          description: Reservation details
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Reservation'
        '404':
          $ref: '#/components/responses/NotFound'

    patch:
      tags: [Reservations]
      summary: Update a reservation
      description: |
        Partially updates a reservation. Only modifiable fields can be changed.
        Status transitions are validated against the reservation state machine.
      operationId: updateReservation
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UpdateReservationRequest'
      responses:
        '200':
          description: Reservation updated successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Reservation'
        '400':
          $ref: '#/components/responses/BadRequest'
        '404':
          $ref: '#/components/responses/NotFound'
        '409':
          description: Revision conflict
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

    delete:
      tags: [Reservations]
      summary: Cancel a reservation
      operationId: cancelReservation
      parameters:
        - name: reason
          in: query
          required: true
          schema:
            $ref: '#/components/schemas/CancellationReason'
      responses:
        '200':
          description: Reservation cancelled
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Reservation'
        '404':
          $ref: '#/components/responses/NotFound'
        '409':
          description: Reservation cannot be cancelled in its current state
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  /reservations/{reservation_id}/participants:
    parameters:
      - $ref: '#/components/parameters/ReservationId'

    get:
      tags: [Reservations]
      summary: Get reservation participants
      operationId: getParticipants
      responses:
        '200':
          description: List of participants
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Participant'
        '404':
          $ref: '#/components/responses/NotFound'

    post:
      tags: [Reservations]
      summary: Add a participant to a reservation
      operationId: addParticipant
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AddParticipantRequest'
      responses:
        '201':
          description: Participant added
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Participant'
        '400':
          $ref: '#/components/responses/BadRequest'
        '404':
          $ref: '#/components/responses/NotFound'
        '409':
          description: Trip at maximum capacity
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  /reservations/{reservation_id}/status:
    parameters:
      - $ref: '#/components/parameters/ReservationId'

    put:
      tags: [Reservation Status]
      summary: Transition reservation status
      description: |
        Explicitly transitions a reservation to a new status.
        Validates that the transition is allowed from the current state.
      operationId: transitionStatus
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/StatusTransitionRequest'
      responses:
        '200':
          description: Status transitioned
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Reservation'
        '400':
          description: Invalid status transition
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '404':
          $ref: '#/components/responses/NotFound'

components:
  parameters:
    ReservationId:
      name: reservation_id
      in: path
      required: true
      schema:
        type: string
        format: uuid
      description: Unique reservation identifier

  schemas:
    Reservation:
      type: object
      required: [id, guest_id, trip_id, status, created_at, _rev]
      properties:
        id:
          type: string
          format: uuid
        guest_id:
          type: string
          format: uuid
        trip_id:
          type: string
          format: uuid
        status:
          $ref: '#/components/schemas/ReservationStatus'
        booking_source:
          $ref: '#/components/schemas/BookingSource'
        participants:
          type: array
          items:
            $ref: '#/components/schemas/Participant'
        gear_package_id:
          type: string
          format: uuid
          description: Selected gear rental package (from svc-gear-inventory)
        special_requirements:
          type: string
          maxLength: 500
        emergency_contact:
          $ref: '#/components/schemas/EmergencyContact'
        payment_reference:
          type: string
          description: Reference to payment in svc-payments
        total_amount:
          type: number
          format: decimal
        currency:
          type: string
          default: USD
        created_at:
          type: string
          format: date-time
        updated_at:
          type: string
          format: date-time
        _rev:
          type: string
          description: Document revision for optimistic concurrency

    ReservationStatus:
      type: string
      enum:
        - PENDING
        - CONFIRMED
        - GEAR_ASSIGNED
        - CHECKED_IN
        - IN_PROGRESS
        - COMPLETED
        - CANCELLED
        - NO_SHOW

    BookingSource:
      type: string
      enum:
        - WEB_DIRECT
        - MOBILE_APP
        - PARTNER_API
        - CALL_CENTER
        - WALK_IN
        - TRAVEL_AGENT

    CancellationReason:
      type: string
      enum:
        - GUEST_REQUEST
        - WEATHER
        - GUIDE_UNAVAILABLE
        - MINIMUM_NOT_MET
        - SAFETY_CONCERN
        - DUPLICATE

    Participant:
      type: object
      required: [guest_id, role]
      properties:
        guest_id:
          type: string
          format: uuid
        role:
          type: string
          enum: [PRIMARY, COMPANION, MINOR]
        waiver_signed:
          type: boolean
          default: false
        medical_clearance:
          type: boolean
          description: Required for high-difficulty adventures
        gear_assignment_id:
          type: string
          format: uuid
          description: Individual gear assignment from svc-gear-inventory
        checked_in:
          type: boolean
          default: false
        check_in_time:
          type: string
          format: date-time

    EmergencyContact:
      type: object
      required: [name, phone]
      properties:
        name:
          type: string
        phone:
          type: string
        relationship:
          type: string

    CreateReservationRequest:
      type: object
      required: [guest_id, trip_id, booking_source]
      properties:
        guest_id:
          type: string
          format: uuid
        trip_id:
          type: string
          format: uuid
        booking_source:
          $ref: '#/components/schemas/BookingSource'
        participants:
          type: array
          items:
            $ref: '#/components/schemas/AddParticipantRequest'
        gear_package_id:
          type: string
          format: uuid
        special_requirements:
          type: string
          maxLength: 500
        emergency_contact:
          $ref: '#/components/schemas/EmergencyContact'
        promo_code:
          type: string

    UpdateReservationRequest:
      type: object
      properties:
        gear_package_id:
          type: string
          format: uuid
        special_requirements:
          type: string
          maxLength: 500
        emergency_contact:
          $ref: '#/components/schemas/EmergencyContact'
        _rev:
          type: string
          description: Required for optimistic concurrency

    AddParticipantRequest:
      type: object
      required: [guest_id, role]
      properties:
        guest_id:
          type: string
          format: uuid
        role:
          type: string
          enum: [PRIMARY, COMPANION, MINOR]
        medical_clearance:
          type: boolean

    StatusTransitionRequest:
      type: object
      required: [target_status, _rev]
      properties:
        target_status:
          $ref: '#/components/schemas/ReservationStatus'
        reason:
          type: string
        _rev:
          type: string

    ReservationPage:
      type: object
      properties:
        content:
          type: array
          items:
            $ref: '#/components/schemas/Reservation'
        page:
          type: integer
        size:
          type: integer
        total_elements:
          type: integer
        total_pages:
          type: integer

    ErrorResponse:
      type: object
      properties:
        error:
          type: string
        message:
          type: string
        timestamp:
          type: string
          format: date-time
        trace_id:
          type: string

    EligibilityErrorResponse:
      allOf:
        - $ref: '#/components/schemas/ErrorResponse'
        - type: object
          properties:
            failed_requirements:
              type: array
              items:
                type: object
                properties:
                  requirement:
                    type: string
                  detail:
                    type: string

  responses:
    BadRequest:
      description: Bad request
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    Unauthorized:
      description: Unauthorized
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    NotFound:
      description: Resource not found
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    InternalServerError:
      description: Internal server error
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
