{
  "openapi": "3.1.0",
  "info": {
    "title": "Printery API",
    "version": "1.0.0",
    "description": "Envie PDFs para as impressoras da sua empresa. Crie a chave em Integrações, no painel do Printery. Plano Pro ou superior."
  },
  "servers": [{ "url": "/" }],
  "security": [{ "bearer": [] }],
  "components": {
    "securitySchemes": { "bearer": { "type": "http", "scheme": "bearer", "description": "Chave prk_… criada no painel." } },
    "schemas": {
      "Printer": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "name": { "type": "string" },
          "station": { "type": "string", "description": "Computador onde a impressora está." },
          "enabled": { "type": "boolean" },
          "available": { "type": "boolean", "description": "O computador ainda vê essa impressora." },
          "online": { "type": "boolean", "description": "O computador falou com o Printery no último minuto." },
          "lastSeenAt": { "type": "string", "format": "date-time" }
        }
      },
      "Options": {
        "type": "object",
        "properties": {
          "copies": { "type": "integer", "minimum": 1, "maximum": 99, "default": 1 },
          "color": { "type": "string", "enum": ["color", "mono"], "default": "color" },
          "duplex": { "type": "string", "enum": ["off", "long", "short"], "default": "off" },
          "fit": { "type": "string", "enum": ["fit", "shrink", "noscale"], "default": "fit" },
          "pages": { "type": "string", "example": "1-3,5" }
        }
      },
      "Job": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "printerId": { "type": "string", "format": "uuid" },
          "printer": { "type": "string" },
          "station": { "type": "string" },
          "title": { "type": "string" },
          "status": {
            "type": "string",
            "enum": ["queued", "processing", "submitted", "needs_review", "canceled"],
            "description": "queued: na fila. processing: o computador pegou. submitted: entregue à impressora. needs_review: falhou ou foi interrompido, confira antes de reenviar. canceled: cancelado no painel."
          },
          "detail": { "type": ["string", "null"] },
          "options": { "$ref": "#/components/schemas/Options" },
          "size": { "type": "integer" },
          "source": { "type": "string", "enum": ["panel", "api"] },
          "createdAt": { "type": "string", "format": "date-time" },
          "history": {
            "type": "array",
            "items": { "type": "object", "properties": { "status": { "type": "string" }, "at": { "type": "string", "format": "date-time" } } }
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": { "error": { "type": "string" }, "code": { "type": "string" }, "requestId": { "type": "string" } }
      }
    }
  },
  "paths": {
    "/v1/printers": {
      "get": {
        "summary": "Listar impressoras",
        "description": "Escopo printers:read.",
        "responses": {
          "200": { "description": "OK", "content": { "application/json": { "schema": { "type": "object", "properties": { "printers": { "type": "array", "items": { "$ref": "#/components/schemas/Printer" } } } } } } },
          "401": { "description": "Chave ausente ou inválida", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
          "402": { "description": "Assinatura inativa" },
          "403": { "description": "Escopo insuficiente ou plano sem API" }
        }
      }
    },
    "/v1/jobs": {
      "post": {
        "summary": "Enviar PDF para impressão",
        "description": "Escopo jobs:write. Repetir a mesma Idempotency-Key com o mesmo arquivo, título, impressora e opções devolve o mesmo trabalho (200). Com qualquer diferença, 409.",
        "parameters": [
          { "name": "Idempotency-Key", "in": "header", "required": true, "schema": { "type": "string", "pattern": "^[A-Za-z0-9._:-]{1,128}$" }, "example": "pedido-1001" },
          { "name": "X-Printer-Id", "in": "header", "required": true, "schema": { "type": "string", "format": "uuid" } },
          { "name": "X-Document-Title", "in": "header", "schema": { "type": "string" }, "description": "Codificado com encodeURIComponent. Até 160 caracteres." },
          { "name": "X-Print-Options", "in": "header", "schema": { "type": "string" }, "description": "JSON com as opções.", "example": "{\"copies\":2,\"color\":\"mono\"}" }
        ],
        "requestBody": { "required": true, "content": { "application/pdf": { "schema": { "type": "string", "format": "binary", "maxLength": 16777216 } } } },
        "responses": {
          "201": { "description": "Na fila", "headers": { "Location": { "schema": { "type": "string" } } }, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Job" } } } },
          "200": { "description": "Mesmo envio repetido", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Job" } } } },
          "400": { "description": "Cabeçalhos, opções ou PDF inválidos" },
          "404": { "description": "Impressora desconhecida" },
          "409": { "description": "Idempotency-Key já usada em outro envio, ou impressora desativada" },
          "413": { "description": "Arquivo acima de 16 MB" },
          "415": { "description": "Content-Type diferente de application/pdf" }
        }
      }
    },
    "/v1/jobs/{id}": {
      "get": {
        "summary": "Consultar trabalho",
        "description": "Escopo jobs:read.",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }],
        "responses": {
          "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Job" } } } },
          "404": { "description": "Trabalho desconhecido" }
        }
      }
    }
  }
}
