{
  "openapi": "3.0.3",
  "info": {
    "title": "HAOS Platform Gateway",
    "version": "1.0.0",
    "description": "ADR-0015 — the versioned, consistently-shaped API surface for the HAOS Platform. As of ADR-0036, the real, working legacy JWT auth endpoints (/api/auth/*) are also documented here, since the web/native frontend's auth flow depends on them — everything else under legacy /api/* remains undocumented."
  },
  "servers": [
    { "url": "/api/v1", "description": "Platform Gateway (ADR-0015)" },
    { "url": "/api", "description": "Legacy API (pre-Gateway) — auth endpoints only, documented here per ADR-0036 since the new web/native frontend's auth flow depends on them" }
  ],
  "components": {
    "securitySchemes": {
      "jwt": { "type": "http", "scheme": "bearer", "bearerFormat": "JWT" },
      "apiKey": { "type": "http", "scheme": "bearer", "description": "Sanctum personal access token" },
      "oauth2": {
        "type": "oauth2",
        "flows": {
          "clientCredentials": { "tokenUrl": "/oauth/token", "scopes": {} }
        }
      }
    },
    "schemas": {
      "Envelope": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean" },
          "data": {},
          "meta": { "type": "object" },
          "errors": { "type": "array", "items": { "type": "string" } }
        }
      },
      "AuthTokens": {
        "type": "object",
        "description": "Legacy /api/auth/* endpoints return this shape directly, not wrapped in Envelope.",
        "properties": {
          "access_token": { "type": "string" },
          "token_type": { "type": "string", "example": "bearer" },
          "expires_in": { "type": "integer", "description": "Access token TTL in seconds" },
          "user": { "type": "object" }
        }
      }
    }
  },
  "security": [{ "jwt": [] }, { "apiKey": [] }, { "oauth2": [] }],
  "paths": {
    "/openapi.json": {
      "get": {
        "summary": "This document",
        "security": [],
        "responses": { "200": { "description": "OpenAPI 3.0 document" } }
      }
    },
    "/auth/login": {
      "servers": [{ "url": "/api" }],
      "post": {
        "summary": "Log in — issues a JWT access token + refresh token",
        "tags": ["auth"],
        "security": [],
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "required": ["email", "password"], "properties": { "email": { "type": "string" }, "password": { "type": "string" } } } } } },
        "responses": {
          "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/AuthTokens" } } } },
          "401": { "description": "Invalid credentials" },
          "429": { "description": "Rate limited — 6/min per IP (ADR-0036)" }
        }
      }
    },
    "/auth/register": {
      "servers": [{ "url": "/api" }],
      "post": {
        "summary": "Register — always creates a customer account (role_id=2, ADR-0018)",
        "tags": ["auth"],
        "security": [],
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "required": ["first_name", "last_name", "phone", "email", "password", "password_confirmation"], "properties": { "first_name": { "type": "string" }, "last_name": { "type": "string" }, "phone": { "type": "string" }, "email": { "type": "string" }, "password": { "type": "string" }, "password_confirmation": { "type": "string" } } } } } },
        "responses": {
          "201": { "description": "Created" },
          "422": { "description": "Validation error" },
          "429": { "description": "Rate limited — 6/min per IP (ADR-0036)" }
        }
      }
    },
    "/auth/refresh": {
      "servers": [{ "url": "/api" }],
      "post": {
        "summary": "Refresh an access token",
        "tags": ["auth"],
        "responses": {
          "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/AuthTokens" } } } },
          "401": { "description": "Refresh token invalid, expired, or blacklisted" },
          "429": { "description": "Rate limited — 6/min per IP (ADR-0036)" }
        }
      }
    },
    "/auth/logout": {
      "servers": [{ "url": "/api" }],
      "post": {
        "summary": "Log out — blacklists the current token",
        "tags": ["auth"],
        "responses": { "200": { "description": "OK" } }
      }
    },
    "/users/me": {
      "get": {
        "summary": "The authenticated user and their per-app roles (ADR-0036)",
        "tags": ["users"],
        "responses": {
          "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Envelope" } } } },
          "422": { "description": "No user for this request (e.g. an OAuth Client Credentials token — that authenticates a machine, not a person)" }
        }
      }
    },
    "/public/currencies": {
      "get": {
        "summary": "Global fiat currency reference",
        "tags": ["public"],
        "security": [],
        "responses": {
          "200": {
            "description": "ISO currency codes with display names and decimal precision",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": { "type": "boolean" },
                    "data": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "required": ["code", "name", "decimals"],
                        "properties": {
                          "code": { "type": "string", "example": "NGN" },
                          "name": { "type": "string", "example": "Nigerian Naira" },
                          "decimals": { "type": "integer", "example": 2 }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/platform/health": {
      "get": {
        "summary": "Kernel health check results",
        "tags": ["platform"],
        "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Envelope" } } } } }
      }
    },
    "/platform/applications": {
      "get": { "summary": "Registered Applications", "tags": ["platform"], "responses": { "200": { "description": "OK" } } }
    },
    "/platform/modules": {
      "get": { "summary": "Registered Modules", "tags": ["platform"], "responses": { "200": { "description": "OK" } } }
    },
    "/admin/applications": {
      "get": { "summary": "List discovered/installed Applications", "tags": ["admin"], "responses": { "200": { "description": "OK" } } }
    },
    "/admin/applications/{slug}/install": {
      "post": { "summary": "Install an Application", "tags": ["admin"], "parameters": [{ "name": "slug", "in": "path", "required": true, "schema": { "type": "string" } }], "responses": { "200": { "description": "OK" }, "404": { "description": "Not discovered" } } }
    },
    "/admin/applications/{slug}/enable": {
      "post": { "summary": "Enable an Application", "tags": ["admin"], "parameters": [{ "name": "slug", "in": "path", "required": true, "schema": { "type": "string" } }], "responses": { "200": { "description": "OK" }, "404": { "description": "Not installed" } } }
    },
    "/admin/applications/{slug}/disable": {
      "post": { "summary": "Disable an Application", "tags": ["admin"], "parameters": [{ "name": "slug", "in": "path", "required": true, "schema": { "type": "string" } }], "responses": { "200": { "description": "OK" }, "404": { "description": "Not installed" } } }
    },
    "/admin/applications/{slug}/uninstall": {
      "post": { "summary": "Uninstall an Application", "tags": ["admin"], "parameters": [{ "name": "slug", "in": "path", "required": true, "schema": { "type": "string" } }], "responses": { "200": { "description": "OK" }, "404": { "description": "Not installed" } } }
    },
    "/admin/applications/{slug}/configure": {
      "put": { "summary": "Set a configuration key/value", "tags": ["admin"], "parameters": [{ "name": "slug", "in": "path", "required": true, "schema": { "type": "string" } }], "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "properties": { "key": { "type": "string" }, "value": {} } } } } }, "responses": { "200": { "description": "OK" }, "404": { "description": "Not installed" } } }
    },
    "/admin/applications/{slug}/update": {
      "post": { "summary": "Record a new installed version", "tags": ["admin"], "parameters": [{ "name": "slug", "in": "path", "required": true, "schema": { "type": "string" } }], "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "properties": { "version": { "type": "string" } } } } } }, "responses": { "200": { "description": "OK" }, "404": { "description": "Not installed" } } }
    },
    "/admin/applications/{slug}/assign-tenant": {
      "post": { "summary": "Assign an Application to a Tenant", "tags": ["admin"], "parameters": [{ "name": "slug", "in": "path", "required": true, "schema": { "type": "string" } }], "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "properties": { "tenant_id": { "type": "string" } } } } } }, "responses": { "200": { "description": "OK" }, "404": { "description": "Not installed or tenant not found" } } }
    },
    "/giftcard/giftcards": {
      "get": { "summary": "List non-disabled gift cards", "tags": ["giftcard"], "responses": { "200": { "description": "OK" } } }
    },
    "/giftcard/giftcards/search/{search_query}": {
      "get": { "summary": "Search gift cards", "tags": ["giftcard"], "parameters": [{ "name": "search_query", "in": "path", "required": false, "schema": { "type": "string" } }], "responses": { "200": { "description": "OK" } } }
    },
    "/giftcard/calculate-sell-price/{type}": {
      "post": { "summary": "Calculate a sell price", "tags": ["giftcard"], "parameters": [{ "name": "type", "in": "path", "required": false, "schema": { "type": "string" } }], "responses": { "200": { "description": "OK" } } }
    },
    "/giftcard/sell": {
      "post": { "summary": "Create a sales offer", "tags": ["giftcard"], "responses": { "200": { "description": "OK" } } }
    },
    "/farm/farmers": {
      "get": { "summary": "List registered farmers", "tags": ["farm"], "responses": { "200": { "description": "OK" } } },
      "post": { "summary": "Register a farmer", "tags": ["farm"], "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "properties": { "identity_id": { "type": "string" }, "full_name": { "type": "string" }, "phone": { "type": "string" } } } } } }, "responses": { "201": { "description": "Created" } } }
    },
    "/farm/opportunities": {
      "get": { "summary": "List opportunities/grants", "tags": ["farm"], "responses": { "200": { "description": "OK" } } }
    },
    "/farm/opportunities/{opportunityId}/apply": {
      "post": { "summary": "Apply to an opportunity", "tags": ["farm"], "parameters": [{ "name": "opportunityId", "in": "path", "required": true, "schema": { "type": "string" } }], "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "properties": { "farmer_id": { "type": "string" } } } } } }, "responses": { "200": { "description": "OK" }, "404": { "description": "Not found" } } }
    },
    "/zimo/courses": {
      "get": { "summary": "List published courses", "tags": ["zimo"], "responses": { "200": { "description": "OK" } } }
    },
    "/zimo/courses/{courseId}/enroll": {
      "post": { "summary": "Enroll a learner in a course", "tags": ["zimo"], "parameters": [{ "name": "courseId", "in": "path", "required": true, "schema": { "type": "string" } }], "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "properties": { "identity_id": { "type": "string" } } } } } }, "responses": { "201": { "description": "Created" }, "404": { "description": "Course not found" } } }
    },
    "/zimo/leaderboard": {
      "get": { "summary": "Top learners by rewards/achievements", "tags": ["zimo"], "parameters": [{ "name": "limit", "in": "query", "required": false, "schema": { "type": "integer", "default": 10 } }], "responses": { "200": { "description": "OK" } } }
    },
    "/marketplace/products": {
      "get": { "summary": "List active products", "tags": ["marketplace"], "responses": { "200": { "description": "OK" } } }
    },
    "/marketplace/orders": {
      "post": {
        "summary": "Place an order as the authenticated buyer",
        "description": "The buyer identity comes from the authenticated user. Optional buyer_identity_id must match that user.",
        "tags": ["marketplace"],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["items"],
                "properties": {
                  "buyer_identity_id": { "type": "string", "description": "Optional legacy field; must match the authenticated user." },
                  "items": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                      "type": "object",
                      "required": ["product_id", "quantity"],
                      "properties": {
                        "product_id": { "type": "integer" },
                        "quantity": { "type": "integer", "minimum": 1 },
                        "offer_id": { "type": "integer" }
                      }
                    }
                  },
                  "trade_terms": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "201": { "description": "Created" },
          "403": { "description": "Buyer identity does not match the authenticated user" },
          "422": { "description": "Invalid order" }
        }
      }
    },
    "/marketplace/orders/{id}": {
      "get": { "summary": "Get an order", "tags": ["marketplace"], "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }], "responses": { "200": { "description": "OK" }, "404": { "description": "Not found" } } }
    },
    "/marketplace/orders/{id}/yaocrypt-intent": {
      "post": {
        "summary": "Issue a buyer-bound Yaocrypt payment intent",
        "description": "Requires the authenticated buyer of a pending order. The returned intent expires after 24 hours.",
        "tags": ["marketplace"],
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": {
          "201": { "description": "Created; data contains token and expires_in in seconds" },
          "403": { "description": "Caller is not the order buyer" },
          "422": { "description": "Order is not awaiting payment or cannot be paid in the current wallet environment" }
        }
      }
    },
    "/marketplace/settlements/mine": {
      "get": {
        "summary": "List the authenticated seller's settlement records",
        "description": "Returns only settlements belonging to the authenticated seller's HAOS account.",
        "tags": ["marketplace"],
        "responses": { "200": { "description": "Settlement records for the authenticated seller" } }
      }
    },
    "/marketplace/webhooks": {
      "get": {
        "summary": "List the authenticated merchant's webhook endpoints",
        "tags": ["marketplace", "webhooks"],
        "responses": { "200": { "description": "Endpoints owned by the authenticated merchant" } }
      },
      "post": {
        "summary": "Register a payment webhook endpoint",
        "description": "Requires a SupplierCredential. Only public HTTPS destinations on port 443 are accepted. The signing secret is returned only once.",
        "tags": ["marketplace", "webhooks"],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["label", "url"],
                "properties": {
                  "label": { "type": "string", "maxLength": 120 },
                  "url": { "type": "string", "format": "uri", "example": "https://merchant.example/hooks/yaocrypt" },
                  "events": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "enum": ["payment.succeeded", "payment.partially_paid", "payment.failed", "payment.expired"]
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": { "description": "Created; response includes endpoint and one-time signing secret" },
          "422": { "description": "Invalid callback URL or event configuration" }
        }
      }
    },
    "/marketplace/webhooks/{id}": {
      "patch": {
        "summary": "Enable or disable a webhook endpoint",
        "tags": ["marketplace", "webhooks"],
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["active"],
                "properties": { "active": { "type": "boolean" } }
              }
            }
          }
        },
        "responses": { "200": { "description": "Endpoint state updated" }, "404": { "description": "Endpoint not found for this merchant" } }
      }
    },
    "/marketplace/webhooks/{id}/rotate-secret": {
      "post": {
        "summary": "Rotate an endpoint's signing secret",
        "description": "The new secret is returned once and takes effect immediately.",
        "tags": ["marketplace", "webhooks"],
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } }],
        "responses": { "200": { "description": "New endpoint secret" }, "404": { "description": "Endpoint not found for this merchant" } }
      }
    },
    "/marketplace/webhooks/{id}/deliveries": {
      "get": {
        "summary": "List recent webhook deliveries",
        "tags": ["marketplace", "webhooks"],
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } }],
        "responses": { "200": { "description": "Recent delivery attempts and outcomes" }, "404": { "description": "Endpoint not found for this merchant" } }
      }
    },
    "/marketplace/webhooks/{id}/deliveries/{deliveryId}/retry": {
      "post": {
        "summary": "Queue another attempt for an undelivered event",
        "tags": ["marketplace", "webhooks"],
        "parameters": [
          { "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } },
          { "name": "deliveryId", "in": "path", "required": true, "schema": { "type": "integer" } }
        ],
        "responses": { "200": { "description": "Delivery queued" }, "404": { "description": "Undelivered event not found for this merchant endpoint" } }
      }
    }
  }
}
