{
  "openapi": "3.0.3",
  "info": {
    "title": "Nonogram Board Audit",
    "version": "1.0.0",
    "description": "Checks monochrome nonograms up to 25 by 25 for contradictions, deductions, and uniqueness. The audit routes cost 0.05 USDC on Base per call through x402. The example and preflight routes are free."
  },
  "servers": [{ "url": "https://nonogram-board-audit.run402.com" }],
  "paths": {
    "/example": {
      "get": {
        "operationId": "getExample",
        "summary": "Read a free worked example",
        "responses": {
          "200": {
            "description": "A board with its audit result.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/WorkedBoard" } } }
          }
        }
      }
    },
    "/preflight": {
      "post": {
        "operationId": "validateBoard",
        "summary": "Validate clues and board dimensions for free",
        "requestBody": { "$ref": "#/components/requestBodies/Board" },
        "responses": {
          "200": {
            "description": "The board passed input validation.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Validation" } } }
          },
          "400": { "$ref": "#/components/responses/InvalidJson" },
          "413": { "$ref": "#/components/responses/LargeBody" },
          "422": { "$ref": "#/components/responses/InvalidBoard" }
        }
      }
    },
    "/audit": {
      "post": {
        "operationId": "auditBoard",
        "summary": "Audit a custom board for 0.05 USDC on Base",
        "parameters": [{ "$ref": "#/components/parameters/PaymentSignature" }],
        "requestBody": { "$ref": "#/components/requestBodies/Board" },
        "responses": {
          "200": {
            "description": "The paid audit result.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/AuditResult" } } }
          },
          "400": { "$ref": "#/components/responses/InvalidJson" },
          "402": { "$ref": "#/components/responses/PaymentRequired" },
          "413": { "$ref": "#/components/responses/LargeBody" },
          "422": { "$ref": "#/components/responses/InvalidBoard" }
        }
      }
    },
    "/check": {
      "get": {
        "operationId": "checkBoard",
        "summary": "Audit a custom or daily board for 0.05 USDC on Base",
        "parameters": [
          {
            "name": "board",
            "in": "query",
            "description": "URL-encoded JSON using the Board schema. When omitted, the result includes the daily 12 by 12 board and its UTC date.",
            "schema": { "type": "string", "minLength": 1, "maxLength": 4000 },
            "example": "{\"rows\":[[1],[1]],\"columns\":[[1],[1]],\"grid\":[\"#?\",\"??\"]}"
          },
          { "$ref": "#/components/parameters/PaymentSignature" }
        ],
        "responses": {
          "200": {
            "description": "A custom audit result, or a dated daily board with its audit result.",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    { "$ref": "#/components/schemas/AuditResult" },
                    { "$ref": "#/components/schemas/DailyBoard" }
                  ]
                }
              }
            }
          },
          "402": { "$ref": "#/components/responses/PaymentRequired" },
          "422": { "$ref": "#/components/responses/InvalidBoard" }
        }
      }
    }
  },
  "components": {
    "parameters": {
      "PaymentSignature": {
        "name": "PAYMENT-SIGNATURE",
        "in": "header",
        "description": "The signed x402 v2 payment payload. An initial unpaid request returns the terms in PAYMENT-REQUIRED.",
        "schema": { "type": "string" }
      }
    },
    "requestBodies": {
      "Board": {
        "required": true,
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Board" },
            "example": { "rows": [[1], [1]], "columns": [[1], [1]], "grid": ["#?", "??"] }
          }
        }
      }
    },
    "responses": {
      "PaymentRequired": {
        "description": "An x402 payment of 0.05 USDC on Base (eip155:8453) is required.",
        "headers": {
          "PAYMENT-REQUIRED": {
            "description": "Base64-encoded x402 v2 terms, including the recipient and amount in atomic units.",
            "schema": { "type": "string", "format": "byte" }
          }
        }
      },
      "InvalidJson": {
        "description": "The body is not valid JSON.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "LargeBody": {
        "description": "The body exceeds 16,384 characters.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "InvalidBoard": {
        "description": "The board or its query encoding is invalid.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      }
    },
    "schemas": {
      "SideLength": { "type": "integer", "minimum": 1, "maximum": 25 },
      "ClueLines": {
        "type": "array",
        "minItems": 1,
        "maxItems": 25,
        "description": "One array of run lengths per line. Each line's runs and separators must fit its length. Use an empty array for an empty line.",
        "items": { "type": "array", "maxItems": 13, "items": { "$ref": "#/components/schemas/SideLength" } }
      },
      "GridLine": { "type": "string", "minLength": 1, "maxLength": 25, "pattern": "^[#.?]+$" },
      "Board": {
        "type": "object",
        "additionalProperties": false,
        "required": ["rows", "columns"],
        "properties": {
          "rows": { "$ref": "#/components/schemas/ClueLines" },
          "columns": { "$ref": "#/components/schemas/ClueLines" },
          "grid": {
            "type": "array",
            "minItems": 1,
            "maxItems": 25,
            "description": "One string per row, with one character per column. Use # for filled, . for empty, and ? for unknown. Omit this field for a fully unknown board.",
            "items": { "$ref": "#/components/schemas/GridLine" }
          }
        }
      },
      "Validation": {
        "type": "object",
        "required": ["valid", "height", "width"],
        "properties": {
          "valid": { "type": "boolean", "enum": [true] },
          "height": { "$ref": "#/components/schemas/SideLength" },
          "width": { "$ref": "#/components/schemas/SideLength" }
        }
      },
      "AuditResult": {
        "type": "object",
        "required": ["status", "height", "width", "deduced_grid", "solutions", "contradiction", "line_checks", "line_check_limit"],
        "properties": {
          "status": {
            "type": "string",
            "enum": ["unique", "multiple", "contradiction", "undetermined"],
            "description": "Undetermined indicates that the search reached its line check limit before proving the result."
          },
          "height": { "$ref": "#/components/schemas/SideLength" },
          "width": { "$ref": "#/components/schemas/SideLength" },
          "deduced_grid": {
            "type": "array",
            "nullable": true,
            "description": "Board after line propagation, or null if no solution exists.",
            "minItems": 1,
            "maxItems": 25,
            "items": { "$ref": "#/components/schemas/GridLine" }
          },
          "solutions": {
            "type": "array",
            "maxItems": 2,
            "items": {
              "type": "array",
              "minItems": 1,
              "maxItems": 25,
              "items": { "type": "string", "minLength": 1, "maxLength": 25, "pattern": "^[#.]+$" }
            }
          },
          "contradiction": {
            "type": "object",
            "nullable": true,
            "description": "The first direct conflict found during line propagation, if any.",
            "required": ["axis", "line"],
            "properties": {
              "axis": { "type": "string", "enum": ["row", "column"] },
              "line": { "$ref": "#/components/schemas/SideLength" }
            }
          },
          "line_checks": { "type": "integer", "minimum": 0, "maximum": 4096 },
          "line_check_limit": { "type": "integer", "enum": [4096] }
        }
      },
      "WorkedBoard": {
        "type": "object",
        "required": ["input", "result"],
        "properties": {
          "input": { "$ref": "#/components/schemas/Board" },
          "result": { "$ref": "#/components/schemas/AuditResult" }
        }
      },
      "DailyBoard": {
        "allOf": [
          { "$ref": "#/components/schemas/WorkedBoard" },
          {
            "type": "object",
            "required": ["date"],
            "properties": { "date": { "type": "string", "format": "date" } }
          }
        ]
      },
      "Error": {
        "type": "object",
        "required": ["error"],
        "properties": { "error": { "type": "string" } }
      }
    }
  }
}
