{
    "openapi": "3.0.3",
    "info": {
        "title": "Sistema Modular Datsch API",
        "version": "1.0.0",
        "description": "Documentação das APIs de vistorias, pendências, Bitrix e planos."
    },
    "servers": [
        {
            "url": "http://vistorias.unicooper.com.br/index.php?route=",
            "description": "Ambiente atual"
        }
    ],
    "components": {
        "securitySchemes": {
            "bearerAuth": {
                "type": "http",
                "scheme": "bearer",
                "bearerFormat": "Token"
            }
        },
        "schemas": {
            "ApiSuccess": {
                "type": "object",
                "properties": {
                    "success": {
                        "type": "boolean",
                        "example": true
                    },
                    "message": {
                        "type": "string",
                        "example": "Sucesso"
                    },
                    "data": {
                        "type": "object"
                    }
                }
            },
            "ApiError": {
                "type": "object",
                "properties": {
                    "error": {
                        "type": "string",
                        "example": "Erro de validação"
                    }
                }
            },
            "WebhookVistoriaInput": {
                "type": "object",
                "required": [
                    "bitrix_card_id"
                ],
                "properties": {
                    "bitrix_card_id": {
                        "type": "string",
                        "example": "12345"
                    },
                    "template_id": {
                        "type": "integer",
                        "example": 1
                    }
                }
            },
            "CriarVistoriaInput": {
                "type": "object",
                "required": [
                    "template_id"
                ],
                "properties": {
                    "template_id": {
                        "type": "integer",
                        "example": 1
                    },
                    "referencia": {
                        "type": "string",
                        "example": "Cliente 123"
                    }
                }
            },
            "PendenciaMotivoInput": {
                "type": "object",
                "required": [
                    "nome"
                ],
                "properties": {
                    "nome": {
                        "type": "string",
                        "example": "Foto desfocada"
                    },
                    "descricao": {
                        "type": "string",
                        "nullable": true,
                        "example": "A imagem precisa ser refeita"
                    },
                    "ordem": {
                        "type": "integer",
                        "example": 1
                    },
                    "ativo": {
                        "type": "boolean",
                        "example": true
                    }
                }
            },
            "PlanoConsultaInput": {
                "type": "object",
                "required": [
                    "valor_autopropelido"
                ],
                "properties": {
                    "valor_autopropelido": {
                        "type": "number",
                        "format": "float",
                        "example": 35000
                    }
                }
            },
            "PlanoInput": {
                "type": "object",
                "required": [
                    "nome",
                    "valor_minimo",
                    "valor_maximo"
                ],
                "properties": {
                    "nome": {
                        "type": "string",
                        "example": "Plano Ouro"
                    },
                    "descricao": {
                        "type": "string",
                        "nullable": true
                    },
                    "valor_minimo": {
                        "type": "number",
                        "format": "float",
                        "example": 0
                    },
                    "valor_maximo": {
                        "type": "number",
                        "format": "float",
                        "example": 100000
                    },
                    "ordem": {
                        "type": "integer",
                        "example": 0
                    },
                    "ativo": {
                        "type": "boolean",
                        "example": true
                    }
                }
            },
            "ProdutoPlanoInput": {
                "type": "object",
                "required": [
                    "nome",
                    "valor"
                ],
                "properties": {
                    "nome": {
                        "type": "string",
                        "example": "Guincho"
                    },
                    "descricao": {
                        "type": "string",
                        "nullable": true
                    },
                    "valor": {
                        "type": "number",
                        "format": "float",
                        "example": 19.9
                    },
                    "tipo": {
                        "type": "string",
                        "example": "fixo"
                    },
                    "ordem": {
                        "type": "integer",
                        "example": 0
                    },
                    "ativo": {
                        "type": "boolean",
                        "example": true
                    }
                }
            },
            "RegraPlanoInput": {
                "type": "object",
                "required": [
                    "nome",
                    "tipo_condicao",
                    "acao"
                ],
                "properties": {
                    "nome": {
                        "type": "string",
                        "example": "Desconto especial"
                    },
                    "tipo_condicao": {
                        "type": "string",
                        "example": "valor_maior"
                    },
                    "valor_condicao_min": {
                        "type": "number",
                        "format": "float",
                        "example": 20000
                    },
                    "valor_condicao_max": {
                        "type": "number",
                        "format": "float",
                        "nullable": true
                    },
                    "acao": {
                        "type": "string",
                        "example": "aplicar_desconto"
                    },
                    "parametro_acao": {
                        "type": "object",
                        "additionalProperties": true
                    },
                    "ordem": {
                        "type": "integer",
                        "example": 0
                    },
                    "ativa": {
                        "type": "boolean",
                        "example": true
                    }
                }
            }
        }
    },
    "paths": {
        "/api/vistorias/webhook": {
            "post": {
                "tags": [
                    "Vistorias"
                ],
                "summary": "Criar vistoria via webhook",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/WebhookVistoriaInput"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Vistoria criada"
                    },
                    "401": {
                        "description": "Token inválido",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/ApiError"
                                }
                            }
                        }
                    }
                }
            }
        },
        "/api/vistorias/criar": {
            "post": {
                "tags": [
                    "Vistorias"
                ],
                "summary": "Criar vistoria manual",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/CriarVistoriaInput"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Vistoria criada"
                    }
                }
            }
        },
        "/api/vistorias/{token}": {
            "get": {
                "tags": [
                    "Vistorias"
                ],
                "summary": "Consultar vistoria por token",
                "parameters": [
                    {
                        "name": "token",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Dados da vistoria"
                    },
                    "404": {
                        "description": "Vistoria não encontrada"
                    }
                }
            }
        },
        "/api/vistorias/{token}/upload-lote": {
            "post": {
                "tags": [
                    "Vistorias"
                ],
                "summary": "Enviar fotos da vistoria em lote",
                "description": "Aceita apenas imagens capturadas pela câmera do celular.",
                "parameters": [
                    {
                        "name": "token",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "multipart/form-data": {
                            "schema": {
                                "type": "object",
                                "required": [
                                    "fotos[]",
                                    "categoria_ids[]"
                                ],
                                "properties": {
                                    "fotos[]": {
                                        "type": "array",
                                        "items": {
                                            "type": "string",
                                            "format": "binary"
                                        }
                                    },
                                    "categoria_ids[]": {
                                        "type": "array",
                                        "items": {
                                            "type": "integer"
                                        }
                                    },
                                    "latitude": {
                                        "type": "number",
                                        "format": "float"
                                    },
                                    "longitude": {
                                        "type": "number",
                                        "format": "float"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Arquivos recebidos"
                    },
                    "400": {
                        "description": "Upload inválido"
                    }
                }
            }
        },
        "/api/vistorias/{token}/finalizar": {
            "post": {
                "tags": [
                    "Vistorias"
                ],
                "summary": "Finalizar vistoria",
                "parameters": [
                    {
                        "name": "token",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Vistoria finalizada"
                    }
                }
            }
        },
        "/api/vistorias/templates": {
            "get": {
                "tags": [
                    "Templates"
                ],
                "summary": "Listar templates de vistoria",
                "responses": {
                    "200": {
                        "description": "Lista de templates"
                    }
                }
            },
            "post": {
                "tags": [
                    "Templates"
                ],
                "summary": "Criar template de vistoria",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "required": [
                                    "nome"
                                ],
                                "properties": {
                                    "nome": {
                                        "type": "string"
                                    },
                                    "descricao": {
                                        "type": "string",
                                        "nullable": true
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Template criado"
                    }
                }
            }
        },
        "/api/vistorias/pendencia-motivos": {
            "get": {
                "tags": [
                    "Pendências"
                ],
                "summary": "Listar motivos de pendência",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "parameters": [
                    {
                        "name": "all",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "integer",
                            "example": 1
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Lista de motivos"
                    }
                }
            },
            "post": {
                "tags": [
                    "Pendências"
                ],
                "summary": "Criar motivo de pendência",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/PendenciaMotivoInput"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Motivo criado"
                    }
                }
            }
        },
        "/api/vistorias/pendencia-motivos/{id}": {
            "put": {
                "tags": [
                    "Pendências"
                ],
                "summary": "Atualizar motivo de pendência",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "parameters": [
                    {
                        "name": "id",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "integer"
                        }
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/PendenciaMotivoInput"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Motivo atualizado"
                    }
                }
            },
            "delete": {
                "tags": [
                    "Pendências"
                ],
                "summary": "Inativar motivo de pendência",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "parameters": [
                    {
                        "name": "id",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "integer"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Motivo inativado"
                    }
                }
            }
        },
        "/api/bitrix/deals/{dealId}/photos": {
            "get": {
                "tags": [
                    "Bitrix"
                ],
                "summary": "Listar fotos do negócio na aba do Bitrix",
                "parameters": [
                    {
                        "name": "dealId",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "integer"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Lista de fotos do negócio"
                    }
                }
            }
        },
        "/api/bitrix/photos/{photoId}/pendenciar": {
            "post": {
                "tags": [
                    "Bitrix"
                ],
                "summary": "Pendenciar uma foto na revisão do Bitrix",
                "parameters": [
                    {
                        "name": "photoId",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "integer"
                        }
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "required": [
                                    "motivo_id"
                                ],
                                "properties": {
                                    "motivo_id": {
                                        "type": "integer"
                                    },
                                    "observacao": {
                                        "type": "string",
                                        "nullable": true
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Foto pendenciada"
                    }
                }
            }
        },
        "/api/planos/consultar": {
            "post": {
                "tags": [
                    "Planos"
                ],
                "summary": "Consultar plano por valor",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/PlanoConsultaInput"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Plano encontrado"
                    },
                    "404": {
                        "description": "Nenhum plano encontrado"
                    }
                }
            }
        },
        "/api/planos": {
            "get": {
                "tags": [
                    "Planos"
                ],
                "summary": "Listar planos",
                "responses": {
                    "200": {
                        "description": "Lista de planos"
                    }
                }
            },
            "post": {
                "tags": [
                    "Planos"
                ],
                "summary": "Criar plano",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/PlanoInput"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Plano criado"
                    }
                }
            }
        },
        "/api/planos/{id}": {
            "put": {
                "tags": [
                    "Planos"
                ],
                "summary": "Atualizar plano",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "parameters": [
                    {
                        "name": "id",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "integer"
                        }
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/PlanoInput"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Plano atualizado"
                    }
                }
            },
            "delete": {
                "tags": [
                    "Planos"
                ],
                "summary": "Inativar plano",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "parameters": [
                    {
                        "name": "id",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "integer"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Plano inativado"
                    }
                }
            }
        },
        "/api/planos/{id}/produtos": {
            "post": {
                "tags": [
                    "Planos"
                ],
                "summary": "Adicionar produto ao plano",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "parameters": [
                    {
                        "name": "id",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "integer"
                        }
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/ProdutoPlanoInput"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Produto adicionado"
                    }
                }
            }
        },
        "/api/planos/{id}/regras": {
            "post": {
                "tags": [
                    "Planos"
                ],
                "summary": "Adicionar regra ao plano",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "parameters": [
                    {
                        "name": "id",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "integer"
                        }
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/RegraPlanoInput"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Regra adicionada"
                    }
                }
            }
        }
    }
}