{
  "openapi": "3.1.0",
  "info": {
    "title": "kove API",
    "version": "1.0.0",
    "summary": "API HTTP de kove: render hosteado de documentos a PDF bien paginado.",
    "description": "Describe el documento en JSON (el mismo modelo que el CLI y el MCP) y kove devuelve un PDF bien paginado: cabeceras de tabla repetidas, filas que no se parten, totales sin orfandad y numeración de páginas. Autenticación con API key (Bearer). El modelo de documento es la fuente de verdad del contrato (ADR-0015).",
    "license": {
      "name": "Propietario",
      "url": "https://kove.dev"
    }
  },
  "servers": [
    {
      "url": "https://api.kove.dev",
      "description": "Producción (placeholder)"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "tags": [
    {
      "name": "Documentos",
      "description": "Render de documentos a PDF."
    },
    {
      "name": "Cuenta",
      "description": "Estado de cuenta y billing."
    },
    {
      "name": "API keys",
      "description": "Gestión de claves de API."
    }
  ],
  "paths": {
    "/v1/documents": {
      "post": {
        "tags": [
          "Documentos"
        ],
        "operationId": "renderDocument",
        "summary": "Renderiza un documento a PDF",
        "description": "Valida y compila el documento (modelo de documento de kove) y devuelve el PDF resultante. El cuerpo se limita a 256 KiB. Sujeto a rate limiting por cuenta.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "document"
                ],
                "additionalProperties": false,
                "properties": {
                  "document": {
                    "$ref": "#/components/schemas/Document"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "PDF renderizado.",
            "content": {
              "application/pdf": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "400": {
            "description": "Documento inválido (no cumple el modelo de documento).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "API key ausente o inválida.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "Cuerpo demasiado grande (> 256 KiB).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Demasiadas peticiones (rate limit).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/account": {
      "get": {
        "tags": [
          "Cuenta"
        ],
        "operationId": "getAccount",
        "summary": "Estado de la cuenta",
        "description": "Plan actual, uso del periodo, límite y restante.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "Estado de cuenta.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AccountStatus"
                }
              }
            }
          },
          "401": {
            "description": "API key ausente o inválida.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/checkout": {
      "post": {
        "tags": [
          "Cuenta"
        ],
        "operationId": "createCheckout",
        "summary": "Crea una sesión de checkout (upgrade de plan)",
        "description": "Crea una sesión de Stripe Checkout y devuelve la URL para abrir en el navegador.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "plan": {
                    "type": "string",
                    "default": "pro",
                    "description": "Plan a contratar."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "URL de checkout.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "checkout_url"
                  ],
                  "properties": {
                    "checkout_url": {
                      "type": "string",
                      "format": "uri"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "API key ausente o inválida.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/keys": {
      "post": {
        "tags": [
          "API keys"
        ],
        "operationId": "createKey",
        "summary": "Emite una nueva API key",
        "description": "Tras el login hosteado (Clerk), el usuario con session token solicita su API key de cliente. El secreto se devuelve UNA sola vez aquí.",
        "security": [
          {
            "sessionAuth": []
          }
        ],
        "responses": {
          "201": {
            "description": "API key emitida (el secreto solo se ve en esta respuesta).",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "api_key"
                  ],
                  "properties": {
                    "api_key": {
                      "type": "string",
                      "description": "El secreto de la API key (single-view)."
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Session token ausente o inválido.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "get": {
        "tags": [
          "API keys"
        ],
        "operationId": "listKeys",
        "summary": "Lista las API keys del usuario",
        "description": "Metadatos de las keys (nunca el secreto).",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "Listado de keys.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/ApiKey"
                  }
                }
              }
            }
          },
          "401": {
            "description": "Autenticación ausente o inválida.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "delete": {
        "tags": [
          "API keys"
        ],
        "operationId": "deleteKey",
        "summary": "Revoca una API key",
        "description": "Revoca la key indicada. Si no existe o es de otro usuario, responde 404.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "id",
            "in": "query",
            "required": true,
            "description": "Identificador de la API key a revocar.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Key revocada.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "ok"
                  ],
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Falta el id de la key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Autenticación ausente o inválida.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Key no encontrada.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "API key de kove en el header Authorization: Bearer <api_key>."
      },
      "sessionAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Session token de Clerk (login hosteado), para emitir la API key de cliente."
      }
    },
    "schemas": {
      "Document": {
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "$id": "https://spec.kove.dev/document/v0.json",
        "title": "Kove Document (v0)",
        "description": "Fuente de verdad del contrato del documento (ADR-0015). API, CLI, SDKs y docs derivan de aquí.",
        "type": "object",
        "required": [
          "body"
        ],
        "additionalProperties": false,
        "properties": {
          "page": {
            "$ref": "#/$defs/page"
          },
          "header": {
            "$ref": "#/$defs/header"
          },
          "footer": {
            "$ref": "#/$defs/footer"
          },
          "body": {
            "type": "array",
            "description": "Lista de bloques del documento en orden.",
            "items": {
              "$ref": "#/$defs/block"
            }
          }
        },
        "$defs": {
          "length": {
            "type": "string",
            "description": "Longitud CSS (p. ej. '18mm', '10mm', '40mm').",
            "pattern": "^[0-9]+(\\.[0-9]+)?(mm|cm|in|px|pt)$"
          },
          "align": {
            "enum": [
              "left",
              "center",
              "right"
            ]
          },
          "page": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "size": {
                "enum": [
                  "A4",
                  "Letter",
                  "Legal",
                  "A5",
                  "A3"
                ],
                "default": "A4"
              },
              "margin": {
                "$ref": "#/$defs/length",
                "default": "18mm"
              }
            }
          },
          "header": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "repeat": {
                "type": "boolean",
                "default": true
              },
              "text": {
                "type": "string",
                "description": "Texto repetido en cada página (se escapa; sin HTML crudo, ADR-0012)."
              }
            }
          },
          "footer": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "repeat": {
                "type": "boolean",
                "default": true
              },
              "pageNumbers": {
                "type": "string",
                "description": "Plantilla con {page} y {pages}. Ej: 'Página {page} de {pages}'."
              }
            }
          },
          "block": {
            "description": "Un bloque del documento. Discriminado por 'type'.",
            "oneOf": [
              {
                "$ref": "#/$defs/heading"
              },
              {
                "$ref": "#/$defs/text"
              },
              {
                "$ref": "#/$defs/fields"
              },
              {
                "$ref": "#/$defs/table"
              },
              {
                "$ref": "#/$defs/totals"
              },
              {
                "$ref": "#/$defs/section"
              },
              {
                "$ref": "#/$defs/spacer"
              },
              {
                "$ref": "#/$defs/divider"
              },
              {
                "$ref": "#/$defs/pageBreak"
              },
              {
                "$ref": "#/$defs/image"
              },
              {
                "$ref": "#/$defs/signature"
              }
            ]
          },
          "heading": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "type",
              "text"
            ],
            "properties": {
              "type": {
                "const": "heading"
              },
              "text": {
                "type": "string"
              },
              "level": {
                "enum": [
                  1,
                  2,
                  3
                ],
                "default": 1
              }
            }
          },
          "text": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "type",
              "text"
            ],
            "properties": {
              "type": {
                "const": "text"
              },
              "text": {
                "type": "string"
              }
            }
          },
          "fields": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "type",
              "items"
            ],
            "properties": {
              "type": {
                "const": "fields"
              },
              "items": {
                "type": "array",
                "items": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "label",
                    "value"
                  ],
                  "properties": {
                    "label": {
                      "type": "string"
                    },
                    "value": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "table": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "type",
              "columns",
              "rows"
            ],
            "properties": {
              "type": {
                "const": "table"
              },
              "repeatHeader": {
                "type": "boolean",
                "default": true,
                "description": "Cabecera en cada página."
              },
              "keepRowTogether": {
                "type": "boolean",
                "default": true,
                "description": "Una fila no se parte."
              },
              "columns": {
                "type": "array",
                "minItems": 1,
                "items": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "key",
                    "label"
                  ],
                  "properties": {
                    "key": {
                      "type": "string"
                    },
                    "label": {
                      "type": "string"
                    },
                    "align": {
                      "$ref": "#/$defs/align"
                    }
                  }
                }
              },
              "rows": {
                "type": "array",
                "items": {
                  "type": "object"
                }
              }
            }
          },
          "totals": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "type",
              "lines"
            ],
            "properties": {
              "type": {
                "const": "totals"
              },
              "keepWithPrevious": {
                "type": "boolean",
                "default": true,
                "description": "No se quedan huérfanos."
              },
              "lines": {
                "type": "array",
                "items": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "label",
                    "value"
                  ],
                  "properties": {
                    "label": {
                      "type": "string"
                    },
                    "value": {
                      "type": "string"
                    },
                    "emphasis": {
                      "type": "boolean",
                      "default": false
                    }
                  }
                }
              }
            }
          },
          "section": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "type",
              "body"
            ],
            "properties": {
              "type": {
                "const": "section"
              },
              "title": {
                "type": "string"
              },
              "keepTogether": {
                "type": "boolean",
                "default": false,
                "description": "La sección no se parte entre páginas."
              },
              "body": {
                "type": "array",
                "items": {
                  "$ref": "#/$defs/block"
                }
              }
            }
          },
          "spacer": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "type"
            ],
            "properties": {
              "type": {
                "const": "spacer"
              },
              "size": {
                "$ref": "#/$defs/length",
                "default": "8mm"
              }
            }
          },
          "divider": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "type"
            ],
            "properties": {
              "type": {
                "const": "divider"
              }
            }
          },
          "pageBreak": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "type"
            ],
            "properties": {
              "type": {
                "const": "pageBreak"
              }
            }
          },
          "image": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "type",
              "src"
            ],
            "properties": {
              "type": {
                "const": "image"
              },
              "src": {
                "type": "string",
                "pattern": "^(https://|data:)",
                "description": "URL https o data URI. No se permiten file:// ni http:// (seguridad: el motor fetcha el recurso)."
              },
              "width": {
                "$ref": "#/$defs/length"
              },
              "align": {
                "$ref": "#/$defs/align"
              },
              "alt": {
                "type": "string"
              }
            }
          },
          "signature": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "type"
            ],
            "properties": {
              "type": {
                "const": "signature"
              },
              "name": {
                "type": "string"
              },
              "label": {
                "type": "string"
              }
            }
          }
        }
      },
      "AccountStatus": {
        "type": "object",
        "description": "Estado de cuenta: plan, uso del periodo, límite y restante.",
        "properties": {
          "plan": {
            "type": "string",
            "description": "Plan activo (p. ej. free, pro)."
          },
          "usage": {
            "type": "integer",
            "description": "Documentos renderizados en el periodo actual."
          },
          "limit": {
            "type": "integer",
            "description": "Límite de documentos del plan en el periodo."
          },
          "remaining": {
            "type": "integer",
            "description": "Documentos restantes en el periodo."
          }
        }
      },
      "ApiKey": {
        "type": "object",
        "description": "Metadatos de una API key (sin el secreto).",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "createdAt": {
            "type": "integer",
            "description": "Epoch ms de creación."
          },
          "lastUsedAt": {
            "type": [
              "integer",
              "null"
            ],
            "description": "Epoch ms del último uso, o null."
          },
          "revoked": {
            "type": "boolean"
          }
        }
      },
      "Error": {
        "type": "object",
        "description": "Error estándar de la API.",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "string",
            "description": "Mensaje de error legible."
          }
        }
      }
    }
  }
}
