openapi: 3.0.0
info:
  title: OCR Invoice Job API
  description: API for managing individual OCR invoice jobs
  version: 1.0.0
  contact:
    name: Repzo
    url: https://repzo.com

servers:
  - url: https://sv.api.repzo.me
    description: Production server
  - url: https://staging.sv.api.repzo.me
    description: Staging server

tags:
  - name: OCR Invoice Job
    description: Operations related to individual OCR invoice job management

paths:
  /ocr-invoice-job:
    get:
      tags:
        - OCR Invoice Job
      summary: Get all OCR invoice jobs
      description: Retrieve a list of all OCR invoice jobs with optional filtering and pagination
      parameters:
        - name: limit
          in: query
          description: Maximum number of records to return
          required: false
          schema:
            type: integer
            minimum: 1
            maximum: 100
            default: 50
        - name: offset
          in: query
          description: Number of records to skip for pagination
          required: false
          schema:
            type: integer
            minimum: 0
            default: 0
        - name: sort
          in: query
          description: Field to sort by
          required: false
          schema:
            type: string
        - name: order
          in: query
          description: Sort order (asc or desc)
          required: false
          schema:
            type: string
            enum: [asc, desc]
            default: desc
        - name: filter
          in: query
          description: Filter conditions in JSON format
          required: false
          schema:
            type: string
        - name: search
          in: query
          description: Search term for text fields
          required: false
          schema:
            type: string
      responses:
        "200":
          description: List of OCR invoice jobs retrieved successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    type: array
                    items:
                      $ref: "#/components/schemas/OcrInvoiceJob"
                  total:
                    type: integer
                    description: Total number of records
        "400":
          description: Bad request
        "401":
          description: Unauthorized
        "500":
          description: Internal server error
    post:
      tags:
        - OCR Invoice Job
      summary: Create a new OCR invoice job
      description: Create a new OCR invoice job with the provided data
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/OcrInvoiceJobInput"
      responses:
        "201":
          description: OCR invoice job created successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    $ref: "#/components/schemas/OcrInvoiceJob"
        "400":
          description: Bad request
        "401":
          description: Unauthorized
        "500":
          description: Internal server error

  /ocr-invoice-job/{id}:
    get:
      tags:
        - OCR Invoice Job
      summary: Get OCR invoice job by ID
      description: Retrieve a specific OCR invoice job by its ID
      parameters:
        - name: id
          in: path
          required: true
          description: OCR invoice job ID
          schema:
            type: string
      responses:
        "200":
          description: OCR invoice job retrieved successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    $ref: "#/components/schemas/OcrInvoiceJob"
        "404":
          description: OCR invoice job not found
        "401":
          description: Unauthorized
        "500":
          description: Internal server error
    put:
      tags:
        - OCR Invoice Job
      summary: Update OCR invoice job
      description: Update an existing OCR invoice job with new data
      parameters:
        - name: id
          in: path
          required: true
          description: OCR invoice job ID
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/OcrInvoiceJobInput"
      responses:
        "200":
          description: OCR invoice job updated successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    $ref: "#/components/schemas/OcrInvoiceJob"
        "400":
          description: Bad request
        "404":
          description: OCR invoice job not found
        "401":
          description: Unauthorized
        "500":
          description: Internal server error
    delete:
      tags:
        - OCR Invoice Job
      summary: Delete OCR invoice job
      description: Delete an existing OCR invoice job
      parameters:
        - name: id
          in: path
          required: true
          description: OCR invoice job ID
          schema:
            type: string
      responses:
        "200":
          description: OCR invoice job deleted successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  message:
                    type: string
                    example: OCR invoice job deleted successfully
        "404":
          description: OCR invoice job not found
        "401":
          description: Unauthorized
        "500":
          description: Internal server error

  /ocr-invoice-job/{id}/process:
    post:
      tags:
        - OCR Invoice Job
      summary: Process OCR invoice job
      description: Start processing an OCR invoice job
      parameters:
        - name: id
          in: path
          required: true
          description: OCR invoice job ID
          schema:
            type: string
      responses:
        "200":
          description: OCR invoice job processing started
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    type: object
                    properties:
                      jobId:
                        type: string
                      status:
                        type: string
                        enum: [queued, processing]
        "404":
          description: OCR invoice job not found
        "401":
          description: Unauthorized
        "500":
          description: Internal server error

  /ocr-invoice-job/{id}/results:
    get:
      tags:
        - OCR Invoice Job
      summary: Get OCR results
      description: Retrieve the OCR processing results for a job
      parameters:
        - name: id
          in: path
          required: true
          description: OCR invoice job ID
          schema:
            type: string
      responses:
        "200":
          description: OCR results retrieved successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    $ref: "#/components/schemas/OcrResults"
        "404":
          description: OCR invoice job not found
        "401":
          description: Unauthorized
        "500":
          description: Internal server error

