openapi: 3.0.4
info:
  title: Evolution Foundation - CRM Service - Pipeline Item Products
  description: |
    Links products (with optional variant, quantity and notes) to pipeline
    items — used by the "Venda" panel inside a pipeline item to track which
    catalog product is being negotiated.

    Available starting `v1.0.0-rc3`. The AI agent can also create these
    links automatically via the native tool `link_product_to_pipeline_item`.

    **Note:** This OpenAPI spec was generated from the controller as part of the rc3 release. Schemas reflect the controller surface but request/response examples may need refinement based on real payloads.
  version: 1.0.0
servers:
- url: https://api.evoai.app
- url: http://localhost:3000
paths:
  /api/v1/pipeline_items/{pipeline_item_id}/products:
    parameters:
    - name: pipeline_item_id
      in: path
      required: true
      schema: { type: string, format: uuid }
      description: ID of the pipeline item (the opportunity)
    get:
      operationId: list-pipeline-item-products
      summary: List Products on Pipeline Item
      description: Lists all products linked to the pipeline item. Returns the product and its variant (when present), plus locked price/currency and free-text notes. Requires `pipelines.read`.
      security:
      - userApiKey: []
      responses:
        '200':
          description: Pipeline item products retrieved successfully
          content:
            application/json:
              schema:
                type: object
                required: [success, data, meta, message]
                properties:
                  success: { type: boolean, example: true }
                  data:
                    type: array
                    items: { $ref: '#/components/schemas/PipelineItemProduct' }
                  meta:
                    type: object
                    properties:
                      timestamp: { type: string, format: date-time }
                  message: { type: string }
        '404':
          $ref: '#/components/responses/NotFound'
    post:
      operationId: link-product-to-pipeline-item
      summary: Link Product
      description: |
        Links a product (and optionally a specific variant) to the pipeline
        item. The price and currency are **frozen** at link time and cannot
        be changed via update — subsequent edits only touch quantity/notes.

        Requires `pipelines.update`.
      security:
      - userApiKey: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [product_id]
              properties:
                product_id:
                  type: string
                  format: uuid
                  description: ID of the catalog product
                product_variant_id:
                  type: string
                  format: uuid
                  description: ID of a specific variant (optional)
                quantity:
                  type: integer
                  default: 1
                  description: Quantity being negotiated
                notes:
                  type: string
                  description: Free-text notes about this line of the deal
      responses:
        '201':
          description: Product linked successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PipelineItemProductResponse'
        '422':
          $ref: '#/components/responses/ValidationError'

  /api/v1/pipeline_items/{pipeline_item_id}/products/{id}:
    parameters:
    - name: pipeline_item_id
      in: path
      required: true
      schema: { type: string, format: uuid }
    - name: id
      in: path
      required: true
      schema: { type: string, format: uuid }
      description: ID of the link (not of the product itself)
    patch:
      operationId: update-pipeline-item-product
      summary: Update Linked Product
      description: |
        Updates **mutable** attributes of the link: `quantity` and `notes`.
        Locked price/currency stay frozen and cannot be changed here — to
        replace the price, unlink and link again.

        Requires `pipelines.update`.
      security:
      - userApiKey: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                quantity: { type: integer, minimum: 1 }
                notes: { type: string }
      responses:
        '200':
          description: Pipeline item product updated successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PipelineItemProductResponse'
        '422':
          $ref: '#/components/responses/ValidationError'
    delete:
      operationId: unlink-product-from-pipeline-item
      summary: Unlink Product
      description: Removes the product link from the pipeline item. Requires `pipelines.update`.
      security:
      - userApiKey: []
      responses:
        '200':
          description: Pipeline item product removed successfully

components:
  securitySchemes:
    userApiKey:
      type: apiKey
      in: header
      name: api_access_token
  schemas:
    PipelineItemProduct:
      type: object
      properties:
        id: { type: string, format: uuid, description: "ID of the link" }
        pipeline_item_id: { type: string, format: uuid }
        product_id: { type: string, format: uuid }
        product_variant_id: { type: string, format: uuid, nullable: true }
        product:
          type: object
          properties:
            id: { type: string, format: uuid }
            name: { type: string }
            slug: { type: string }
        product_variant:
          type: object
          nullable: true
          properties:
            id: { type: string, format: uuid }
            name: { type: string }
            sku: { type: string }
        quantity: { type: integer }
        notes: { type: string, nullable: true }
        locked_price: { type: number, format: float, description: "Price at the moment of linking — frozen" }
        locked_currency: { type: string, description: "Currency at the moment of linking — frozen" }
        actor_id: { type: string, format: uuid, nullable: true, description: "User or agent that created/last updated the link" }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
    PipelineItemProductResponse:
      type: object
      required: [success, data, meta, message]
      properties:
        success: { type: boolean, example: true }
        data: { $ref: '#/components/schemas/PipelineItemProduct' }
        meta:
          type: object
          properties:
            timestamp: { type: string, format: date-time }
        message: { type: string }
  responses:
    NotFound:
      description: Resource not found
      content:
        application/json:
          schema:
            type: object
            properties:
              success: { type: boolean, example: false }
              message: { type: string }
    ValidationError:
      description: Validation failed
      content:
        application/json:
          schema:
            type: object
            properties:
              success: { type: boolean, example: false }
              message: { type: string }
              details:
                type: array
                items: { type: string }
