{
  "openapi": "3.0.3",
  "info": {
    "title": "ValutazioniOnline API",
    "version": "1.1.0",
    "description": "API B2B per la valutazione di auto usate basata su un modello ML addestrato su annunci reali. Ogni POST /valuation consuma 1 credito (con rimborso automatico se la valutazione fallisce per errore del servizio). Rate limit per piano: Free 10, Starter 30, Pro 60, Business 120 richieste/minuto. Per provare senza consumare crediti usa il playground in /api-doc (max 10 valutazioni/giorno).",
    "contact": { "name": "ValutazioniOnline", "url": "https://valutazionionline.com/" }
  },
  "servers": [
    { "url": "https://valutazionionline.com", "description": "Produzione" }
  ],
  "security": [ { "ApiKeyAuth": [] } ],
  "paths": {
    "/api/v1/health": {
      "get": {
        "summary": "Stato del servizio",
        "operationId": "health",
        "security": [ { "ApiKeyAuth": [] } ],
        "responses": {
          "200": {
            "description": "Stato del servizio e del motore di valutazione",
            "content": {
              "application/json": {
                "schema": { "type": "object", "properties": {
                  "status": { "type": "string", "example": "ok" },
                  "model_available": { "type": "boolean", "example": true },
                  "model_trained_at": { "type": "string", "format": "date-time" }
                } }
              }
            }
          }
        }
      }
    },
    "/api/v1/valuation": {
      "post": {
        "summary": "Valuta un veicolo usato",
        "operationId": "valuation",
        "description": "Consuma 1 credito. Su errore del servizio il credito viene rimborsato automaticamente.",
        "security": [ { "ApiKeyAuth": [] } ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/ValuationRequest" },
              "example": { "marca": "Volkswagen", "modello": "Golf", "km": 80000, "anno": 2020, "alimentazione": "Diesel", "regione": "Lombardia" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Valutazione riuscita",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ValuationResponse" }
              }
            }
          },
          "400": { "description": "Input non valido o combinazione non coperta (nessun addebito)", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "401": { "description": "API key mancante o non valida", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "402": { "description": "Crediti esauriti", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "429": { "description": "Rate limit superato", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
        }
      }
    },
    "/api/v1/usage": {
      "get": {
        "summary": "Statistiche d'uso dei tuoi crediti",
        "operationId": "usage",
        "security": [ { "ApiKeyAuth": [] } ],
        "responses": {
          "200": {
            "description": "Crediti residui, piano e uso per endpoint (30 giorni)",
            "content": { "application/json": { "schema": { "type": "object" } } }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": { "type": "apiKey", "in": "header", "name": "X-API-Key" }
    },
    "schemas": {
      "ValuationRequest": {
        "type": "object",
        "required": ["marca", "modello", "km", "anno"],
        "properties": {
          "marca": { "type": "string", "example": "Volkswagen" },
          "modello": { "type": "string", "example": "Golf" },
          "km": { "type": "integer", "minimum": 0, "maximum": 1000000, "example": 80000 },
          "anno": { "type": "integer", "minimum": 1950, "example": 2020 },
          "allestimento": { "type": "string", "example": "GTI" },
          "alimentazione": { "type": "string", "example": "Diesel" },
          "cambio": { "type": "string", "example": "Manuale" },
          "potenza_cv": { "type": "integer", "example": 150 },
          "regione": { "type": "string", "example": "Lombardia" },
          "tipo_venditore": { "type": "string", "enum": ["privato", "dealer"], "example": "dealer" },
          "sconto_pct": { "type": "integer", "enum": [0, -5, -10, -20], "description": "Sconto prudenziale per valutazioni di ritiro/permuta", "example": -10 }
        }
      },
      "ValuationResponse": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean", "example": true },
          "prezzo_stimato": { "type": "integer", "description": "Valore di mercato scoperto", "example": 14500 },
          "prezzo_finale": { "type": "integer", "description": "Valore finale dopo eventuale sconto ritiro", "example": 13050 },
          "sconto_pct": { "type": "integer", "example": -10 },
          "range_min": { "type": "integer", "example": 12800 },
          "range_max": { "type": "integer", "example": 15300 },
          "confidenza": { "type": "string", "enum": ["alta", "media", "bassa"], "example": "alta" },
          "n_comparabili": { "type": "integer", "example": 42 },
          "metodo": { "type": "string", "example": "machine_learning" },
          "comparabili": { "type": "array", "description": "Annunci reali comparabili su cui si basa la stima", "items": { "type": "object" } },
          "spiegazione": { "type": "object", "description": "Explainability SHAP: top_contributi + testo" },
          "model_metadata": { "type": "object" }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "properties": {
          "error": { "type": "string", "example": "API key non valida" }
        }
      }
    }
  }
}