{
  "openapi": "3.0.1",
  "info": {
    "title": "Lex API",
    "description": "An API to manage your documents",
    "license": {
      "name": "MIT"
    },
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://lex.page/api/v1"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {
    "/docs": {
      "post": {
        "description": "Creates a new document",
        "requestBody": {
          "description": "Document to create",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/NewDoc"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "Created",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DocCreated"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "get": {
        "description": "Retrieves a paginated list of documents for the current user",
        "parameters": [
          {
            "name": "page",
            "in": "query",
            "description": "Page number for pagination",
            "schema": {
              "type": "integer",
              "default": 1
            }
          },
          {
            "name": "per_page",
            "in": "query",
            "description": "Number of items per page",
            "schema": {
              "type": "integer",
              "default": 20
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "docs": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/DocWithUserDetails"
                      }
                    },
                    "pagination": {
                      "$ref": "#/components/schemas/PaginationInfo"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/docs/{uuid}": {
      "put": {
        "description": "Updates an existing document's content and/or title",
        "parameters": [
          {
            "name": "uuid",
            "in": "path",
            "description": "The UUID of the document to update",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "requestBody": {
          "description": "Document update data",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/DocUpdate"
              }
            }
          },
          "required": true
        },
        "responses": {
          "204": {
            "description": "Document updated successfully"
          },
          "400": {
            "description": "Bad Request - At least one of title or html must be provided",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Document not found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer"
      }
    },
    "schemas": {
      "NewDoc": {
        "type": "object",
        "required": ["title"],
        "properties": {
          "title": {
            "type": "string",
            "description": "The title of the document."
          },
          "html": {
            "type": "string",
            "description": "The HTML content the document can be pre-filled with."
          },
          "path": {
            "type": "string",
            "description": "The location of the document in the API key user's folder structure. The path is equal to the URL in Lex of the folder, for example the home folder is `https://lex.page/~` and a subfolder might be `https://lex.page/~/my-folder`.",
            "default": "~"
          }
        }
      },
      "DocUpdate": {
        "type": "object",
        "description": "Document update data. At least one of title or html must be provided.",
        "properties": {
          "title": {
            "type": "string",
            "description": "The new title for the document."
          },
          "html": {
            "type": "string",
            "description": "The new HTML content for the document."
          }
        },
        "anyOf": [
          {
            "required": ["title"]
          },
          {
            "required": ["html"]
          }
        ]
      },
      "DocCreated": {
        "type": "object",
        "properties": {
          "uuid": {
            "type": "string",
            "description": "The unique identifier of the created document."
          }
        }
      },
      "DocWithUserDetails": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "description": "The document ID."
          },
          "updated_at": {
            "type": "string",
            "format": "date-time",
            "description": "The last update timestamp of the document."
          },
          "user_docs": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "user": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "integer",
                      "description": "The user ID."
                    },
                    "fname": {
                      "type": "string",
                      "description": "The user's first name."
                    },
                    "lname": {
                      "type": "string",
                      "description": "The user's last name."
                    },
                    "email": {
                      "type": "string",
                      "format": "email",
                      "description": "The user's email address."
                    },
                    "avatar_url": {
                      "type": "string",
                      "format": "uri",
                      "description": "The URL of the user's avatar image."
                    }
                  }
                }
              }
            }
          }
        }
      },
      "PaginationInfo": {
        "type": "object",
        "properties": {
          "current_page": {
            "type": "integer",
            "description": "The current page number"
          },
          "next_page": {
            "type": "integer",
            "nullable": true,
            "description": "The next page number, or null if there is no next page"
          },
          "prev_page": {
            "type": "integer",
            "nullable": true,
            "description": "The previous page number, or null if there is no previous page"
          },
          "total_pages": {
            "type": "integer",
            "description": "The total number of pages"
          },
          "total_count": {
            "type": "integer",
            "description": "The total number of items across all pages"
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string",
            "description": "The error message"
          }
        }
      }
    }
  }
}
