{
    "openapi": "3.0.3",
    "info": {
        "title": "Up Integration - API Publica",
        "version": "1.1.0",
        "description": "API publica de estoque e enriquecimento OEM para parceiros externos."
    },
    "servers": [
        {
            "url": "http://localhost"
        }
    ],
    "tags": [
        {
            "name": "Estoque",
            "description": "Consultas de estoque por token"
        },
        {
            "name": "OEM",
            "description": "Busca e enriquecimento de codigos de montadora por token"
        }
    ],
    "paths": {
        "/api/v1/stock": {
            "get": {
                "tags": [
                    "Estoque"
                ],
                "summary": "Lista itens de estoque",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "parameters": [
                    {
                        "name": "sku",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "ean",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "updated_since",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "string",
                            "format": "date-time"
                        }
                    },
                    {
                        "name": "page",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "integer",
                            "minimum": 1
                        }
                    },
                    {
                        "name": "per_page",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "integer",
                            "minimum": 1,
                            "maximum": 200
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Lista de itens",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/components/schemas/StockItem"
                                            }
                                        },
                                        "meta": {
                                            "type": "object",
                                            "properties": {
                                                "current_page": {
                                                    "type": "integer"
                                                },
                                                "per_page": {
                                                    "type": "integer"
                                                },
                                                "total": {
                                                    "type": "integer"
                                                },
                                                "last_page": {
                                                    "type": "integer"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "401": {
                        "description": "Nao autorizado"
                    },
                    "422": {
                        "description": "Parametro invalido"
                    },
                    "429": {
                        "description": "Rate limit excedido"
                    }
                }
            }
        },
        "/api/v1/stock/{sku}": {
            "get": {
                "tags": [
                    "Estoque"
                ],
                "summary": "Busca item por SKU",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "parameters": [
                    {
                        "name": "sku",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Item encontrado",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "$ref": "#/components/schemas/StockItem"
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "401": {
                        "description": "Nao autorizado"
                    },
                    "404": {
                        "description": "Item nao encontrado"
                    },
                    "429": {
                        "description": "Rate limit excedido"
                    }
                }
            }
        },
        "/api/v1/oem/search": {
            "get": {
                "tags": [
                    "OEM"
                ],
                "summary": "Busca OEM por EAN ou SKU",
                "description": "Consulta providers configurados (cache, titulo, descricao, Mercado Livre) para o cliente do token. Requer scope oem:read quando o token tiver scopes definidos.",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "parameters": [
                    {
                        "name": "ean",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "sku",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Resultado da busca",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/OemSearchResponse"
                                }
                            }
                        }
                    },
                    "403": {
                        "description": "Token sem permissao oem:read"
                    },
                    "404": {
                        "description": "Integracao OEM nao configurada"
                    },
                    "422": {
                        "description": "Informe ean ou sku"
                    }
                }
            }
        },
        "/api/v1/oem/enrich": {
            "post": {
                "tags": [
                    "OEM"
                ],
                "summary": "Enfileira lote de enriquecimento OEM",
                "description": "Processa produtos sem OEM no snapshot do cliente. Requer scope oem:enrich quando o token tiver scopes definidos.",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "requestBody": {
                    "required": false,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "limit": {
                                        "type": "integer",
                                        "minimum": 1,
                                        "maximum": 500,
                                        "default": 500,
                                        "description": "Quantidade maxima de produtos sem OEM a processar"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "202": {
                        "description": "Enriquecimento enfileirado",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/OemEnrichResponse"
                                }
                            }
                        }
                    },
                    "403": {
                        "description": "Token sem permissao oem:enrich"
                    },
                    "404": {
                        "description": "Integracao OEM nao configurada"
                    }
                }
            }
        }
    },
    "components": {
        "securitySchemes": {
            "bearerAuth": {
                "type": "http",
                "scheme": "bearer",
                "bearerFormat": "Token"
            }
        },
        "schemas": {
            "StockItem": {
                "type": "object",
                "properties": {
                    "source_product_id": {
                        "type": "integer",
                        "example": 12345
                    },
                    "sku": {
                        "type": "string",
                        "nullable": true,
                        "example": "B123"
                    },
                    "ean": {
                        "type": "string",
                        "nullable": true,
                        "example": "7909501098"
                    },
                    "name": {
                        "type": "string",
                        "nullable": true,
                        "example": "ABSORVEDOR"
                    },
                    "brand": {
                        "type": "string",
                        "nullable": true,
                        "example": "RF"
                    },
                    "price": {
                        "type": "number",
                        "format": "float",
                        "nullable": true,
                        "example": 165.12
                    },
                    "quantity": {
                        "type": "integer",
                        "nullable": true,
                        "example": 5
                    },
                    "status": {
                        "type": "string",
                        "nullable": true,
                        "example": "publish"
                    },
                    "product_updated_at": {
                        "type": "string",
                        "format": "date-time",
                        "nullable": true
                    },
                    "snapshot_updated_at": {
                        "type": "string",
                        "format": "date-time",
                        "nullable": true
                    }
                }
            },
            "OemSearchResponse": {
                "type": "object",
                "properties": {
                    "success": {
                        "type": "boolean",
                        "example": true
                    },
                    "ean": {
                        "type": "string",
                        "nullable": true,
                        "example": "7909501027840"
                    },
                    "sku": {
                        "type": "string",
                        "nullable": true,
                        "example": "18253"
                    },
                    "oem": {
                        "type": "array",
                        "items": {
                            "type": "string"
                        },
                        "example": [
                            "8200123456"
                        ]
                    },
                    "fonte": {
                        "type": "string",
                        "nullable": true,
                        "example": "title_pattern"
                    },
                    "confianca": {
                        "type": "integer",
                        "example": 85
                    },
                    "from_cache": {
                        "type": "boolean",
                        "example": false
                    }
                }
            },
            "OemEnrichResponse": {
                "type": "object",
                "properties": {
                    "success": {
                        "type": "boolean",
                        "example": true
                    },
                    "run_id": {
                        "type": "integer",
                        "example": 12
                    },
                    "limit": {
                        "type": "integer",
                        "example": 500
                    },
                    "message": {
                        "type": "string",
                        "example": "Enriquecimento enfileirado. Consulte o historico OEM no painel."
                    }
                }
            }
        }
    }
}
