{
  "openapi": "3.0.3",
  "info": {
    "title": "FinBridge API",
    "version": "1.0.0",
    "description": "Public REST API for invoices, clients, payments, bills, vendors, and inventory. Authenticate with a bearer token (sk_live_...) from the Developer dashboard.",
    "contact": { "name": "FinBridge Support", "url": "https://finmgt.lovable.app/docs/api" }
  },
  "servers": [
    { "url": "https://yongulvpjrqirvbdcdhh.supabase.co/functions/v1/api-v1", "description": "Production" }
  ],
  "security": [{ "bearerAuth": [] }],
  "tags": [
    { "name": "Invoices" },
    { "name": "Clients" },
    { "name": "Payments" },
    { "name": "Bills" },
    { "name": "Vendors" },
    { "name": "Inventory" }
  ],
  "paths": {
    "/v1/invoices": {
      "get": { "tags": ["Invoices"], "summary": "List invoices", "parameters": [
        { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 50, "maximum": 200 } },
        { "name": "offset", "in": "query", "schema": { "type": "integer", "default": 0 } }
      ], "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/InvoiceList" } } } } } },
      "post": { "tags": ["Invoices"], "summary": "Create invoice", "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Invoice" } } } }, "responses": { "201": { "description": "Created", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Invoice" } } } } } }
    },
    "/v1/invoices/{id}": {
      "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }],
      "get": { "tags": ["Invoices"], "summary": "Retrieve invoice", "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Invoice" } } } }, "404": { "$ref": "#/components/responses/NotFound" } } },
      "patch": { "tags": ["Invoices"], "summary": "Update invoice", "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Invoice" } } } }, "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Invoice" } } } } } },
      "delete": { "tags": ["Invoices"], "summary": "Delete invoice", "responses": { "200": { "description": "OK" } } }
    },
    "/v1/clients": {
      "get": { "tags": ["Clients"], "summary": "List clients", "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ClientList" } } } } } },
      "post": { "tags": ["Clients"], "summary": "Create client", "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Client" } } } }, "responses": { "201": { "description": "Created", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Client" } } } } } }
    },
    "/v1/clients/{id}": {
      "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }],
      "get": { "tags": ["Clients"], "summary": "Retrieve client", "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Client" } } } } } },
      "patch": { "tags": ["Clients"], "summary": "Update client", "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Client" } } } }, "responses": { "200": { "description": "OK" } } },
      "delete": { "tags": ["Clients"], "summary": "Delete client", "responses": { "200": { "description": "OK" } } }
    },
    "/v1/payments": {
      "get": { "tags": ["Payments"], "summary": "List payments", "responses": { "200": { "description": "OK" } } },
      "post": { "tags": ["Payments"], "summary": "Record payment", "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Payment" } } } }, "responses": { "201": { "description": "Created" } } }
    },
    "/v1/payments/{id}": {
      "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }],
      "get": { "tags": ["Payments"], "summary": "Retrieve payment", "responses": { "200": { "description": "OK" } } },
      "patch": { "tags": ["Payments"], "summary": "Update payment", "responses": { "200": { "description": "OK" } } },
      "delete": { "tags": ["Payments"], "summary": "Delete payment", "responses": { "200": { "description": "OK" } } }
    },
    "/v1/bills": {
      "get": { "tags": ["Bills"], "summary": "List bills", "responses": { "200": { "description": "OK" } } },
      "post": { "tags": ["Bills"], "summary": "Create bill", "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Bill" } } } }, "responses": { "201": { "description": "Created" } } }
    },
    "/v1/bills/{id}": {
      "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }],
      "get": { "tags": ["Bills"], "summary": "Retrieve bill", "responses": { "200": { "description": "OK" } } },
      "patch": { "tags": ["Bills"], "summary": "Update bill", "responses": { "200": { "description": "OK" } } },
      "delete": { "tags": ["Bills"], "summary": "Delete bill", "responses": { "200": { "description": "OK" } } }
    },
    "/v1/vendors": {
      "get": { "tags": ["Vendors"], "summary": "List vendors", "responses": { "200": { "description": "OK" } } },
      "post": { "tags": ["Vendors"], "summary": "Create vendor", "responses": { "201": { "description": "Created" } } }
    },
    "/v1/vendors/{id}": {
      "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }],
      "get": { "tags": ["Vendors"], "summary": "Retrieve vendor", "responses": { "200": { "description": "OK" } } },
      "patch": { "tags": ["Vendors"], "summary": "Update vendor", "responses": { "200": { "description": "OK" } } },
      "delete": { "tags": ["Vendors"], "summary": "Delete vendor", "responses": { "200": { "description": "OK" } } }
    },
    "/v1/inventory": {
      "get": { "tags": ["Inventory"], "summary": "List inventory items", "responses": { "200": { "description": "OK" } } },
      "post": { "tags": ["Inventory"], "summary": "Create inventory item", "responses": { "201": { "description": "Created" } } }
    },
    "/v1/inventory/{id}": {
      "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }],
      "get": { "tags": ["Inventory"], "summary": "Retrieve inventory item", "responses": { "200": { "description": "OK" } } },
      "patch": { "tags": ["Inventory"], "summary": "Update inventory item", "responses": { "200": { "description": "OK" } } },
      "delete": { "tags": ["Inventory"], "summary": "Delete inventory item", "responses": { "200": { "description": "OK" } } }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": { "type": "http", "scheme": "bearer", "bearerFormat": "sk_live_..." }
    },
    "responses": {
      "NotFound": { "description": "Resource not found", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
      "Unauthorized": { "description": "Missing or invalid API key", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } }
    },
    "schemas": {
      "Error": { "type": "object", "properties": { "error": { "type": "string" } } },
      "Invoice": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid", "readOnly": true },
          "invoice_number": { "type": "string" },
          "client_id": { "type": "string", "format": "uuid" },
          "issue_date": { "type": "string", "format": "date" },
          "due_date": { "type": "string", "format": "date" },
          "status": { "type": "string", "enum": ["draft", "sent", "paid", "overdue", "void"] },
          "currency": { "type": "string" },
          "exchange_rate": { "type": "number" },
          "subtotal": { "type": "number" },
          "tax_total": { "type": "number" },
          "total": { "type": "number" },
          "notes": { "type": "string", "nullable": true },
          "created_at": { "type": "string", "format": "date-time", "readOnly": true }
        },
        "required": ["invoice_number", "client_id"]
      },
      "InvoiceList": {
        "type": "object",
        "properties": {
          "data": { "type": "array", "items": { "$ref": "#/components/schemas/Invoice" } },
          "total": { "type": "integer" },
          "limit": { "type": "integer" },
          "offset": { "type": "integer" }
        }
      },
      "Client": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid", "readOnly": true },
          "name": { "type": "string" },
          "company_name": { "type": "string", "nullable": true },
          "email": { "type": "string", "nullable": true },
          "phone": { "type": "string", "nullable": true },
          "payment_terms": { "type": "string", "enum": ["net7", "net14", "net30", "net60", "net90", "due_on_receipt"] },
          "status": { "type": "string", "enum": ["active", "inactive", "prospect"] },
          "billing_address": { "type": "string", "nullable": true },
          "city": { "type": "string", "nullable": true },
          "country": { "type": "string", "nullable": true }
        },
        "required": ["name"]
      },
      "ClientList": {
        "type": "object",
        "properties": {
          "data": { "type": "array", "items": { "$ref": "#/components/schemas/Client" } },
          "total": { "type": "integer" }
        }
      },
      "Payment": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid", "readOnly": true },
          "invoice_id": { "type": "string", "format": "uuid" },
          "amount": { "type": "number" },
          "payment_date": { "type": "string", "format": "date" },
          "payment_method": { "type": "string" },
          "reference": { "type": "string", "nullable": true },
          "notes": { "type": "string", "nullable": true }
        },
        "required": ["invoice_id", "amount"]
      },
      "Bill": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid", "readOnly": true },
          "vendor_id": { "type": "string", "format": "uuid" },
          "bill_number": { "type": "string" },
          "issue_date": { "type": "string", "format": "date" },
          "due_date": { "type": "string", "format": "date" },
          "status": { "type": "string", "enum": ["draft", "received", "paid", "overdue", "void"] },
          "currency": { "type": "string" },
          "subtotal": { "type": "number" },
          "tax_total": { "type": "number" },
          "total": { "type": "number" }
        },
        "required": ["vendor_id", "bill_number"]
      }
    }
  }
}
