openapi: 3.0.0
info:
  title: Contract Installment API
  description: API for managing contract installments
  version: 1.0.0
servers:
  - url: https://api.repzo.me/v1
    description: Production server
security:
  - ApiKeyAuth: []
tags:
  - name: Contract Installment
    description: Contract installment management operations
paths:
  /contract-installment:
    get:
      tags:
        - Contract Installment
      summary: Get all contract installments
      description: Retrieve a list of all contract installments with optional filtering and pagination
      parameters:
        - name: page
          in: query
          description: Page number for pagination
          required: false
          schema:
            type: integer
            minimum: 1
            default: 1
        - name: per_page
          in: query
          description: Number of items per page
          required: false
          schema:
            type: integer
            minimum: 1
            maximum: 100
            default: 25
        - name: sort
          in: query
          description: Sort field
          required: false
          schema:
            type: string
        - name: company_namespace
          in: query
          description: Company namespace for filtering
          required: false
          schema:
            type: array
            items:
              type: string
        - name: contract
          in: query
          description: Filter by contract ID
          required: false
          schema:
            type: string
        - name: status
          in: query
          description: Filter by payment status
          required: false
          schema:
            type: string
            enum: ["pending", "paid", "overdue", "cancelled"]
        - name: due_date_from
          in: query
          description: Filter by due date from
          required: false
          schema:
            type: string
            format: date
        - name: due_date_to
          in: query
          description: Filter by due date to
          required: false
          schema:
            type: string
            format: date
        - name: _id
          in: query
          description: Filter by ID
          required: false
          schema:
            type: array
            items:
              type: string
      responses:
        "200":
          description: Successful response
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    type: array
                    items:
                      $ref: "#/components/schemas/ContractInstallment"
                  paging:
                    $ref: "#/components/schemas/PagingInfo"
        "400":
          $ref: "#/components/responses/BadRequest"
        "401":
          $ref: "#/components/responses/Unauthorized"
        "403":
          $ref: "#/components/responses/Forbidden"
        "500":
          $ref: "#/components/responses/InternalServerError"
    post:
      tags:
        - Contract Installment
      summary: Create a new contract installment
      description: Create a new contract installment
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/CreateContractInstallmentRequest"
      responses:
        "201":
          description: Contract installment created successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    $ref: "#/components/schemas/ContractInstallment"
        "400":
          $ref: "#/components/responses/BadRequest"
        "401":
          $ref: "#/components/responses/Unauthorized"
        "403":
          $ref: "#/components/responses/Forbidden"
        "422":
          $ref: "#/components/responses/ValidationError"
        "500":
          $ref: "#/components/responses/InternalServerError"
  /contract-installment/{id}:
    get:
      tags:
        - Contract Installment
      summary: Get contract installment by ID
      description: Retrieve a specific contract installment by its ID
      parameters:
        - name: id
          in: path
          required: true
          description: Contract installment ID
          schema:
            type: string
      responses:
        "200":
          description: Successful response
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    $ref: "#/components/schemas/ContractInstallment"
        "400":
          $ref: "#/components/responses/BadRequest"
        "401":
          $ref: "#/components/responses/Unauthorized"
        "403":
          $ref: "#/components/responses/Forbidden"
        "404":
          $ref: "#/components/responses/NotFound"
        "500":
          $ref: "#/components/responses/InternalServerError"
    patch:
      tags:
        - Contract Installment
      summary: Update contract installment
      description: Update an existing contract installment
      parameters:
        - name: id
          in: path
          required: true
          description: Contract installment ID
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/UpdateContractInstallmentRequest"
      responses:
        "200":
          description: Contract installment updated successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    $ref: "#/components/schemas/ContractInstallment"
        "400":
          $ref: "#/components/responses/BadRequest"
        "401":
          $ref: "#/components/responses/Unauthorized"
        "403":
          $ref: "#/components/responses/Forbidden"
        "404":
          $ref: "#/components/responses/NotFound"
        "422":
          $ref: "#/components/responses/ValidationError"
        "500":
          $ref: "#/components/responses/InternalServerError"
    delete:
      tags:
        - Contract Installment
      summary: Delete contract installment
      description: Delete a contract installment by ID
      parameters:
        - name: id
          in: path
          required: true
          description: Contract installment ID
          schema:
            type: string
      responses:
        "200":
          description: Contract installment deleted successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
        "400":
          $ref: "#/components/responses/BadRequest"
        "401":
          $ref: "#/components/responses/Unauthorized"
        "403":
          $ref: "#/components/responses/Forbidden"
        "404":
          $ref: "#/components/responses/NotFound"
        "500":
          $ref: "#/components/responses/InternalServerError"
  /contract-installment/{id}/pay:
    post:
      tags:
        - Contract Installment
      summary: Mark installment as paid
      description: Mark a contract installment as paid
      parameters:
        - name: id
          in: path
          required: true
          description: Contract installment ID
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                payment_date:
                  type: string
                  format: date
                  description: Date of payment
                payment_amount:
                  type: number
                  format: float
                  description: Amount paid
                payment_method:
                  type: string
                  description: Payment method used
                  enum:
                    ["cash", "check", "credit_card", "bank_transfer", "other"]
                payment_reference:
                  type: string
                  description: Payment reference number
                notes:
                  type: string
                  description: Payment notes
              required:
                - payment_date
                - payment_amount
      responses:
        "200":
          description: Installment marked as paid successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    $ref: "#/components/schemas/ContractInstallment"
        "400":
          $ref: "#/components/responses/BadRequest"
        "401":
          $ref: "#/components/responses/Unauthorized"
        "403":
          $ref: "#/components/responses/Forbidden"
        "404":
          $ref: "#/components/responses/NotFound"
        "500":
          $ref: "#/components/responses/InternalServerError"
  /contract-installment/overdue:
    get:
      tags:
        - Contract Installment
      summary: Get overdue installments
      description: Get all overdue contract installments
      parameters:
        - name: page
          in: query
          description: Page number for pagination
          required: false
          schema:
            type: integer
            minimum: 1
            default: 1
        - name: per_page
          in: query
          description: Number of items per page
          required: false
          schema:
            type: integer
            minimum: 1
            maximum: 100
            default: 25
        - name: days_overdue
          in: query
          description: Minimum days overdue
          required: false
          schema:
            type: integer
            minimum: 0
      responses:
        "200":
          description: Overdue installments retrieved successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    type: array
                    items:
                      allOf:
                        - $ref: "#/components/schemas/ContractInstallment"
                        - type: object
                          properties:
                            days_overdue:
                              type: integer
                              description: Number of days overdue
                  paging:
                    $ref: "#/components/schemas/PagingInfo"
        "400":
          $ref: "#/components/responses/BadRequest"
        "401":
          $ref: "#/components/responses/Unauthorized"
        "403":
          $ref: "#/components/responses/Forbidden"
        "500":
          $ref: "#/components/responses/InternalServerError"
