{
  "openapi": "3.1.0",
  "info": {
    "title": "CreateDocs.ai API",
    "version": "1.0.0",
    "description": "REST API for programmatic AI legal-document generation. AI writes complete documents from a description or a template + form data. Agent-ready: every endpoint is session- or API-key authenticated. Not a law firm; generated documents are drafts the user is responsible for reviewing. Full terms: https://createdocs.ai/terms",
    "contact": { "url": "https://createdocs.ai/support" },
    "license": { "name": "Proprietary" }
  },
  "servers": [{ "url": "https://createdocs.ai", "description": "Production" }],
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Session cookie OR API key (Authorization: Bearer <token>). Obtain an API key at /dashboard/settings/integrations."
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": { "type": "string" },
          "code": { "type": "string", "examples": ["LIMIT_EXCEEDED"] },
          "currentCalls": { "type": "integer" },
          "maxCalls": { "type": "integer" }
        }
      },
      "GenerateRequest": {
        "type": "object",
        "required": ["templateId", "inputData"],
        "properties": {
          "templateId": { "type": "string", "description": "ID of a template from /api/templates" },
          "inputData": {
            "type": "object",
            "additionalProperties": true,
            "description": "Key/value answers to the template's input schema"
          },
          "title": { "type": "string", "description": "Optional document title" },
          "options": {
            "type": "object",
            "properties": {
              "tone": { "type": "string", "examples": ["formal", "plain", "friendly"] },
              "length": { "type": "string", "examples": ["short", "standard", "detailed"] }
            }
          }
        }
      },
      "GenerateResponse": {
        "type": "object",
        "properties": {
          "status": { "type": "string", "examples": ["completed", "processing"] },
          "documentId": { "type": "string" },
          "content": { "type": "string", "description": "Full document text (present when status=completed)" },
          "jobId": { "type": "string", "description": "Present when status=processing; poll GET /api/documents/generate?id=<jobId>" }
        }
      }
    }
  },
  "security": [{ "bearerAuth": [] }],
  "paths": {
    "/api/templates": {
      "get": {
        "summary": "List templates",
        "description": "Returns the browsable template catalog (id, name, category, input schema).",
        "parameters": [
          { "name": "category", "in": "query", "schema": { "type": "string" } },
          { "name": "q", "in": "query", "schema": { "type": "string" }, "description": "Search query" }
        ],
        "responses": {
          "200": { "description": "Template list" },
          "401": { "$ref": "#/components/schemas/Error" }
        }
      }
    },
    "/api/documents": {
      "get": {
        "summary": "List the tenant's documents",
        "responses": {
          "200": { "description": "Documents for the authenticated tenant" },
          "401": { "$ref": "#/components/schemas/Error" }
        }
      }
    },
    "/api/documents/generate": {
      "post": {
        "summary": "Generate a document from a template",
        "description": "Async. Returns content inline if fast (<3s), else a jobId to poll. Each call counts as one AI generation against the tenant's monthly plan quota (Solo 1,000 / Team 4,000 / Business 15,000 / Enterprise 50,000). Exceeding the quota returns 429 LIMIT_EXCEEDED.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": { "schema": { "$ref": "#/components/schemas/GenerateRequest" } }
          }
        },
        "responses": {
          "200": { "description": "Completed or accepted", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/GenerateResponse" } } } },
          "400": { "$ref": "#/components/schemas/Error" },
          "401": { "$ref": "#/components/schemas/Error" },
          "404": { "$ref": "#/components/schemas/Error" },
          "429": { "$ref": "#/components/schemas/Error", "description": "AI generation limit reached" }
        }
      }
    },
    "/api/assistant/chat": {
      "post": {
        "summary": "Chat with the document assistant",
        "description": "Conversational help: pick a template, refine wording, explain clauses. Counts as an AI call.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "message": { "type": "string" },
                  "documentId": { "type": "string", "description": "Optional context document" }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Assistant reply" },
          "401": { "$ref": "#/components/schemas/Error" }
        }
      }
    },
    "/api/ai/templates/generate": {
      "post": {
        "summary": "Generate a new template from a description",
        "description": "Admin/owner-only. Builds a reusable template from a natural-language description. Counts as one AI call.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "description": { "type": "string", "minLength": 10, "maxLength": 2000 },
                  "documentType": { "type": "string" },
                  "targetAudience": { "type": "string" },
                  "tone": { "type": "string" }
                },
                "required": ["description"]
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Generated template" },
          "401": { "$ref": "#/components/schemas/Error" },
          "403": { "$ref": "#/components/schemas/Error" }
        }
      }
    }
  }
}
