{
  "openapi": "3.1.0",
  "info": {
    "title": "Invoodle API",
    "version": "1.0.0",
    "description": "REST API to create, list, update, delete, and share invoices programmatically. Requires a deployed Supabase function and an account key. Supplied totals are stored, not calculated. Share links contain readable snapshots; PDF endpoints return browser URLs, not PDF bytes."
  },
  "servers": [
    {
      "url": "https://{project}.supabase.co/functions/v1/api-v1",
      "variables": {
        "project": {
          "default": "YOUR_PROJECT",
          "description": "Replace with your deployed Supabase project reference."
        }
      }
    }
  ],
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "invd_live_..."
      }
    },
    "schemas": {
      "InvoiceItem": {
        "type": "object",
        "properties": {
          "id": {
            "oneOf": [
              {
                "type": "string"
              },
              {
                "type": "number"
              }
            ]
          },
          "description": {
            "type": "string"
          },
          "quantity": {
            "type": "number"
          },
          "unit": {
            "type": "string"
          },
          "rate": {
            "type": "number"
          },
          "amount": {
            "type": "number"
          }
        }
      },
      "InvoiceData": {
        "type": "object",
        "required": [
          "items"
        ],
        "properties": {
          "template": {
            "type": "string"
          },
          "country": {
            "type": "string"
          },
          "currencyKey": {
            "type": "string"
          },
          "storeName": {
            "type": "string"
          },
          "customerName": {
            "type": "string"
          },
          "invoiceNumber": {
            "type": "string"
          },
          "invoiceDate": {
            "type": "string"
          },
          "items": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/InvoiceItem"
            },
            "minItems": 1
          }
        }
      },
      "Invoice": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "invoice_number": {
            "type": "string"
          },
          "status": {
            "type": "string"
          },
          "total": {
            "type": "number"
          },
          "currency": {
            "type": "string"
          },
          "data": {
            "$ref": "#/components/schemas/InvoiceData"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "updated_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "InvoiceCreate": {
        "type": "object",
        "properties": {
          "invoice_number": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "draft",
              "sent",
              "paid"
            ]
          },
          "total": {
            "type": "number"
          },
          "currency": {
            "type": "string"
          },
          "data": {
            "$ref": "#/components/schemas/InvoiceData"
          }
        },
        "required": [
          "data"
        ],
        "description": "Recommended nested create shape. total defaults to 0 and is not recalculated. Keep summary fields consistent with data. Top-level invoice data is also accepted by the handler."
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          }
        }
      },
      "InvoicePatch": {
        "type": "object",
        "description": "Partial summary update. data replaces the entire payload without merging or recalculating totals.",
        "properties": {
          "invoice_number": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "draft",
              "sent",
              "paid"
            ]
          },
          "total": {
            "type": "number"
          },
          "currency": {
            "type": "string"
          },
          "data": {
            "$ref": "#/components/schemas/InvoiceData"
          }
        }
      }
    }
  },
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {
    "/me": {
      "get": {
        "summary": "Identity check",
        "responses": {
          "200": {
            "description": "OK"
          },
          "401": {
            "description": "Missing or invalid API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/invoices": {
      "get": {
        "summary": "List invoices",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 50,
              "maximum": 200
            }
          },
          {
            "name": "offset",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 0
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Preferred over offset for large lists. When set, the response omits the `count` field."
          },
          {
            "name": "status",
            "in": "query",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Invoice"
                      }
                    },
                    "count": {
                      "type": "integer",
                      "description": "Total matching rows. Omitted when `cursor` is used."
                    },
                    "next_cursor": {
                      "type": [
                        "string",
                        "null"
                      ]
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "Rate limit exceeded",
            "headers": {
              "Retry-After": {
                "schema": {
                  "type": "integer"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Create invoice",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/InvoiceCreate"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Invoice"
                }
              }
            }
          },
          "400": {
            "description": "Validation error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "Request body exceeds 256 KB",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/invoices/{id}": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "Get invoice",
        "responses": {
          "200": {
            "description": "OK"
          },
          "404": {
            "description": "Not found"
          }
        }
      },
      "patch": {
        "summary": "Update invoice",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/InvoicePatch"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      },
      "delete": {
        "summary": "Delete invoice",
        "responses": {
          "204": {
            "description": "Deleted"
          }
        }
      }
    },
    "/invoices/{id}/share": {
      "post": {
        "summary": "Share URL for saved invoice",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "url": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/invoices/{id}/pdf": {
      "post": {
        "summary": "Get auto-download PDF URL",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "url": {
                      "type": "string"
                    },
                    "filename": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/share": {
      "post": {
        "summary": "Stateless share link + QR",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "data": {
                    "$ref": "#/components/schemas/InvoiceData"
                  }
                },
                "required": [
                  "data"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "share_url": {
                      "type": "string"
                    },
                    "qr_png_base64": {
                      "type": "string",
                      "description": "Complete PNG data URL, including data:image/png;base64, prefix."
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