components:
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: Authorization
      description: "Use format: Bearer {api_key}"
  schemas:
    ContractInstallment:
      type: object
      properties:
        _id:
          type: string
          description: Unique identifier
          example: "507f1f77bcf86cd799439011"
        contract:
          type: string
          description: Contract ID reference
          example: "507f1f77bcf86cd799439012"
        installment_number:
          type: integer
          description: Installment sequence number
          example: 1
        due_date:
          type: string
          format: date
          description: Due date for this installment
          example: "2024-01-15"
        amount:
          type: number
          format: float
          description: Installment amount
          example: 1500.00
        currency:
          type: string
          description: Currency code
          example: "USD"
        status:
          type: string
          description: Payment status
          enum: ["pending", "paid", "overdue", "cancelled"]
          example: "pending"
        payment_date:
          type: string
          format: date
          description: Date when payment was made (if paid)
        payment_amount:
          type: number
          format: float
          description: Amount actually paid
        payment_method:
          type: string
          description: Payment method used
          enum: ["cash", "check", "credit_card", "bank_transfer", "other"]
        payment_reference:
          type: string
          description: Payment reference number
        late_fee:
          type: number
          format: float
          description: Late fee charged if overdue
          example: 50.00
        discount_applied:
          type: number
          format: float
          description: Discount applied to this installment
        interest_rate:
          type: number
          format: float
          description: Interest rate for this installment
          example: 5.5
        grace_period_days:
          type: integer
          description: Grace period in days before marking overdue
          example: 5
        reminder_sent:
          type: boolean
          description: Whether payment reminder has been sent
          example: false
        reminder_dates:
          type: array
          items:
            type: string
            format: date-time
          description: Dates when reminders were sent
        auto_pay_enabled:
          type: boolean
          description: Whether auto-pay is enabled for this installment
          example: false
        payment_history:
          type: array
          items:
            type: object
            properties:
              date:
                type: string
                format: date-time
              amount:
                type: number
                format: float
              method:
                type: string
              reference:
                type: string
              notes:
                type: string
          description: History of payment attempts
        notes:
          type: string
          description: Additional notes
        custom_fields:
          type: object
          description: Custom fields for additional data
        company_namespace:
          type: array
          items:
            type: string
          description: Company namespace
        createdAt:
          type: string
          format: date-time
          description: Creation timestamp
        modifiedAt:
          type: string
          format: date-time
          description: Last modification timestamp
        SVClient:
          type: integer
          description: Client version
        __v:
          type: integer
          description: Document version
      required:
        - contract
        - installment_number
        - due_date
        - amount
        - status
    CreateContractInstallmentRequest:
      type: object
      properties:
        contract:
          type: string
          description: Contract ID reference
          example: "507f1f77bcf86cd799439012"
        installment_number:
          type: integer
          description: Installment sequence number
          example: 1
        due_date:
          type: string
          format: date
          description: Due date for this installment
          example: "2024-01-15"
        amount:
          type: number
          format: float
          description: Installment amount
          example: 1500.00
        currency:
          type: string
          description: Currency code
          default: "USD"
        late_fee:
          type: number
          format: float
          description: Late fee charged if overdue
          default: 0
        discount_applied:
          type: number
          format: float
          description: Discount applied to this installment
          default: 0
        interest_rate:
          type: number
          format: float
          description: Interest rate for this installment
          default: 0
        grace_period_days:
          type: integer
          description: Grace period in days before marking overdue
          default: 0
        auto_pay_enabled:
          type: boolean
          description: Whether auto-pay is enabled for this installment
          default: false
        notes:
          type: string
          description: Additional notes
        custom_fields:
          type: object
          description: Custom fields for additional data
        company_namespace:
          type: array
          items:
            type: string
          description: Company namespace
      required:
        - contract
        - installment_number
        - due_date
        - amount
    UpdateContractInstallmentRequest:
      type: object
      properties:
        due_date:
          type: string
          format: date
          description: Due date for this installment
        amount:
          type: number
          format: float
          description: Installment amount
        status:
          type: string
          description: Payment status
          enum: ["pending", "paid", "overdue", "cancelled"]
        payment_date:
          type: string
          format: date
          description: Date when payment was made
        payment_amount:
          type: number
          format: float
          description: Amount actually paid
        payment_method:
          type: string
          description: Payment method used
          enum: ["cash", "check", "credit_card", "bank_transfer", "other"]
        payment_reference:
          type: string
          description: Payment reference number
        late_fee:
          type: number
          format: float
          description: Late fee charged if overdue
        discount_applied:
          type: number
          format: float
          description: Discount applied to this installment
        interest_rate:
          type: number
          format: float
          description: Interest rate for this installment
        grace_period_days:
          type: integer
          description: Grace period in days before marking overdue
        auto_pay_enabled:
          type: boolean
          description: Whether auto-pay is enabled for this installment
        notes:
          type: string
          description: Additional notes
        custom_fields:
          type: object
          description: Custom fields for additional data
        company_namespace:
          type: array
          items:
            type: string
          description: Company namespace
    PagingInfo:
      type: object
      properties:
        total:
          type: integer
          description: Total number of items
        page:
          type: integer
          description: Current page number
        per_page:
          type: integer
          description: Items per page
        pages:
          type: integer
          description: Total number of pages
  responses:
    BadRequest:
      description: Bad request
      content:
        application/json:
          schema:
            type: object
            properties:
              success:
                type: boolean
                example: false
              error:
                type: string
                example: "Invalid request parameters"
    Unauthorized:
      description: Unauthorized
      content:
        application/json:
          schema:
            type: object
            properties:
              success:
                type: boolean
                example: false
              error:
                type: string
                example: "Authentication required"
    Forbidden:
      description: Forbidden
      content:
        application/json:
          schema:
            type: object
            properties:
              success:
                type: boolean
                example: false
              error:
                type: string
                example: "Insufficient permissions"
    NotFound:
      description: Resource not found
      content:
        application/json:
          schema:
            type: object
            properties:
              success:
                type: boolean
                example: false
              error:
                type: string
                example: "Resource not found"
    ValidationError:
      description: Validation error
      content:
        application/json:
          schema:
            type: object
            properties:
              success:
                type: boolean
                example: false
              error:
                type: string
                example: "Validation failed"
    InternalServerError:
      description: Internal server error
      content:
        application/json:
          schema:
            type: object
            properties:
              success:
                type: boolean
                example: false
              error:
                type: string
                example: "Internal server error"
