{
  "openapi": "3.1.0",
  "info": {
    "title": "SuperCarros24 HTTP API",
    "version": "1.1.0",
    "description": "Endpoints públicos de leitura para anúncios de EVs usados em Portugal e descoberta por agentes. Rotas autenticadas podem exigir sessão."
  },
  "servers": [{ "url": "https://www.supercarros24.pt" }],
  "paths": {
    "/api/health": {
      "get": {
        "summary": "Estado do serviço",
        "operationId": "getHealth",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": { "type": "string", "example": "ok" },
                    "service": { "type": "string", "example": "supercarros24" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/veiculos/search": {
      "get": {
        "summary": "Procurar anúncios de elétricos usados",
        "description": "Pesquisa pública de anúncios activos em Portugal. Use comBateria=1 para só listagens com SOH estimado. Inclui url canónica, batterySoh, batteryGrade e evScore (derivado do SOH).",
        "operationId": "searchVeiculos",
        "parameters": [
          {
            "name": "marca",
            "in": "query",
            "schema": { "type": "string", "example": "Tesla" },
            "description": "Marca (correspondência exacta, case-insensitive)"
          },
          {
            "name": "modelo",
            "in": "query",
            "schema": { "type": "string", "example": "Model 3" },
            "description": "Modelo (correspondência parcial)"
          },
          {
            "name": "distrito",
            "in": "query",
            "schema": { "type": "string", "example": "Lisboa" }
          },
          {
            "name": "concelho",
            "in": "query",
            "schema": { "type": "string" }
          },
          {
            "name": "comBateria",
            "in": "query",
            "schema": { "type": "string", "enum": ["0", "1", "true", "false"] },
            "description": "Se 1/true, só anúncios com battery_soh preenchido"
          },
          {
            "name": "page",
            "in": "query",
            "schema": { "type": "integer", "minimum": 1, "default": 1 }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": { "type": "integer", "minimum": 1, "maximum": 24, "default": 12 }
          }
        ],
        "responses": {
          "200": {
            "description": "Resultados da procura",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "page": { "type": "integer" },
                    "limit": { "type": "integer" },
                    "total": { "type": "integer" },
                    "totalPages": { "type": "integer" },
                    "filters": { "type": "object" },
                    "items": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/PublicListing" }
                    },
                    "webSearchUrl": {
                      "type": "string",
                      "description": "URL equivalente na UI /procurar"
                    }
                  }
                }
              }
            }
          },
          "429": { "description": "Rate limit (60 pedidos / minuto / IP)" }
        }
      }
    },
    "/api/veiculos/latest": {
      "get": {
        "summary": "Últimos veículos publicados",
        "operationId": "getLatestVeiculos",
        "responses": {
          "200": {
            "description": "Lista de veículos (JSON)",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": { "type": "object" }
                }
              }
            }
          }
        }
      }
    },
    "/api/veiculos/{id}": {
      "get": {
        "summary": "Detalhe de veículo por id",
        "operationId": "getVeiculoById",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Veículo",
            "content": {
              "application/json": {
                "schema": { "type": "object" }
              }
            }
          },
          "404": { "description": "Não encontrado" }
        }
      }
    },
    "/api/sitemap": {
      "get": {
        "summary": "Sitemap XML do site",
        "operationId": "getSitemap",
        "responses": {
          "200": {
            "description": "Documento sitemap",
            "content": {
              "application/xml": { "schema": { "type": "string" } }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "PublicListing": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "marca": { "type": "string" },
          "modelo": { "type": "string" },
          "ano": { "type": "integer" },
          "quilometragem": { "type": "integer" },
          "preco": { "type": "number" },
          "potencia": { "type": ["number", "null"] },
          "autonomia": { "type": ["number", "null"] },
          "distrito": { "type": "string" },
          "concelho": { "type": "string" },
          "tipoOperacao": { "type": "string" },
          "imagem": { "type": ["string", "null"] },
          "batterySoh": { "type": ["number", "null"] },
          "batteryGrade": { "type": ["string", "null"] },
          "evScore": {
            "type": ["integer", "null"],
            "description": "Derivado do SOH estimado (≈ SOH arredondado)"
          },
          "url": { "type": "string", "format": "uri" },
          "criadoEm": { "type": "string" }
        }
      }
    }
  }
}
