{
  "openapi": "3.1.0",
  "info": {
    "title": "Luiri Gaming Operator API",
    "version": "1.0.0",
    "description": "Signed B2B launch, game session, round, and wallet callback contract for Luiri Gaming operators."
  },
  "servers": [
    {
      "url": "https://luirigaming.com",
      "description": "Production"
    },
    {
      "url": "http://localhost:3000",
      "description": "Local staging"
    }
  ],
  "tags": [
    {
      "name": "Operator API",
      "description": "Server-side APIs called by the operator backend."
    },
    {
      "name": "Game Runtime",
      "description": "APIs called by the Unity WebGL runtime after a signed launch token exists."
    }
  ],
  "paths": {
    "/api/v1/games": {
      "get": {
        "tags": ["Operator API"],
        "summary": "List games available on the Luiri platform",
        "responses": {
          "200": {
            "description": "Game catalog",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GameCatalogResponse"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/launch": {
      "post": {
        "tags": ["Operator API"],
        "summary": "Create a short-lived iframe launch session",
        "description": "Call from the operator backend only. The browser must never receive the Luiri API secret.",
        "security": [
          {
            "LuiriApiKey": [],
            "LuiriSignature": []
          }
        ],
        "parameters": [
          {
            "name": "x-luiri-idempotency-key",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string",
              "maxLength": 160
            },
            "description": "Optional replay-safe key for launch request tracking."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/LaunchRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Launch token and iframe URL",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/LaunchResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "409": {
            "$ref": "#/components/responses/ReplayRejected"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/api/game/session/{token}/config": {
      "get": {
        "tags": ["Game Runtime"],
        "summary": "Return game config for a launch token",
        "parameters": [
          {
            "name": "token",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Session, game, settings, wallet snapshot, and runtime endpoints",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SessionConfigResponse"
                }
              }
            }
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/api/game/rounds/start": {
      "post": {
        "tags": ["Game Runtime"],
        "summary": "Start a round and debit the operator wallet",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RoundStartRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Started round and updated balance",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RoundResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          }
        }
      }
    },
    "/api/game/rounds/settle": {
      "post": {
        "tags": ["Game Runtime"],
        "summary": "Settle a round and credit payout when needed",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RoundSettleRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Settled round and updated balance",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RoundResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          }
        }
      }
    },
    "/api/game/rounds/rollback": {
      "post": {
        "tags": ["Game Runtime"],
        "summary": "Rollback a started round and reverse the debit",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RollbackRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Rolled-back round and updated balance",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RoundResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          }
        }
      }
    }
  },
  "webhooks": {
    "operatorWalletBalance": {
      "post": {
        "summary": "Luiri asks the operator wallet for current balance",
        "security": [
          {
            "LuiriWalletSignature": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/WalletBalanceRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "$ref": "#/components/responses/WalletOk"
          }
        }
      }
    },
    "operatorWalletTransaction": {
      "post": {
        "summary": "Luiri asks the operator wallet to debit, credit, or rollback",
        "security": [
          {
            "LuiriWalletSignature": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/WalletTransactionRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "$ref": "#/components/responses/WalletOk"
          },
          "409": {
            "description": "Duplicate or conflicting wallet operation",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "LuiriApiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "x-luiri-key"
      },
      "LuiriSignature": {
        "type": "apiKey",
        "in": "header",
        "name": "x-luiri-signature",
        "description": "Format: t={timestamp},v1={HMAC_SHA256(apiSecret, timestamp + '.' + rawBody)}"
      },
      "LuiriWalletSignature": {
        "type": "apiKey",
        "in": "header",
        "name": "x-luiri-wallet-signature",
        "description": "Format: t={timestamp},v1={HMAC_SHA256(walletSecret, timestamp + '.' + rawBody)}"
      }
    },
    "responses": {
      "BadRequest": {
        "description": "Validation or lifecycle error",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "Unauthorized": {
        "description": "Missing API key or invalid signature",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "ReplayRejected": {
        "description": "Exact signed request replay was rejected",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "RateLimited": {
        "description": "Operator launch rate limit exceeded",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "NotFound": {
        "description": "Invalid or expired resource",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "WalletOk": {
        "description": "Operator wallet accepted the callback",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/WalletResponse"
            }
          }
        }
      }
    },
    "schemas": {
      "GameCatalogResponse": {
        "type": "object",
        "required": ["data"],
        "properties": {
          "data": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Game"
            }
          }
        }
      },
      "Game": {
        "type": "object",
        "required": ["id", "slug", "title", "status"],
        "properties": {
          "id": {
            "type": "string",
            "example": "jetx-cash"
          },
          "slug": {
            "type": "string",
            "example": "jetx-cash"
          },
          "title": {
            "type": "string",
            "example": "JetX Cash"
          },
          "category": {
            "type": "string",
            "example": "Crash Game"
          },
          "status": {
            "type": "string",
            "enum": ["live", "coming-soon", "disabled"]
          },
          "demoAvailable": {
            "type": "boolean"
          },
          "buildPath": {
            "type": ["string", "null"],
            "example": "/games/jetx-cash/webgl/index.html"
          }
        }
      },
      "LaunchRequest": {
        "type": "object",
        "required": ["operatorPlayerId"],
        "properties": {
          "gameId": {
            "type": "string",
            "default": "jetx-cash"
          },
          "operatorPlayerId": {
            "type": "string",
            "example": "player_10291"
          },
          "currency": {
            "type": "string",
            "example": "INR"
          },
          "locale": {
            "type": "string",
            "example": "en"
          },
          "originDomain": {
            "type": "string",
            "example": "https://casino.example"
          },
          "returnUrl": {
            "type": "string",
            "example": "https://casino.example/lobby"
          }
        }
      },
      "LaunchResponse": {
        "type": "object",
        "required": ["data"],
        "properties": {
          "data": {
            "type": "object",
            "required": ["token", "launchUrl", "expiresAt", "iframe"],
            "properties": {
              "token": {
                "type": "string",
                "example": "lg_..."
              },
              "launchUrl": {
                "type": "string",
                "example": "https://luirigaming.com/play/lg_..."
              },
              "expiresAt": {
                "type": "string",
                "format": "date-time"
              },
              "iframe": {
                "type": "string"
              }
            }
          }
        }
      },
      "SessionConfigResponse": {
        "type": "object",
        "required": ["data"],
        "properties": {
          "data": {
            "type": "object",
            "required": ["session", "game", "settings", "wallet", "bridge"],
            "properties": {
              "session": {
                "$ref": "#/components/schemas/PublicSession"
              },
              "game": {
                "$ref": "#/components/schemas/Game"
              },
              "settings": {
                "$ref": "#/components/schemas/GameSettings"
              },
              "wallet": {
                "$ref": "#/components/schemas/WalletSnapshot"
              },
              "bridge": {
                "$ref": "#/components/schemas/RuntimeBridge"
              }
            }
          }
        }
      },
      "PublicSession": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "example": "ses_..."
          },
          "gameId": {
            "type": "string"
          },
          "operatorPlayerId": {
            "type": "string"
          },
          "currency": {
            "type": "string"
          },
          "status": {
            "type": "string"
          },
          "expiresAt": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "GameSettings": {
        "type": "object",
        "properties": {
          "gameId": {
            "type": "string"
          },
          "difficulty": {
            "type": "number",
            "description": "Luiri admin-controlled game difficulty."
          },
          "minBet": {
            "type": "number"
          },
          "maxBet": {
            "type": "number"
          },
          "currencyCode": {
            "type": "string"
          },
          "currencyIconUrl": {
            "type": "string"
          },
          "enabled": {
            "type": "boolean"
          }
        }
      },
      "WalletSnapshot": {
        "type": "object",
        "properties": {
          "balance": {
            "type": "number"
          },
          "currency": {
            "type": "string"
          },
          "mode": {
            "type": "string",
            "enum": ["test", "live", "demo"]
          }
        }
      },
      "RuntimeBridge": {
        "type": "object",
        "properties": {
          "endpoints": {
            "type": "object",
            "properties": {
              "startRound": {
                "type": "string"
              },
              "settleRound": {
                "type": "string"
              },
              "rollbackRound": {
                "type": "string"
              }
            }
          },
          "features": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      },
      "RoundStartRequest": {
        "type": "object",
        "required": ["token", "betAmount", "idempotencyKey"],
        "properties": {
          "token": {
            "type": "string"
          },
          "betAmount": {
            "type": "number",
            "minimum": 0
          },
          "idempotencyKey": {
            "type": "string"
          }
        }
      },
      "RoundSettleRequest": {
        "type": "object",
        "required": ["token", "roundId", "multiplier", "idempotencyKey"],
        "properties": {
          "token": {
            "type": "string"
          },
          "roundId": {
            "type": "string"
          },
          "multiplier": {
            "type": "number"
          },
          "winAmount": {
            "type": "number",
            "description": "Optional client hint; Luiri authorizes payout server-side."
          },
          "idempotencyKey": {
            "type": "string"
          }
        }
      },
      "RollbackRequest": {
        "type": "object",
        "required": ["token", "roundId", "idempotencyKey"],
        "properties": {
          "token": {
            "type": "string"
          },
          "roundId": {
            "type": "string"
          },
          "idempotencyKey": {
            "type": "string"
          },
          "reason": {
            "type": "string"
          }
        }
      },
      "RoundResponse": {
        "type": "object",
        "required": ["data"],
        "properties": {
          "data": {
            "type": "object",
            "properties": {
              "session": {
                "$ref": "#/components/schemas/PublicSession"
              },
              "round": {
                "$ref": "#/components/schemas/Round"
              },
              "balance": {
                "type": "number"
              },
              "idempotencyKey": {
                "type": ["string", "null"]
              }
            }
          }
        }
      },
      "Round": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "betAmount": {
            "type": "number"
          },
          "winAmount": {
            "type": "number"
          },
          "multiplier": {
            "type": "number"
          },
          "roundGgr": {
            "type": "number"
          },
          "platformFeeDelta": {
            "type": "number"
          },
          "status": {
            "type": "string",
            "enum": ["started", "settled", "rolled-back", "failed"]
          }
        }
      },
      "WalletBalanceRequest": {
        "type": "object",
        "required": ["operatorId", "playerId", "currency"],
        "properties": {
          "operatorId": {
            "type": "string"
          },
          "playerId": {
            "type": "string"
          },
          "currency": {
            "type": "string"
          }
        }
      },
      "WalletTransactionRequest": {
        "type": "object",
        "required": ["transactionId", "idempotencyKey", "playerId", "sessionId", "roundId", "type", "amount", "currency"],
        "properties": {
          "transactionId": {
            "type": "string"
          },
          "idempotencyKey": {
            "type": "string"
          },
          "operatorId": {
            "type": "string"
          },
          "playerId": {
            "type": "string"
          },
          "sessionId": {
            "type": "string"
          },
          "roundId": {
            "type": "string"
          },
          "type": {
            "type": "string",
            "enum": ["debit", "credit", "rollback"]
          },
          "amount": {
            "type": "number"
          },
          "currency": {
            "type": "string"
          }
        }
      },
      "WalletResponse": {
        "type": "object",
        "required": ["balance"],
        "properties": {
          "balance": {
            "type": "number"
          },
          "currency": {
            "type": "string"
          },
          "externalRef": {
            "type": "string"
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": {
            "type": "string"
          }
        }
      }
    }
  }
}