components:
  schemas:
    OcrInvoiceJob:
      type: object
      properties:
        _id:
          type: string
          description: Unique identifier for the OCR invoice job
          example: "60f7b1b3e4b0e8b3f8b3f8b3"
        jobNumber:
          type: string
          description: Unique job number
          example: "OCR-2023-001"
        documentName:
          type: string
          description: Name of the document being processed
          example: "invoice_2023_001.pdf"
        documentUrl:
          type: string
          format: uri
          description: URL of the document to process
          example: "https://storage.example.com/invoices/invoice_2023_001.pdf"
        documentType:
          type: string
          enum: [pdf, png, jpg, jpeg, tiff, bmp]
          description: Type of document
          example: "pdf"
        template:
          type: string
          description: OCR template ID to use
          example: "60f7b1b3e4b0e8b3f8b3f8b4"
        group:
          type: string
          description: OCR job group ID if part of a batch
          example: "60f7b1b3e4b0e8b3f8b3f8b5"
        status:
          type: string
          enum: [created, queued, processing, completed, failed, cancelled]
          default: created
          description: Processing status
        priority:
          type: string
          enum: [low, medium, high, urgent]
          default: medium
          description: Processing priority
        progress:
          type: object
          properties:
            percentage:
              type: number
              format: double
              minimum: 0
              maximum: 100
              description: Processing progress percentage
            currentStep:
              type: string
              description: Current processing step
            totalSteps:
              type: integer
              description: Total number of processing steps
        startedAt:
          type: string
          format: date-time
          description: When processing started
          example: "2023-01-15T08:00:00Z"
        completedAt:
          type: string
          format: date-time
          description: When processing completed
          example: "2023-01-15T08:05:00Z"
        processingTime:
          type: number
          format: double
          description: Processing time in seconds
          example: 45.6
        documentInfo:
          type: object
          properties:
            pages:
              type: integer
              description: Number of pages in document
            size:
              type: integer
              description: File size in bytes
            format:
              type: string
              description: Document format
            resolution:
              type: string
              description: Document resolution
            language:
              type: string
              description: Detected language
        extractedData:
          type: object
          properties:
            invoice:
              type: object
              properties:
                number:
                  type: string
                  description: Invoice number
                date:
                  type: string
                  format: date
                  description: Invoice date
                dueDate:
                  type: string
                  format: date
                  description: Due date
                supplier:
                  type: object
                  properties:
                    name:
                      type: string
                    address:
                      type: string
                    taxId:
                      type: string
                customer:
                  type: object
                  properties:
                    name:
                      type: string
                    address:
                      type: string
                amounts:
                  type: object
                  properties:
                    subtotal:
                      type: number
                      format: double
                    tax:
                      type: number
                      format: double
                    total:
                      type: number
                      format: double
                    currency:
                      type: string
            lineItems:
              type: array
              items:
                type: object
                properties:
                  description:
                    type: string
                  quantity:
                    type: number
                    format: double
                  unitPrice:
                    type: number
                    format: double
                  amount:
                    type: number
                    format: double
        confidence:
          type: object
          properties:
            overall:
              type: number
              format: double
              minimum: 0
              maximum: 1
              description: Overall confidence score
            fields:
              type: object
              additionalProperties:
                type: number
                format: double
              description: Confidence per field
        validationResults:
          type: object
          properties:
            isValid:
              type: boolean
              description: Whether validation passed
            errors:
              type: array
              items:
                type: object
                properties:
                  field:
                    type: string
                  message:
                    type: string
                  severity:
                    type: string
                    enum: [error, warning, info]
        cost:
          type: number
          format: double
          minimum: 0
          description: Processing cost
        currency:
          type: string
          description: Currency code
          example: "USD"
        errorLog:
          type: string
          description: Error log if processing failed
        retryCount:
          type: integer
          minimum: 0
          description: Number of retry attempts
        createdBy:
          type: string
          description: User who created the job
          example: "60f7b1b3e4b0e8b3f8b3f8b6"
        metadata:
          type: object
          additionalProperties: true
          description: Additional metadata
        createdAt:
          type: string
          format: date-time
          description: Record creation timestamp
          example: "2023-01-15T08:00:00Z"
        updatedAt:
          type: string
          format: date-time
          description: Record last update timestamp
          example: "2023-01-15T10:30:00Z"
      required:
        - jobNumber
        - documentName
        - documentUrl
        - template

    OcrInvoiceJobInput:
      type: object
      properties:
        jobNumber:
          type: string
          description: Unique job number
          example: "OCR-2023-001"
        documentName:
          type: string
          description: Name of the document being processed
          example: "invoice_2023_001.pdf"
        documentUrl:
          type: string
          format: uri
          description: URL of the document to process
          example: "https://storage.example.com/invoices/invoice_2023_001.pdf"
        documentType:
          type: string
          enum: [pdf, png, jpg, jpeg, tiff, bmp]
          description: Type of document
          example: "pdf"
        template:
          type: string
          description: OCR template ID to use
          example: "60f7b1b3e4b0e8b3f8b3f8b4"
        group:
          type: string
          description: OCR job group ID if part of a batch
          example: "60f7b1b3e4b0e8b3f8b3f8b5"
        priority:
          type: string
          enum: [low, medium, high, urgent]
          default: medium
          description: Processing priority
        createdBy:
          type: string
          description: User who created the job
          example: "60f7b1b3e4b0e8b3f8b3f8b6"
        metadata:
          type: object
          additionalProperties: true
          description: Additional metadata
      required:
        - jobNumber
        - documentName
        - documentUrl
        - template

    OcrResults:
      type: object
      properties:
        extractedData:
          type: object
          description: All extracted data from the document
        confidence:
          type: object
          description: Confidence scores for extracted data
        validationResults:
          type: object
          description: Validation results
        processingTime:
          type: number
          format: double
          description: Total processing time

  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: api-key

security:
  - ApiKeyAuth: []
