{
  "$comment": "Contract for opportunities.json, the Phase 4 output of a full hunt. validate-opportunities.cjs reads this schema directly and adds the cross-field checks that are clearer in code.",
  "type": "array",
  "maxItems": 500,
  "items": {
    "oneOf": [
      {
        "type": "object",
        "description": "An app with proven demand and a weakness that Phase 3 failed to disprove and Phase 5 reproduced.",
        "required": ["verdict", "fingerprint", "app", "sources", "score", "demand", "weaknesses", "estimate", "complaint_themes", "monetization", "wedge", "verification"],
        "additionalProperties": false,
        "properties": {
          "verdict": { "const": "confirmed_opportunity" },
          "fingerprint": { "$ref": "#/definitions/fingerprint" },
          "app": { "$ref": "#/definitions/app" },
          "sources": { "$ref": "#/definitions/sources" },
          "score": { "$ref": "#/definitions/score" },
          "demand": {
            "type": "object",
            "description": "Where the demand comes from: the chart the app was found on and its rating volume.",
            "required": ["chart", "genre", "rank", "rating", "rating_count"],
            "additionalProperties": false,
            "properties": {
              "chart": { "enum": ["free", "paid", "grossing"] },
              "genre": { "type": "string", "minLength": 1, "maxLength": 60 },
              "rank": { "type": ["integer", "null"], "minimum": 1, "maximum": 100 },
              "rating": { "type": ["number", "null"], "minimum": 0, "maximum": 5 },
              "rating_count": { "type": ["integer", "null"], "minimum": 0 },
              "installs": { "type": ["integer", "null"], "minimum": 0 }
            }
          },
          "weaknesses": {
            "type": "array",
            "minItems": 1,
            "maxItems": 10,
            "items": {
              "type": "object",
              "required": ["signal", "detail"],
              "additionalProperties": false,
              "properties": {
                "signal": { "enum": ["low_rating", "stale", "one_star_heavy", "complaint_theme", "pricing_gap", "missing_platform"] },
                "detail": { "$ref": "#/definitions/prose" }
              }
            }
          },
          "estimate": {
            "type": "object",
            "description": "Ranges from the engine, never one number. label is fixed so a reader cannot mistake it for reported revenue.",
            "required": ["label", "downloads_monthly", "mrr", "currency", "velocity_source"],
            "additionalProperties": false,
            "properties": {
              "label": { "const": "estimate" },
              "downloads_monthly": { "$ref": "#/definitions/range" },
              "mrr": { "type": ["array", "null"], "description": "null when the app has no price or IAP to read.", "minItems": 2, "maxItems": 2, "items": { "type": "number", "minimum": 0 } },
              "currency": { "type": ["string", "null"], "pattern": "^[A-Z]{3}$" },
              "velocity_source": { "enum": ["snapshot", "lifetime", "installs"] }
            }
          },
          "complaint_themes": {
            "type": "array",
            "maxItems": 10,
            "items": {
              "type": "object",
              "required": ["theme", "share", "quotes"],
              "additionalProperties": false,
              "properties": {
                "theme": { "enum": ["Crashes and bugs", "Paywall and pricing", "Ads", "Login and account", "Sync and data loss", "Speed and battery", "Missing features", "Design and usability", "Customer support", "Notifications and spam"] },
                "share": { "type": "number", "minimum": 0, "maximum": 1 },
                "quotes": { "type": "array", "maxItems": 2, "items": { "type": "string", "minLength": 1, "maxLength": 160 } }
              }
            }
          },
          "monetization": {
            "type": "object",
            "required": ["model", "tiers"],
            "additionalProperties": false,
            "properties": {
              "model": { "enum": ["subscription", "iap_packs", "paid", "ads_only", "free"] },
              "tiers": {
                "type": "array",
                "maxItems": 30,
                "items": {
                  "type": "object",
                  "required": ["name", "price", "period"],
                  "additionalProperties": false,
                  "properties": {
                    "name": { "type": "string", "minLength": 1, "maxLength": 120 },
                    "price": { "type": "number", "minimum": 0 },
                    "period": { "type": ["string", "null"], "pattern": "^P\\d+[DWMY]$" },
                    "intro": { "type": ["string", "null"], "maxLength": 80 }
                  }
                }
              },
              "iap_range": { "type": ["string", "null"], "maxLength": 80 }
            }
          },
          "wedge": { "$ref": "#/definitions/prose", "description": "The one angle a challenger would win on, tied to a weakness above." },
          "verification": {
            "type": "object",
            "description": "Phase 5: a fresh agent re-ran the lookup and reproduced the score and the top weakness.",
            "required": ["result", "checked_at", "score_seen"],
            "additionalProperties": false,
            "properties": {
              "result": { "const": "reproduced" },
              "checked_at": { "$ref": "#/definitions/timestamp" },
              "score_seen": { "type": "integer", "minimum": 0, "maximum": 100 }
            }
          }
        }
      },
      {
        "type": "object",
        "description": "A candidate with a specific open question that the public data could not settle.",
        "required": ["verdict", "fingerprint", "app", "sources", "score", "blocker", "missing", "next_step"],
        "additionalProperties": false,
        "properties": {
          "verdict": { "const": "needs_validation" },
          "fingerprint": { "$ref": "#/definitions/fingerprint" },
          "app": { "$ref": "#/definitions/app" },
          "sources": { "$ref": "#/definitions/sources" },
          "score": { "$ref": "#/definitions/score" },
          "blocker": { "$ref": "#/definitions/prose" },
          "missing": { "type": "array", "minItems": 1, "maxItems": 10, "items": { "enum": ["iap_tiers", "reviews", "rating_split", "rating_velocity", "installs", "keyword_rank", "other_country", "score_not_reproduced"] } },
          "next_step": { "$ref": "#/definitions/prose" }
        }
      },
      {
        "type": "object",
        "description": "A candidate that Phase 3 or Phase 5 disproved. Kept so a later run does not hunt it again blind.",
        "required": ["verdict", "fingerprint", "app", "sources", "reason", "detail"],
        "additionalProperties": false,
        "properties": {
          "verdict": { "const": "rejected" },
          "fingerprint": { "$ref": "#/definitions/fingerprint" },
          "app": { "$ref": "#/definitions/app" },
          "sources": { "$ref": "#/definitions/sources" },
          "score": { "$ref": "#/definitions/score" },
          "reason": { "enum": ["demand_too_low", "incumbent_strong", "weakness_disproved", "recently_fixed", "duplicate", "out_of_scope"] },
          "detail": { "$ref": "#/definitions/prose" }
        }
      }
    ]
  },
  "definitions": {
    "prose": { "type": "string", "minLength": 1, "maxLength": 600, "visibleContent": true },
    "timestamp": { "type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}(:\\d{2}(\\.\\d+)?)?Z$" },
    "fingerprint": {
      "type": "string",
      "description": "store:country:app id. Stable across runs, so a later run can match a record.",
      "pattern": "^(apple:[a-z]{2}:\\d{5,12}|play:[a-z]{2}:[A-Za-z][A-Za-z0-9_]*(\\.[A-Za-z0-9_]+)+)$"
    },
    "app": {
      "type": "object",
      "required": ["store", "country", "id", "name", "developer"],
      "additionalProperties": false,
      "properties": {
        "store": { "enum": ["apple", "play"] },
        "country": { "type": "string", "pattern": "^[a-z]{2}$" },
        "id": { "type": "string", "minLength": 1, "maxLength": 160 },
        "name": { "type": "string", "minLength": 1, "maxLength": 200, "visibleContent": true },
        "developer": { "type": "string", "minLength": 1, "maxLength": 200 },
        "url": { "type": "string", "pattern": "^https://(apps\\.apple\\.com|play\\.google\\.com)/" }
      }
    },
    "sources": {
      "type": "array",
      "minItems": 1,
      "maxItems": 20,
      "uniqueItems": true,
      "items": {
        "type": "object",
        "required": ["call", "fetched_at"],
        "additionalProperties": false,
        "properties": {
          "call": { "enum": ["app_tracker_charts", "app_tracker_opportunities", "app_tracker_lookup", "app_tracker_keywords"] },
          "args": { "type": "string", "maxLength": 300 },
          "fetched_at": { "$ref": "#/definitions/timestamp" }
        }
      }
    },
    "score": {
      "type": "object",
      "description": "The Stale Winner score exactly as the engine returned it.",
      "required": ["score", "demand", "weakness", "flags"],
      "additionalProperties": false,
      "properties": {
        "score": { "type": "integer", "minimum": 0, "maximum": 100 },
        "demand": { "type": "number", "minimum": 0, "maximum": 1 },
        "weakness": { "type": "number", "minimum": 0, "maximum": 1 },
        "flags": { "type": "array", "uniqueItems": true, "items": { "enum": ["low_rating", "stale", "one_star_heavy"] } }
      }
    },
    "range": {
      "type": "array",
      "description": "[low, high], low <= high.",
      "minItems": 2,
      "maxItems": 2,
      "items": { "type": "number", "minimum": 0 }
    }
  }
}
