{
  "openapi": "3.1.0",
  "info": {
    "title": "StockAvg public agent API",
    "version": "1.0.0",
    "summary": "Read-only endpoints that expose the documented average-cost method and the site's public page inventory.",
    "description": "Public, read-only, no authentication. The same maths the StockAvg calculator applies in the browser (weighted average cost per share, rounded down to 4 decimals for prices and 2 decimals for money). Answers are computed from the request; no data is stored.",
    "contact": {
      "name": "StockAvg",
      "url": "https://stockavg.com/contact",
      "email": "contact@stockavg.com"
    },
    "license": {
      "name": "MIT (method reference implementation)",
      "url": "https://github.com/stockavg/stock-average-cost"
    }
  },
  "servers": [
    {
      "url": "https://stockavg.com",
      "description": "Production origin (Cloudflare Pages)"
    }
  ],
  "paths": {
    "/api/agent/pages": {
      "get": {
        "operationId": "listPages",
        "summary": "List the indexable pages of the site with stable ids",
        "responses": {
          "200": {
            "description": "Page inventory",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "count": { "type": "integer" },
                    "updated": { "type": "string", "description": "Date this inventory was last reconciled with /sitemap.xml." },
                    "sitemap": { "type": "string" },
                    "pages": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": { "type": "string" },
                          "type": { "type": "string", "enum": ["calculator", "page"] },
                          "title": { "type": "string" },
                          "url": { "type": "string" },
                          "lastmod": { "type": "string", "description": "Same date as the matching entry in /sitemap.xml." },
                          "summary": { "type": "string" }
                        },
                        "required": ["id", "type", "title", "url", "lastmod", "summary"]
                      }
                    },
                    "source": { "type": "string" }
                  },
                  "required": ["count", "updated", "sitemap", "pages", "source"]
                }
              }
            }
          }
        }
      }
    },
    "/api/agent/page": {
      "get": {
        "operationId": "getPage",
        "summary": "Fetch one page record by id",
        "parameters": [
          {
            "name": "id",
            "in": "query",
            "required": true,
            "schema": { "type": "string" },
            "description": "Page id from /api/agent/pages, for example \"average-down-calculator\"."
          }
        ],
        "responses": {
          "200": {
            "description": "Page record",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": { "type": "string" },
                    "type": { "type": "string", "enum": ["calculator", "page"] },
                    "title": { "type": "string" },
                    "url": { "type": "string" },
                    "lastmod": { "type": "string" },
                    "summary": { "type": "string" },
                    "source_url": { "type": "string" }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Unknown page id",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" }
              }
            }
          }
        }
      }
    },
    "/api/agent/method": {
      "get": {
        "operationId": "getMethod",
        "summary": "Documented rounding and precision rules plus a worked example",
        "responses": {
          "200": {
            "description": "Method specification",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "formula": { "type": "string" },
                    "rounding": { "type": "string" },
                    "priceDecimals": { "type": "integer" },
                    "moneyDecimals": { "type": "integer" },
                    "workedExample": { "type": "object" },
                    "source_url": { "type": "string" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/agent/average": {
      "get": {
        "operationId": "getAverage",
        "summary": "Weighted average cost per share for a set of purchases",
        "parameters": [
          {
            "name": "legs",
            "in": "query",
            "required": true,
            "description": "Comma separated purchases in the form shares x price, for example \"100x50,50x60\". Maximum 10 legs.",
            "schema": { "type": "string" }
          },
          {
            "name": "currentPrice",
            "in": "query",
            "required": false,
            "description": "Optional current share price. When present the response also reports market value, profit or loss and return.",
            "schema": { "type": "number" }
          }
        ],
        "responses": {
          "200": {
            "description": "Computed position",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/AverageResult" }
              }
            }
          },
          "400": {
            "description": "Missing or malformed legs",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": { "type": "string" },
          "message": { "type": "string" },
          "hint": { "type": "string" }
        },
        "required": ["error", "message"]
      },
      "AverageResult": {
        "type": "object",
        "properties": {
          "averageCostPerShare": { "type": "number" },
          "totalShares": { "type": "number" },
          "totalCost": { "type": "number" },
          "currency": { "type": "string" },
          "legs": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "shares": { "type": "number" },
                "price": { "type": "number" },
                "cost": { "type": "number" }
              }
            }
          },
          "withCurrentPrice": {
            "type": ["object", "null"],
            "properties": {
              "currentPrice": { "type": "number" },
              "marketValue": { "type": "number" },
              "profitLoss": { "type": "number" },
              "returnPercent": { "type": "number" }
            }
          },
          "worked": { "type": "string" },
          "source_url": { "type": "string" }
        },
        "required": ["averageCostPerShare", "totalShares", "totalCost", "legs", "worked", "source_url"]
      }
    }
  }
}
