/api/patvisit:
  get:
    tags: [Patient Visit]
    summary: List patient visits
    security:
      - bearerAuth: []
    parameters:
      - name: InternalPID
        in: query
        schema:
          type: integer
        description: Filter by internal patient ID (exact match)
      - name: PVID
        in: query
        schema:
          type: string
        description: Filter by visit ID (partial match)
      - name: PatientID
        in: query
        schema:
          type: string
        description: Filter by patient ID (partial match)
      - name: PatientName
        in: query
        schema:
          type: string
        description: Search by patient name (searches in both first and last name)
      - name: CreateDateFrom
        in: query
        schema:
          type: string
          format: date-time
        description: Filter visits created on or after this date
      - name: CreateDateTo
        in: query
        schema:
          type: string
          format: date-time
        description: Filter visits created on or before this date
      - name: page
        in: query
        schema:
          type: integer
      - name: perPage
        in: query
        schema:
          type: integer
    responses:
      '200':
        description: List of patient visits
        content:
          application/json:
            schema:
              type: object
              properties:
                status:
                  type: string
                message:
                  type: string
                data:
                  type: array
                  items:
                    $ref: '../components/schemas/patient-visit.yaml#/PatientVisit'
                total:
                  type: integer
                  description: Total number of records
                page:
                  type: integer
                  description: Current page number
                per_page:
                  type: integer
                  description: Number of records per page

  post:
    tags: [Patient Visit]
    summary: Create patient visit
    description: |
      Creates a new patient visit. PVID is auto-generated with 'DV' prefix if not provided.
      Can optionally include PatDiag (diagnosis) and PatVisitADT (ADT information).
    security:
      - bearerAuth: []
    requestBody:
      required: true
      content:
        application/json:
          schema:
            type: object
            required:
              - InternalPID
            properties:
              PVID:
                type: string
                description: Visit ID (auto-generated with DV prefix if not provided)
              InternalPID:
                type: integer
                description: Patient ID (required)
              EpisodeID:
                type: string
                description: Episode identifier
              SiteID:
                type: integer
                description: Site reference
              PatDiag:
                type: object
                description: Optional diagnosis information
                properties:
                  DiagCode:
                    type: string
                  Diagnosis:
                    type: string
              PatVisitADT:
                type: object
                description: Optional ADT information
                properties:
                  ADTCode:
                    type: string
                    enum: [A01, A02, A03, A04, A08]
                  LocationID:
                    type: integer
                  AttDoc:
                    type: integer
                  RefDoc:
                    type: integer
                  AdmDoc:
                    type: integer
                  CnsDoc:
                    type: integer
    responses:
      '201':
        description: Visit created successfully
        content:
          application/json:
            schema:
              type: object
              properties:
                status:
                  type: string
                message:
                  type: string
                data:
                  type: object
                  properties:
                    PVID:
                      type: string
                    InternalPVID:
                      type: integer


  delete:
    tags: [Patient Visit]
    summary: Delete patient visit
    security:
      - bearerAuth: []
    responses:
      '200':
        description: Visit deleted successfully

/api/patvisit/{id}:
  get:
    tags: [Patient Visit]
    summary: Get visit by ID
    security:
      - bearerAuth: []
    parameters:
      - name: id
        in: path
        required: true
        schema:
          type: string
        description: PVID (visit identifier like DV00001)
    responses:
      '200':
        description: Visit details
        content:
          application/json:
            schema:
              type: object
              properties:
                status:
                  type: string
                message:
                  type: string
                data:
                  $ref: '../components/schemas/patient-visit.yaml#/PatientVisit'

  patch:
    tags: [Patient Visit]
    summary: Update patient visit
    description: |
      Updates an existing patient visit. InternalPVID is required.
      Can update main visit data, PatDiag, and add new PatVisitADT records.
    security:
      - bearerAuth: []
    parameters:
      - name: id
        in: path
        required: true
        schema:
          type: integer
        description: Internal visit ID (InternalPVID)
    requestBody:
      required: true
      content:
        application/json:
          schema:
            type: object
            properties:
              PVID:
                type: string
              InternalPID:
                type: integer
              EpisodeID:
                type: string
              SiteID:
                type: integer
              PatDiag:
                type: object
                description: Diagnosis information (will update if exists)
                properties:
                  DiagCode:
                    type: string
                  Diagnosis:
                    type: string
              PatVisitADT:
                type: array
                description: Array of ADT records to add (new records only)
                items:
                  type: object
                  properties:
                    ADTCode:
                      type: string
                      enum: [A01, A02, A03, A04, A08]
                    LocationID:
                      type: integer
                    AttDoc:
                      type: integer
                    RefDoc:
                      type: integer
                    AdmDoc:
                      type: integer
                    CnsDoc:
                      type: integer
                    sequence:
                      type: integer
                      description: Used for ordering multiple ADT records
    responses:
      '200':
        description: Visit updated successfully
        content:
          application/json:
            schema:
              type: object
              properties:
                status:
                  type: string
                message:
                  type: string
                data:
                  type: object
                  properties:
                    PVID:
                      type: string
                    InternalPVID:
                      type: integer

