/api/ordertest:
  get:
    tags: [Order]
    summary: List orders
    security:
      - bearerAuth: []
    parameters:
      - name: page
        in: query
        schema:
          type: integer
      - name: perPage
        in: query
        schema:
          type: integer
      - name: InternalPID
        in: query
        schema:
          type: integer
        description: Filter by internal patient ID
      - name: OrderStatus
        in: query
        schema:
          type: string
          enum: [ORD, SCH, ANA, VER, REV, REP]
        description: |
          ORD: Ordered
          SCH: Scheduled
          ANA: Analysis
          VER: Verified
          REV: Reviewed
          REP: Reported
    responses:
      '200':
        description: List of orders
        content:
          application/json:
            schema:
              type: object
              properties:
                status:
                  type: string
                message:
                  type: string
                data:
                  type: array
                  items:
                    $ref: '../components/schemas/orders.yaml#/OrderTestList'

  post:
    tags: [Order]
    summary: Create order with specimens and tests
    description: Creates an order with associated specimens and patres records. Tests are grouped by container type to minimize specimen creation.
    security:
      - bearerAuth: []
    requestBody:
      required: true
      content:
        application/json:
          schema:
            type: object
            required:
              - InternalPID
              - Tests
            properties:
              OrderID:
                type: string
                description: Optional custom order ID (auto-generated if not provided)
              InternalPID:
                type: integer
                description: Patient internal ID
              PatVisitID:
                type: integer
                description: Visit ID
              SiteID:
                type: integer
                default: 1
              PlacerID:
                type: string
              Priority:
                type: string
                enum: [R, S, U]
                default: R
                description: |
                  R: Routine
                  S: Stat
                  U: Urgent
              ReqApp:
                type: string
                description: Requesting application
              Comment:
                type: string
              Tests:
                type: array
                items:
                  type: object
                  required:
                    - TestSiteID
                  properties:
                    TestSiteID:
                      type: integer
                      description: Test definition site ID
                    TestID:
                      type: integer
                      description: Alias for TestSiteID
    responses:
      '201':
        description: Order created successfully with specimens and tests
        content:
          application/json:
            schema:
              type: object
              properties:
                status:
                  type: string
                  example: success
                message:
                  type: string
                data:
                  $ref: '../components/schemas/orders.yaml#/OrderTest'
      '400':
        description: Validation error
      '500':
        description: Server error


  delete:
    tags: [Order]
    summary: Delete order
    security:
      - bearerAuth: []
    requestBody:
      required: true
      content:
        application/json:
          schema:
            type: object
            required:
              - OrderID
            properties:
              OrderID:
                type: string
    responses:
      '200':
        description: Order deleted

/api/ordertest/status:
  post:
    tags: [Order]
    summary: Update order status
    security:
      - bearerAuth: []
    requestBody:
      required: true
      content:
        application/json:
          schema:
            type: object
            required:
              - OrderID
              - OrderStatus
            properties:
              OrderID:
                type: string
              OrderStatus:
                type: string
                enum: [ORD, SCH, ANA, VER, REV, REP]
                description: |
                  ORD: Ordered
                  SCH: Scheduled
                  ANA: Analysis
                  VER: Verified
                  REV: Reviewed
                  REP: Reported
    responses:
      '200':
        description: Order status updated
        content:
          application/json:
            schema:
              type: object
              properties:
                status:
                  type: string
                message:
                  type: string
                data:
                  $ref: '../components/schemas/orders.yaml#/OrderTest'

/api/ordertest/{id}:
  get:
    tags: [Order]
    summary: Get order by ID
    description: Returns order details with associated specimens and tests
    security:
      - bearerAuth: []
    parameters:
      - name: id
        in: path
        required: true
        schema:
          type: string
        description: Order ID (e.g., 0025030300001)
    responses:
      '200':
        description: Order details with specimens and tests
        content:
          application/json:
            schema:
              type: object
              properties:
                status:
                  type: string
                message:
                  type: string
                data:
                  $ref: '../components/schemas/orders.yaml#/OrderTest'

  patch:
    tags: [Order]
    summary: Update order
    security:
      - bearerAuth: []
    parameters:
      - name: id
        in: path
        required: true
        schema:
          type: string
        description: Order ID
    requestBody:
      required: true
      content:
        application/json:
          schema:
            type: object
            properties:
              Priority:
                type: string
                enum: [R, S, U]
              OrderStatus:
                type: string
                enum: [ORD, SCH, ANA, VER, REV, REP]
              OrderingProvider:
                type: string
              DepartmentID:
                type: integer
              WorkstationID:
                type: integer
    responses:
      '200':
        description: Order updated
        content:
          application/json:
            schema:
              type: object
              properties:
                status:
                  type: string
                message:
                  type: string
                data:
                  $ref: '../components/schemas/orders.yaml#/OrderTest'
