{
  "openapi": "3.1.0",
  "info": {
    "title": "Cashew - Morpheus Blockchain Explorer API",
    "version": "1.0.0",
    "description": "Real-time Morpheus blockchain explorer API. Provider, bid, session, and model data with sub-400ms response times. Fetches model names from on-chain ModelRegistry and tracks retracted bid history. All /mor/v1/* endpoints require a valid API key via X-Cashew-Key header."
  },
  "servers": [
    {
      "url": "https://cashew.explorer.drm3.network",
      "description": "Production"
    }
  ],
  "paths": {
    "/health": {
      "get": {
        "summary": "Health Check",
        "description": "Returns sync status, block heights, and database counts.",
        "tags": [
          "Status"
        ],
        "responses": {
          "200": {
            "description": "Health status",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HealthResponse"
                }
              }
            }
          }
        }
      }
    },
    "/teaser": {
      "get": {
        "summary": "Public Stats",
        "description": "Quick stats without authentication - for splash screens.",
        "tags": [
          "Status"
        ],
        "responses": {
          "200": {
            "description": "Basic stats",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TeaserResponse"
                }
              }
            }
          }
        }
      }
    },
    "/mor/v1/all": {
      "get": {
        "summary": "Full Marketplace",
        "description": "All providers with their bids (active + retracted), model names from chain.",
        "tags": [
          "Marketplace"
        ],
        "security": [
          {
            "apiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Full marketplace data",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MarketplaceResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/mor/v1/providers": {
      "get": {
        "summary": "Provider List",
        "description": "All registered providers with endpoint info.",
        "tags": [
          "Marketplace"
        ],
        "security": [
          {
            "apiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Provider list",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProvidersResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/mor/v1/bids": {
      "get": {
        "summary": "All Bids",
        "description": "All bids with model names and pricing.",
        "tags": [
          "Marketplace"
        ],
        "security": [
          {
            "apiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Bid list",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BidsResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/mor/v1/models": {
      "get": {
        "summary": "Model Registry",
        "description": "All registered models with human-readable names from chain.",
        "tags": [
          "Models"
        ],
        "security": [
          {
            "apiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Model list",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ModelsResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/mor/v1/models/lookup": {
      "get": {
        "summary": "Model ID Lookup",
        "description": "Mapping of model IDs to human-readable names.",
        "tags": [
          "Models"
        ],
        "security": [
          {
            "apiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Model ID → name mapping",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": {
                    "type": "string"
                  },
                  "example": {
                    "0x972f711716...": "qwen3-235b:web"
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/mor/v1/sessions": {
      "get": {
        "summary": "All Sessions",
        "description": "Paginated list of all sessions (active + closed).",
        "tags": [
          "Sessions"
        ],
        "security": [
          {
            "apiKey": []
          }
        ],
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 100
            }
          },
          {
            "name": "offset",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 0
            }
          },
          {
            "name": "active",
            "in": "query",
            "schema": {
              "type": "boolean"
            },
            "description": "Filter active only"
          }
        ],
        "responses": {
          "200": {
            "description": "Session list",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SessionsResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/mor/v1/sessions/{wallet}": {
      "get": {
        "summary": "Wallet Sessions",
        "description": "Sessions for a specific wallet address.",
        "tags": [
          "Sessions"
        ],
        "security": [
          {
            "apiKey": []
          }
        ],
        "parameters": [
          {
            "name": "wallet",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "0x wallet address"
          }
        ],
        "responses": {
          "200": {
            "description": "Wallet sessions",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SessionsResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/mor/v1/sessions/analytics": {
      "get": {
        "summary": "Session Analytics",
        "description": "Per-wallet analytics with claimable stake tracking.",
        "tags": [
          "Sessions"
        ],
        "security": [
          {
            "apiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Analytics data",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AnalyticsResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/mor/v1/wallet/{wallet}": {
      "get": {
        "summary": "Wallet Detail",
        "description": "Full wallet breakdown with ETH/MOR balances and session history.",
        "tags": [
          "Sessions"
        ],
        "security": [
          {
            "apiKey": []
          }
        ],
        "parameters": [
          {
            "name": "wallet",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "0x wallet address"
          }
        ],
        "responses": {
          "200": {
            "description": "Wallet detail",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WalletDetailResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "apiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "X-Cashew-Key",
        "description": "API key for authentication (format: cshw_xxxxx)"
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "Missing or invalid API key",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "error": {
                  "type": "string",
                  "example": "Invalid API key"
                }
              }
            }
          }
        }
      }
    },
    "schemas": {
      "SyncMetadata": {
        "type": "object",
        "properties": {
          "currentBlock": {
            "type": "integer",
            "description": "Latest block on chain"
          },
          "syncedBlock": {
            "type": "integer",
            "description": "Last synced block"
          },
          "blocksBehind": {
            "type": "integer",
            "description": "How many blocks behind"
          },
          "lastSyncTs": {
            "type": "string",
            "format": "date-time"
          },
          "timestamp": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "HealthResponse": {
        "type": "object",
        "properties": {
          "status": {
            "type": "string",
            "enum": [
              "healthy",
              "degraded"
            ]
          },
          "currentBlock": {
            "type": "integer"
          },
          "syncedBlock": {
            "type": "integer"
          },
          "blocksBehind": {
            "type": "integer"
          },
          "providers": {
            "type": "integer"
          },
          "bids": {
            "type": "integer"
          },
          "sessions": {
            "type": "integer"
          },
          "activeSessions": {
            "type": "integer"
          }
        }
      },
      "TeaserResponse": {
        "type": "object",
        "properties": {
          "providers": {
            "type": "integer"
          },
          "bids": {
            "type": "integer"
          },
          "activeSessions": {
            "type": "integer"
          },
          "totalSessions": {
            "type": "integer"
          },
          "morStaked": {
            "type": "integer",
            "description": "Total MOR staked in active sessions"
          }
        }
      },
      "Provider": {
        "type": "object",
        "properties": {
          "address": {
            "type": "string",
            "description": "0x provider address"
          },
          "endpoint": {
            "type": "string",
            "description": "Provider RPC endpoint"
          },
          "stake": {
            "type": "string",
            "description": "Provider stake in wei"
          },
          "bidCount": {
            "type": "integer",
            "description": "Active bid count"
          },
          "retractedBidCount": {
            "type": "integer",
            "description": "Retracted bid count"
          },
          "bids": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Bid"
            }
          },
          "retractedBids": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/RetractedBid"
            }
          }
        }
      },
      "Bid": {
        "type": "object",
        "properties": {
          "bidId": {
            "type": "string",
            "description": "bytes32 bid ID"
          },
          "provider": {
            "type": "string"
          },
          "modelId": {
            "type": "string",
            "description": "bytes32 model ID"
          },
          "model": {
            "type": "string",
            "description": "Human-readable model name"
          },
          "tags": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "pricePerSecond": {
            "type": "string",
            "description": "Price in wei per second"
          },
          "priceMorPerDay": {
            "type": "string",
            "description": "Price in MOR per day"
          },
          "priceMorPerWeek": {
            "type": "string",
            "description": "Price in MOR per week"
          }
        }
      },
      "RetractedBid": {
        "type": "object",
        "properties": {
          "bidId": {
            "type": "string"
          },
          "model": {
            "type": "string"
          },
          "priceMorPerDay": {
            "type": "string"
          },
          "retractedAt": {
            "type": "integer",
            "description": "Unix timestamp when retracted"
          }
        }
      },
      "Session": {
        "type": "object",
        "properties": {
          "sessionId": {
            "type": "string"
          },
          "user": {
            "type": "string",
            "description": "User wallet address"
          },
          "provider": {
            "type": "string"
          },
          "bidId": {
            "type": "string"
          },
          "modelId": {
            "type": "string"
          },
          "model": {
            "type": "string"
          },
          "stake": {
            "type": "string",
            "description": "Staked amount in wei"
          },
          "stakeMor": {
            "type": "number",
            "description": "Staked amount in MOR"
          },
          "isActive": {
            "type": "boolean"
          },
          "openedAt": {
            "type": "integer"
          },
          "closedAt": {
            "type": "integer",
            "nullable": true
          },
          "endsAt": {
            "type": "integer"
          }
        }
      },
      "MarketplaceResponse": {
        "allOf": [
          {
            "$ref": "#/components/schemas/SyncMetadata"
          },
          {
            "type": "object",
            "properties": {
              "providers": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Provider"
                }
              }
            }
          }
        ]
      },
      "ProvidersResponse": {
        "allOf": [
          {
            "$ref": "#/components/schemas/SyncMetadata"
          },
          {
            "type": "object",
            "properties": {
              "providers": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Provider"
                }
              }
            }
          }
        ]
      },
      "BidsResponse": {
        "allOf": [
          {
            "$ref": "#/components/schemas/SyncMetadata"
          },
          {
            "type": "object",
            "properties": {
              "bids": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Bid"
                }
              }
            }
          }
        ]
      },
      "ModelsResponse": {
        "allOf": [
          {
            "$ref": "#/components/schemas/SyncMetadata"
          },
          {
            "type": "object",
            "properties": {
              "models": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "modelId": {
                      "type": "string"
                    },
                    "name": {
                      "type": "string"
                    },
                    "tags": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    }
                  }
                }
              }
            }
          }
        ]
      },
      "SessionsResponse": {
        "allOf": [
          {
            "$ref": "#/components/schemas/SyncMetadata"
          },
          {
            "type": "object",
            "properties": {
              "sessions": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Session"
                }
              },
              "total": {
                "type": "integer"
              }
            }
          }
        ]
      },
      "AnalyticsResponse": {
        "allOf": [
          {
            "$ref": "#/components/schemas/SyncMetadata"
          },
          {
            "type": "object",
            "properties": {
              "wallets": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "wallet": {
                      "type": "string"
                    },
                    "activeSessions": {
                      "type": "integer"
                    },
                    "totalSessions": {
                      "type": "integer"
                    },
                    "totalStakedMor": {
                      "type": "number"
                    }
                  }
                }
              }
            }
          }
        ]
      },
      "WalletDetailResponse": {
        "allOf": [
          {
            "$ref": "#/components/schemas/SyncMetadata"
          },
          {
            "type": "object",
            "properties": {
              "wallet": {
                "type": "string"
              },
              "ethBalance": {
                "type": "string"
              },
              "morBalance": {
                "type": "string"
              },
              "activeSessions": {
                "type": "integer"
              },
              "totalSessions": {
                "type": "integer"
              },
              "sessions": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Session"
                }
              }
            }
          }
        ]
      }
    }
  }
}