/api/patvisit/patient/{patientId}:
  get:
    tags: [Patient Visit]
    summary: Get visits by patient ID
    security:
      - bearerAuth: []
    parameters:
      - name: patientId
        in: path
        required: true
        schema:
          type: integer
        description: Internal Patient ID (InternalPID)
    responses:
      '200':
        description: Patient visits list
        content:
          application/json:
            schema:
              type: object
              properties:
                status:
                  type: string
                data:
                  type: array
                  items:
                    $ref: '../components/schemas/patient-visit.yaml#/PatientVisit'

/api/patvisitadt:
  post:
    tags: [Patient Visit]
    summary: Create ADT record
    description: Create a new Admission/Discharge/Transfer record
    security:
      - bearerAuth: []
    requestBody:
      required: true
      content:
        application/json:
          schema:
            $ref: '../components/schemas/patient-visit.yaml#/PatVisitADT'
    responses:
      '201':
        description: ADT record created successfully
        content:
          application/json:
            schema:
              $ref: '../components/schemas/common.yaml#/SuccessResponse'

  

  delete:
    tags: [Patient Visit]
    summary: Delete ADT visit (soft delete)
    security:
      - bearerAuth: []
    requestBody:
      required: true
      content:
        application/json:
          schema:
            type: object
            required:
              - PVADTID
            properties:
              PVADTID:
                type: integer
                description: ADT record ID to delete
    responses:
      '200':
        description: ADT visit deleted successfully

/api/patvisitadt/visit/{visitId}:
  get:
    tags: [Patient Visit]
    summary: Get ADT history by visit ID
    description: Retrieve the complete Admission/Discharge/Transfer history for a visit, including all locations and doctors
    security:
      - bearerAuth: []
    parameters:
      - name: visitId
        in: path
        required: true
        schema:
          type: integer
        description: Internal Visit ID (InternalPVID)
    responses:
      '200':
        description: ADT history retrieved successfully
        content:
          application/json:
            schema:
              type: object
              properties:
                status:
                  type: string
                  example: success
                message:
                  type: string
                  example: ADT history retrieved
                data:
                  type: array
                  items:
                    type: object
                    properties:
                      PVADTID:
                        type: integer
                      InternalPVID:
                        type: integer
                      ADTCode:
                        type: string
                        enum: [A01, A02, A03, A04, A08]
                      LocationID:
                        type: integer
                      LocationName:
                        type: string
                      AttDoc:
                        type: integer
                      AttDocFirstName:
                        type: string
                      AttDocLastName:
                        type: string
                      RefDoc:
                        type: integer
                      RefDocFirstName:
                        type: string
                      RefDocLastName:
                        type: string
                      AdmDoc:
                        type: integer
                      AdmDocFirstName:
                        type: string
                      AdmDocLastName:
                        type: string
                      CnsDoc:
                        type: integer
                      CnsDocFirstName:
                        type: string
                      CnsDocLastName:
                        type: string
                      CreateDate:
                        type: string
                        format: date-time
                      EndDate:
                        type: string
                        format: date-time
/api/patvisitadt/{id}:
  get:
    tags: [Patient Visit]
    summary: Get ADT record by ID
    description: Retrieve a single ADT record by its ID, including location and doctor details
    security:
      - bearerAuth: []
    parameters:
      - name: id
        in: path
        required: true
        schema:
          type: integer
        description: ADT record ID (PVADTID)
    responses:
      '200':
        description: ADT record retrieved successfully
        content:
          application/json:
            schema:
              type: object
              properties:
                status:
                  type: string
                  example: success
                message:
                  type: string
                  example: ADT record retrieved
                data:
                  type: object
                  properties:
                    PVADTID:
                      type: integer
                    InternalPVID:
                      type: integer
                    ADTCode:
                      type: string
                      enum: [A01, A02, A03, A04, A08]
                    LocationID:
                      type: integer
                    LocationName:
                      type: string
                    AttDoc:
                      type: integer
                    AttDocFirstName:
                      type: string
                    AttDocLastName:
                      type: string
                    RefDoc:
                      type: integer
                    RefDocFirstName:
                      type: string
                    RefDocLastName:
                      type: string
                    AdmDoc:
                      type: integer
                    AdmDocFirstName:
                      type: string
                    AdmDocLastName:
                      type: string
                    CnsDoc:
                      type: integer
                    CnsDocFirstName:
                      type: string
                    CnsDocLastName:
                      type: string
                    CreateDate:
                      type: string
                      format: date-time
                    EndDate:
                      type: string
                      format: date-time

  patch:
    tags: [Patient Visit]
    summary: Update ADT record
    description: Update an existing ADT record
    security:
      - bearerAuth: []
    parameters:
      - name: id
        in: path
        required: true
        schema:
          type: integer
        description: ADT record ID (PVADTID)
    requestBody:
      required: true
      content:
        application/json:
          schema:
            $ref: '../components/schemas/patient-visit.yaml#/PatVisitADT'
    responses:
      '200':
        description: ADT record updated successfully
        content:
          application/json:
            schema:
              $ref: '../components/schemas/common.yaml#/SuccessResponse'
