{
  "openapi": "3.1.0",
  "info": {
    "title": "VerdicallAI Public API",
    "version": "1.0.0",
    "description": "API publique sécurisée et cloisonnée par centre pour l'intégration de la téléphonie, la qualification des appels, la consultation des verdicts IA avec citations probantes et l'accès aux exports."
  },
  "servers": [
    {
      "url": "/api/v1",
      "description": "Serveur API v1"
    }
  ],
  "security": [
    {
      "BearerAuth": []
    }
  ],
  "paths": {
    "/calls": {
      "get": {
        "summary": "Lister les appels du centre",
        "description": "Retourne la liste paginée des appels du centre associé à la clé d'API, avec filtres par statut, campagne et dates.",
        "operationId": "listCalls",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "description": "Nombre maximal d'éléments retournés (1 à 100, défaut 20)",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 20
            }
          },
          {
            "name": "offset",
            "in": "query",
            "description": "Nombre d'éléments à passer (défaut 0)",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 0,
              "default": 0
            }
          },
          {
            "name": "status",
            "in": "query",
            "description": "Filtrer par statut de traitement (ex. PENDING, COMPLETED, FAILED)",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "campaignId",
            "in": "query",
            "description": "Filtrer par identifiant de campagne",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "from",
            "in": "query",
            "description": "Date de début minimale au format ISO 8601",
            "required": false,
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          },
          {
            "name": "to",
            "in": "query",
            "description": "Date de fin maximale au format ISO 8601",
            "required": false,
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Liste des appels récupérée avec succès",
            "headers": {
              "X-RateLimit-Limit": {
                "$ref": "#/components/headers/X-RateLimit-Limit"
              },
              "X-RateLimit-Remaining": {
                "$ref": "#/components/headers/X-RateLimit-Remaining"
              },
              "X-RateLimit-Reset": {
                "$ref": "#/components/headers/X-RateLimit-Reset"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CallListResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          },
          "403": {
            "$ref": "#/components/responses/ForbiddenScopeError"
          },
          "429": {
            "$ref": "#/components/responses/RateLimitError"
          }
        }
      },
      "post": {
        "summary": "Importer un appel",
        "description": "Crée un enregistrement d'appel dans le centre. Gère la déduplication automatique sur le couple centre:identifiant_source.",
        "operationId": "createCall",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateCallRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Appel créé avec succès",
            "headers": {
              "X-RateLimit-Limit": {
                "$ref": "#/components/headers/X-RateLimit-Limit"
              },
              "X-RateLimit-Remaining": {
                "$ref": "#/components/headers/X-RateLimit-Remaining"
              },
              "X-RateLimit-Reset": {
                "$ref": "#/components/headers/X-RateLimit-Reset"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreateCallResponse"
                }
              }
            }
          },
          "200": {
            "description": "Appel existant détecté (doublon ignoré de manière idempotente)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DuplicateCallResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/ValidationError"
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          },
          "403": {
            "$ref": "#/components/responses/ForbiddenScopeError"
          },
          "429": {
            "$ref": "#/components/responses/RateLimitError"
          }
        }
      }
    },
    "/calls/{id}": {
      "get": {
        "summary": "Consulter un appel",
        "description": "Retourne les détails d'un appel spécifique du centre, avec statut d'analyse et métadonnées.",
        "operationId": "getCall",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Identifiant interne de l'appel",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Détails de l'appel",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CallDetailResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          },
          "403": {
            "$ref": "#/components/responses/ForbiddenScopeError"
          },
          "404": {
            "$ref": "#/components/responses/NotFoundError"
          },
          "429": {
            "$ref": "#/components/responses/RateLimitError"
          }
        }
      }
    },
    "/calls/{id}/verdict": {
      "get": {
        "summary": "Consulter le verdict et les citations de l'appel",
        "description": "Retourne le verdict d'analyse IA (CONFIRME, CONTREDIT, A_VERIFIER, SANS_VERDICT), les critères évalués et les citations verbatim horodatées avec interlocuteur.",
        "operationId": "getCallVerdict",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Identifiant interne de l'appel",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Verdict et citations probantes",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/VerdictResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          },
          "403": {
            "$ref": "#/components/responses/ForbiddenScopeError"
          },
          "404": {
            "$ref": "#/components/responses/NotFoundError"
          },
          "429": {
            "$ref": "#/components/responses/RateLimitError"
          }
        }
      }
    },
    "/exports": {
      "get": {
        "summary": "Lister les exports du centre",
        "description": "Retourne la liste des archives et exports générés pour le centre.",
        "operationId": "listExports",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 20
            }
          },
          {
            "name": "offset",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 0,
              "default": 0
            }
          },
          {
            "name": "status",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Liste des exports",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ExportsResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/UnauthorizedError"
          },
          "403": {
            "$ref": "#/components/responses/ForbiddenScopeError"
          },
          "429": {
            "$ref": "#/components/responses/RateLimitError"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "API Key",
        "description": "Clé secrète de centre préfixée par vk_live_ transmise dans l'en-tête Authorization: Bearer <clé>"
      }
    },
    "headers": {
      "X-RateLimit-Limit": {
        "schema": {
          "type": "integer"
        },
        "description": "Quota maximal de requêtes autorisées par minute"
      },
      "X-RateLimit-Remaining": {
        "schema": {
          "type": "integer"
        },
        "description": "Nombre de requêtes restantes sur la fenêtre courante"
      },
      "X-RateLimit-Reset": {
        "schema": {
          "type": "integer"
        },
        "description": "Horodatage Unix (secondes) de réinitialisation du quota"
      }
    },
    "schemas": {
      "ErrorResponse": {
        "type": "object",
        "required": ["error", "code"],
        "properties": {
          "error": {
            "type": "string",
            "description": "Message explicatif de l'erreur"
          },
          "code": {
            "type": "string",
            "description": "Code machine normalisé de l'erreur"
          }
        }
      },
      "CallSummary": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "sourceCallId": { "type": "string" },
          "title": { "type": "string", "nullable": true },
          "direction": { "type": "string", "enum": ["INBOUND", "OUTBOUND"] },
          "status": { "type": "string" },
          "durationSeconds": { "type": "integer" },
          "startedAt": { "type": "string", "format": "date-time" },
          "campaign": {
            "type": "object",
            "nullable": true,
            "properties": {
              "id": { "type": "string" },
              "name": { "type": "string" }
            }
          },
          "agent": {
            "type": "object",
            "nullable": true,
            "properties": {
              "id": { "type": "string" },
              "displayName": { "type": "string" }
            }
          },
          "verdict": { "type": "string", "nullable": true },
          "proposedStatusCode": { "type": "string", "nullable": true },
          "sourceStatusCode": { "type": "string", "nullable": true },
          "failureReason": { "type": "string", "nullable": true },
          "createdAt": { "type": "string", "format": "date-time" }
        }
      },
      "CallListResponse": {
        "type": "object",
        "required": ["calls", "total", "limit", "offset"],
        "properties": {
          "calls": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/CallSummary" }
          },
          "total": { "type": "integer" },
          "limit": { "type": "integer" },
          "offset": { "type": "integer" }
        }
      },
      "CreateCallRequest": {
        "type": "object",
        "properties": {
          "sourceCallId": {
            "type": "string",
            "description": "Identifiant unique de l'appel dans la téléphonie source"
          },
          "campaignId": {
            "type": "string",
            "description": "Identifiant de campagne VerdicallAI optionnel"
          },
          "title": { "type": "string" },
          "direction": { "type": "string", "enum": ["INBOUND", "OUTBOUND"], "default": "OUTBOUND" },
          "sourceStatusCode": { "type": "string" },
          "durationSeconds": { "type": "integer", "minimum": 0 },
          "startedAt": { "type": "string", "format": "date-time" }
        }
      },
      "CreateCallResponse": {
        "type": "object",
        "required": ["ok", "call"],
        "properties": {
          "ok": { "type": "boolean", "example": true },
          "call": {
            "type": "object",
            "properties": {
              "id": { "type": "string" },
              "sourceCallId": { "type": "string" },
              "title": { "type": "string", "nullable": true },
              "direction": { "type": "string" },
              "status": { "type": "string" },
              "startedAt": { "type": "string", "format": "date-time" },
              "createdAt": { "type": "string", "format": "date-time" }
            }
          }
        }
      },
      "DuplicateCallResponse": {
        "type": "object",
        "required": ["ok", "duplicate", "call"],
        "properties": {
          "ok": { "type": "boolean", "example": true },
          "duplicate": { "type": "boolean", "example": true },
          "call": {
            "type": "object",
            "properties": {
              "id": { "type": "string" },
              "sourceCallId": { "type": "string" },
              "status": { "type": "string" },
              "createdAt": { "type": "string", "format": "date-time" }
            }
          },
          "message": { "type": "string" }
        }
      },
      "CallDetailResponse": {
        "type": "object",
        "required": ["call"],
        "properties": {
          "call": {
            "allOf": [
              { "$ref": "#/components/schemas/CallSummary" },
              {
                "type": "object",
                "properties": {
                  "transcription": {
                    "type": "object",
                    "nullable": true,
                    "properties": {
                      "language": { "type": "string" },
                      "speakersMode": { "type": "string" },
                      "createdAt": { "type": "string", "format": "date-time" }
                    }
                  }
                }
              }
            ]
          }
        }
      },
      "Citation": {
        "type": "object",
        "required": ["quote"],
        "properties": {
          "criterionId": { "type": "string", "nullable": true },
          "quote": { "type": "string" },
          "start": { "type": "number", "nullable": true },
          "end": { "type": "number", "nullable": true },
          "speaker": { "type": "string", "nullable": true }
        }
      },
      "VerdictResponse": {
        "type": "object",
        "required": ["callId", "analysisId", "verdict", "status", "criteria", "citations"],
        "properties": {
          "callId": { "type": "string" },
          "analysisId": { "type": "string" },
          "verdict": { "type": "string", "enum": ["CONFIRME", "CONTREDIT", "A_VERIFIER", "SANS_VERDICT"] },
          "proposedStatusCode": { "type": "string", "nullable": true },
          "sourceStatusCode": { "type": "string", "nullable": true },
          "status": { "type": "string" },
          "reviewReason": { "type": "string", "nullable": true },
          "reasons": { "type": "array", "items": { "type": "string" } },
          "reserves": { "type": "array", "items": { "type": "string" } },
          "criteria": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "id": { "type": "string" },
                "result": { "type": "string" },
                "reason": { "type": "string" },
                "quote": { "type": "string", "nullable": true },
                "citation": { "$ref": "#/components/schemas/Citation", "nullable": true }
              }
            }
          },
          "citations": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/Citation" }
          },
          "analyzedAt": { "type": "string", "format": "date-time" }
        }
      },
      "ExportSummary": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "kind": { "type": "string" },
          "status": { "type": "string" },
          "callCount": { "type": "integer" },
          "downloadCount": { "type": "integer" },
          "errorDetail": { "type": "string", "nullable": true },
          "expiresAt": { "type": "string", "format": "date-time", "nullable": true },
          "createdAt": { "type": "string", "format": "date-time" }
        }
      },
      "ExportsResponse": {
        "type": "object",
        "required": ["exports", "total", "limit", "offset"],
        "properties": {
          "exports": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/ExportSummary" }
          },
          "total": { "type": "integer" },
          "limit": { "type": "integer" },
          "offset": { "type": "integer" }
        }
      }
    },
    "responses": {
      "UnauthorizedError": {
        "description": "Authentification requise ou clé d'API invalide / expirée / révoquée",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "ForbiddenScopeError": {
        "description": "Portée d'API insuffisante ou centre inactif",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "NotFoundError": {
        "description": "Ressource introuvable ou n'appartenant pas au centre",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "ValidationError": {
        "description": "Erreur de validation de la requête",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "RateLimitError": {
        "description": "Quota de requêtes par minute dépassé",
        "headers": {
          "X-RateLimit-Limit": {
            "$ref": "#/components/headers/X-RateLimit-Limit"
          },
          "X-RateLimit-Remaining": {
            "$ref": "#/components/headers/X-RateLimit-Remaining"
          },
          "X-RateLimit-Reset": {
            "$ref": "#/components/headers/X-RateLimit-Reset"
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      }
    }
  }
}
