{
  "openapi": "3.0.3",
  "info": {
    "title": "StockFirme API de inventario",
    "version": "0.1.0",
    "description": "Contrato público de integración en piloto: catálogo, bodegas, existencias y movimientos. Autenticación mediante token de integración vinculado al negocio."
  },
  "servers": [
    {
      "url": "https://vzfqsdvljtgkwnrquapz.supabase.co/functions/v1/stockfirme-api"
    }
  ],
  "security": [
    {
      "integrationToken": []
    }
  ],
  "paths": {
    "/v1/products": {
      "get": {
        "operationId": "listar_productos",
        "summary": "Buscar y listar productos",
        "description": "Busca por SKU o código de barras exacto, o por coincidencia de nombre, SKU, código o categoría con q. Devuelve productos activos del negocio del token.",
        "security": [
          {
            "integrationToken": []
          }
        ],
        "x-required-permission": "products:read",
        "parameters": [
          {
            "name": "sku",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "barcode",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "q",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 500,
              "default": 100
            }
          },
          {
            "name": "offset",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 0,
              "maximum": 1000000,
              "default": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Operación correcta. El ejemplo muestra campos relevantes; la respuesta puede incluir metadatos adicionales.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                },
                "example": {
                  "items": [
                    {
                      "id": "11111111-1111-4111-a111-111111111111",
                      "name": "Producto de ejemplo",
                      "sku": "SF-DEMO-001",
                      "barcode": "0000000000017",
                      "cost": 1200,
                      "price": 1800,
                      "minimum_stock": 5
                    }
                  ],
                  "limit": 100,
                  "offset": 0
                }
              }
            }
          },
          "400": {
            "description": "Datos no válidos",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Token no válido, vencido o revocado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "Permiso insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Registro no encontrado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "409": {
            "description": "Conflicto, duplicado o stock insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "Demasiadas solicitudes",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Error temporal del servidor",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "crear_producto",
        "summary": "Crear un producto",
        "description": "Crea catálogo y, opcionalmente, stock inicial. Para quantity mayor que cero también necesitas inventory:write. El ejemplo crea el producto con stock cero.",
        "security": [
          {
            "integrationToken": []
          }
        ],
        "x-required-permission": "products:write",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200
            },
            "description": "Identificador estable del evento externo. No cambiar al reintentar."
          }
        ],
        "responses": {
          "200": {
            "description": "Operación correcta. El ejemplo muestra campos relevantes; la respuesta puede incluir metadatos adicionales.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                },
                "example": {
                  "success": true,
                  "id": "11111111-1111-4111-a111-111111111111"
                }
              }
            }
          },
          "400": {
            "description": "Datos no válidos",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Token no válido, vencido o revocado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "Permiso insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Registro no encontrado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "409": {
            "description": "Conflicto, duplicado o stock insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "Demasiadas solicitudes",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Error temporal del servidor",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "product",
                  "warehouse_id",
                  "operation_id"
                ],
                "properties": {
                  "product": {
                    "type": "object",
                    "additionalProperties": false,
                    "required": [
                      "name"
                    ],
                    "properties": {
                      "id": {
                        "type": "string",
                        "format": "uuid"
                      },
                      "name": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 200
                      },
                      "sku": {
                        "type": "string",
                        "nullable": true,
                        "maxLength": 80
                      },
                      "barcode": {
                        "type": "string",
                        "nullable": true,
                        "maxLength": 100
                      },
                      "category": {
                        "type": "string",
                        "maxLength": 120
                      },
                      "supplier": {
                        "type": "string",
                        "maxLength": 160
                      },
                      "cost": {
                        "type": "number",
                        "minimum": 0,
                        "maximum": 10000000000.0
                      },
                      "price": {
                        "type": "number",
                        "minimum": 0,
                        "maximum": 10000000000.0
                      },
                      "minimum_stock": {
                        "type": "number",
                        "minimum": 0,
                        "maximum": 10000000000.0,
                        "multipleOf": 0.001
                      }
                    }
                  },
                  "warehouse_id": {
                    "type": "string",
                    "format": "uuid"
                  },
                  "quantity": {
                    "type": "number",
                    "minimum": 0,
                    "maximum": 10000000000.0,
                    "multipleOf": 0.001
                  },
                  "operation_id": {
                    "type": "string",
                    "format": "uuid"
                  }
                }
              },
              "example": {
                "product": {
                  "id": "11111111-1111-4111-a111-111111111111",
                  "name": "Producto de ejemplo",
                  "sku": "SF-DEMO-001",
                  "barcode": "0000000000017",
                  "category": "Ejemplos",
                  "cost": 1200,
                  "price": 1800,
                  "minimum_stock": 5
                },
                "warehouse_id": "22222222-2222-4222-a222-222222222222",
                "quantity": 0,
                "operation_id": "33333333-3333-4333-a333-333333333333"
              }
            }
          }
        }
      }
    },
    "/v1/products/{id}": {
      "get": {
        "operationId": "ver_producto",
        "summary": "Consultar un producto",
        "description": "Usa el ID de StockFirme obtenido al buscar por SKU o código de barras. Los IDs del ERP pueden ser distintos.",
        "security": [
          {
            "integrationToken": []
          }
        ],
        "x-required-permission": "products:read",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Operación correcta. El ejemplo muestra campos relevantes; la respuesta puede incluir metadatos adicionales.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                },
                "example": {
                  "id": "11111111-1111-4111-a111-111111111111",
                  "name": "Producto de ejemplo",
                  "sku": "SF-DEMO-001",
                  "barcode": "0000000000017",
                  "category": "Ejemplos",
                  "cost": 1200,
                  "price": 1800,
                  "minimum_stock": 5
                }
              }
            }
          },
          "400": {
            "description": "Datos no válidos",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Token no válido, vencido o revocado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "Permiso insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Registro no encontrado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "409": {
            "description": "Conflicto, duplicado o stock insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "Demasiadas solicitudes",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Error temporal del servidor",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      },
      "patch": {
        "operationId": "editar_producto",
        "summary": "Editar el catálogo",
        "description": "Envía solo los campos que quieras cambiar: los omitidos se conservan. Esta ruta no modifica existencias. Define qué sistema controla cada campo para evitar sobrescribir cambios de otro sistema.",
        "security": [
          {
            "integrationToken": []
          }
        ],
        "x-required-permission": "products:write",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Operación correcta. El ejemplo muestra campos relevantes; la respuesta puede incluir metadatos adicionales.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                },
                "example": {
                  "id": "11111111-1111-4111-a111-111111111111",
                  "name": "Producto de ejemplo",
                  "sku": "SF-DEMO-001",
                  "price": 1900,
                  "category": "Ejemplos"
                }
              }
            }
          },
          "400": {
            "description": "Datos no válidos",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Token no válido, vencido o revocado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "Permiso insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Registro no encontrado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "409": {
            "description": "Conflicto, duplicado o stock insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "Demasiadas solicitudes",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Error temporal del servidor",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                  "name": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 200
                  },
                  "sku": {
                    "type": "string",
                    "nullable": true,
                    "maxLength": 80
                  },
                  "barcode": {
                    "type": "string",
                    "nullable": true,
                    "maxLength": 100
                  },
                  "category": {
                    "type": "string",
                    "maxLength": 120
                  },
                  "supplier": {
                    "type": "string",
                    "maxLength": 160
                  },
                  "cost": {
                    "type": "number",
                    "minimum": 0,
                    "maximum": 10000000000.0
                  },
                  "price": {
                    "type": "number",
                    "minimum": 0,
                    "maximum": 10000000000.0
                  },
                  "minimum_stock": {
                    "type": "number",
                    "minimum": 0,
                    "maximum": 10000000000.0,
                    "multipleOf": 0.001
                  }
                },
                "minProperties": 1
              },
              "example": {
                "price": 1900,
                "category": "Ejemplos"
              }
            }
          }
        }
      }
    },
    "/v1/warehouses": {
      "get": {
        "operationId": "listar_bodegas",
        "summary": "Consultar bodegas",
        "description": "Relaciona la bodega del ERP con su ID en StockFirme. No asumas que una bodega con otro nombre o ID es la misma.",
        "security": [
          {
            "integrationToken": []
          }
        ],
        "x-required-permission": "warehouses:read",
        "parameters": [],
        "responses": {
          "200": {
            "description": "Operación correcta. El ejemplo muestra campos relevantes; la respuesta puede incluir metadatos adicionales.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                },
                "example": {
                  "items": [
                    {
                      "id": "22222222-2222-4222-a222-222222222222",
                      "name": "Bodega de ejemplo",
                      "active": true
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Datos no válidos",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Token no válido, vencido o revocado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "Permiso insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Registro no encontrado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "409": {
            "description": "Conflicto, duplicado o stock insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "Demasiadas solicitudes",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Error temporal del servidor",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/v1/stock": {
      "get": {
        "operationId": "listar_stock",
        "summary": "Consultar stock por páginas",
        "description": "Cada fila corresponde a un producto en una bodega. quantity es el saldo actual; no sumes bodegas si necesitas disponibilidad en una ubicación concreta.",
        "security": [
          {
            "integrationToken": []
          }
        ],
        "x-required-permission": "inventory:read",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 500,
              "default": 100
            }
          },
          {
            "name": "offset",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 0,
              "maximum": 1000000,
              "default": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Operación correcta. El ejemplo muestra campos relevantes; la respuesta puede incluir metadatos adicionales.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                },
                "example": {
                  "items": [
                    {
                      "product_id": "11111111-1111-4111-a111-111111111111",
                      "warehouse_id": "22222222-2222-4222-a222-222222222222",
                      "name": "Producto de ejemplo",
                      "sku": "SF-DEMO-001",
                      "quantity": "30.000",
                      "version": 2
                    }
                  ],
                  "limit": 100,
                  "offset": 0
                }
              }
            }
          },
          "400": {
            "description": "Datos no válidos",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Token no válido, vencido o revocado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "Permiso insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Registro no encontrado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "409": {
            "description": "Conflicto, duplicado o stock insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "Demasiadas solicitudes",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Error temporal del servidor",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/v1/stock/{id}": {
      "get": {
        "operationId": "ver_stock",
        "summary": "Consultar stock de un producto",
        "description": "Consulta todas sus bodegas o filtra por warehouse_id. Una combinación sin movimientos puede no tener fila: items vacío no equivale a producto inexistente. Comprueba el producto y la bodega antes de interpretar un saldo como cero.",
        "security": [
          {
            "integrationToken": []
          }
        ],
        "x-required-permission": "inventory:read",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "warehouse_id",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Operación correcta. El ejemplo muestra campos relevantes; la respuesta puede incluir metadatos adicionales.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                },
                "example": {
                  "items": [
                    {
                      "product_id": "11111111-1111-4111-a111-111111111111",
                      "warehouse_id": "22222222-2222-4222-a222-222222222222",
                      "quantity": "30.000",
                      "version": 2
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Datos no válidos",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Token no válido, vencido o revocado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "Permiso insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Registro no encontrado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "409": {
            "description": "Conflicto, duplicado o stock insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "Demasiadas solicitudes",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Error temporal del servidor",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/v1/operations": {
      "post": {
        "operationId": "registrar_movimiento",
        "summary": "Registrar una entrada o salida",
        "description": "Registra el movimiento y su trazabilidad. Entrada: ENTRY y cantidad positiva. Venta o salida: EXIT y cantidad negativa. El servidor rechaza saldos negativos. También admite RETURN_IN, RETURN_OUT, ADJUSTMENT_PLUS y ADJUSTMENT_MINUS con el signo correspondiente.",
        "security": [
          {
            "integrationToken": []
          }
        ],
        "x-required-permission": "inventory:write",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200
            },
            "description": "Identificador estable del evento externo. No cambiar al reintentar."
          }
        ],
        "responses": {
          "200": {
            "description": "Operación correcta. El ejemplo muestra campos relevantes; la respuesta puede incluir metadatos adicionales.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                },
                "example": {
                  "success": true,
                  "product_id": "11111111-1111-4111-a111-111111111111",
                  "warehouse_id": "22222222-2222-4222-a222-222222222222",
                  "previous_stock": 32,
                  "new_stock": 30,
                  "quantity": -2
                }
              }
            }
          },
          "400": {
            "description": "Datos no válidos",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Token no válido, vencido o revocado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "Permiso insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Registro no encontrado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "409": {
            "description": "Conflicto, duplicado o stock insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "Demasiadas solicitudes",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Error temporal del servidor",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "product_id",
                  "warehouse_id",
                  "quantity",
                  "id",
                  "type",
                  "movement_type"
                ],
                "properties": {
                  "product_id": {
                    "type": "string",
                    "format": "uuid"
                  },
                  "warehouse_id": {
                    "type": "string",
                    "format": "uuid"
                  },
                  "quantity": {
                    "type": "number",
                    "minimum": -10000000000.0,
                    "maximum": 10000000000.0,
                    "multipleOf": 0.001
                  },
                  "reason": {
                    "type": "string",
                    "maxLength": 200
                  },
                  "reference": {
                    "type": "string",
                    "maxLength": 200
                  },
                  "id": {
                    "type": "string",
                    "format": "uuid"
                  },
                  "type": {
                    "type": "string",
                    "enum": [
                      "movement"
                    ]
                  },
                  "movement_type": {
                    "type": "string",
                    "enum": [
                      "ENTRY",
                      "EXIT",
                      "RETURN_IN",
                      "RETURN_OUT",
                      "ADJUSTMENT_PLUS",
                      "ADJUSTMENT_MINUS"
                    ]
                  },
                  "note": {
                    "type": "string",
                    "maxLength": 1000
                  }
                }
              },
              "examples": {
                "entrada": {
                  "summary": "Recepción de 32 unidades",
                  "value": {
                    "id": "44444444-4444-4444-a444-444444444444",
                    "type": "movement",
                    "product_id": "11111111-1111-4111-a111-111111111111",
                    "warehouse_id": "22222222-2222-4222-a222-222222222222",
                    "movement_type": "ENTRY",
                    "quantity": 32,
                    "reason": "Recepción de mercadería",
                    "reference": "RECEPCION-DEMO-001"
                  }
                },
                "salida": {
                  "summary": "Salida de 2 unidades por venta",
                  "value": {
                    "id": "55555555-5555-4555-a555-555555555555",
                    "type": "movement",
                    "product_id": "11111111-1111-4111-a111-111111111111",
                    "warehouse_id": "22222222-2222-4222-a222-222222222222",
                    "movement_type": "EXIT",
                    "quantity": -2,
                    "reason": "Venta desde ERP",
                    "reference": "VENTA-DEMO-001"
                  }
                }
              }
            }
          }
        }
      }
    },
    "/v1/inventory/adjustments": {
      "post": {
        "operationId": "ajustar_stock",
        "summary": "Aplicar una diferencia de stock",
        "description": "Suma o resta la diferencia indicada y deja historial de ajuste. Para representar una venta o devolución con su tipo propio, utiliza Registrar una entrada o salida.",
        "security": [
          {
            "integrationToken": []
          }
        ],
        "x-required-permission": "inventory:write",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200
            },
            "description": "Identificador estable del evento externo. No cambiar al reintentar."
          }
        ],
        "responses": {
          "200": {
            "description": "Operación correcta. El ejemplo muestra campos relevantes; la respuesta puede incluir metadatos adicionales.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                },
                "example": {
                  "success": true,
                  "product_id": "11111111-1111-4111-a111-111111111111",
                  "warehouse_id": "22222222-2222-4222-a222-222222222222",
                  "previous_stock": 30,
                  "new_stock": 31,
                  "quantity": 1
                }
              }
            }
          },
          "400": {
            "description": "Datos no válidos",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Token no válido, vencido o revocado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "Permiso insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Registro no encontrado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "409": {
            "description": "Conflicto, duplicado o stock insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "Demasiadas solicitudes",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Error temporal del servidor",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "product_id",
                  "warehouse_id",
                  "quantity"
                ],
                "properties": {
                  "product_id": {
                    "type": "string",
                    "format": "uuid"
                  },
                  "warehouse_id": {
                    "type": "string",
                    "format": "uuid"
                  },
                  "quantity": {
                    "type": "number",
                    "minimum": -10000000000.0,
                    "maximum": 10000000000.0,
                    "multipleOf": 0.001
                  },
                  "reason": {
                    "type": "string",
                    "maxLength": 200
                  },
                  "reference": {
                    "type": "string",
                    "maxLength": 200
                  }
                }
              },
              "example": {
                "product_id": "11111111-1111-4111-a111-111111111111",
                "warehouse_id": "22222222-2222-4222-a222-222222222222",
                "quantity": 1,
                "reason": "Diferencia confirmada de conteo",
                "reference": "AJUSTE-DEMO-001"
              }
            }
          }
        }
      }
    },
    "/v1/movements": {
      "get": {
        "operationId": "listar_movimientos",
        "summary": "Consultar el historial",
        "description": "Historial del negocio ordenado por recepción en servidor, del más reciente al más antiguo. Los filtros from/to se aplican a server_timestamp, ambos inclusive. Para conciliar un producto filtra por product_id en tu conector.",
        "security": [
          {
            "integrationToken": []
          }
        ],
        "x-required-permission": "movements:read",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 500,
              "default": 100
            }
          },
          {
            "name": "offset",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 0,
              "maximum": 1000000,
              "default": 0
            }
          },
          {
            "name": "from",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          },
          {
            "name": "to",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Operación correcta. El ejemplo muestra campos relevantes; la respuesta puede incluir metadatos adicionales.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                },
                "example": {
                  "items": [
                    {
                      "product_id": "11111111-1111-4111-a111-111111111111",
                      "warehouse_id": "22222222-2222-4222-a222-222222222222",
                      "movement_type": "EXIT",
                      "quantity": "-2.000",
                      "previous_quantity": "32.000",
                      "new_quantity": "30.000",
                      "reference": "VENTA-DEMO-001",
                      "source": "api"
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Datos no válidos",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Token no válido, vencido o revocado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "Permiso insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Registro no encontrado",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "409": {
            "description": "Conflicto, duplicado o stock insuficiente",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "Demasiadas solicitudes",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Error temporal del servidor",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "integrationToken": {
        "type": "http",
        "scheme": "bearer",
        "description": "Token privado creado en StockFirme → Más → Integraciones → API StockFirme. Los permisos se administran en la app."
      }
    }
  }
}
