#!/usr/bin/env python3
"""Replicate the app's exact manifest request (with expo-update-id header)
to see what the server returns vs. a bare probe."""
import urllib.request, json, sys

BASE = "https://ota.46-4-121-190.sslip.io"

def fetch(headers, label):
    req = urllib.request.Request(f"{BASE}/api/manifest", headers=headers)
    try:
        with urllib.request.urlopen(req, timeout=30) as r:
            body = r.read().decode('utf-8', 'replace')
            # extract first JSON object
            start = body.find('{')
            depth = 0
            end = len(body)
            for i in range(start, len(body)):
                if body[i] == '{': depth += 1
                elif body[i] == '}':
                    depth -= 1
                    if depth == 0:
                        end = i + 1
                        break
            j = json.loads(body[start:end])
            print(f"[{label}] HTTP {r.status}")
            print(f"  id: {j.get('id')}")
            print(f"  runtimeVersion: {j.get('runtimeVersion')}")
            print(f"  isVerified: {j.get('isVerified')}")
            print(f"  launchAsset url: {(j.get('launchAsset') or {}).get('url','')[:120]}")
            return j
    except Exception as e:
        print(f"[{label}] ERROR: {type(e).__name__}: {e}")
        return None

# 1. Bare probe (no update-id) — what I've been doing
fetch({
    "expo-project": "pixiesprout",
    "expo-channel-name": "default",
    "Expo-Platform": "android",
    "Expo-Runtime-Version": "1.0.116",
    "Expo-Channel": "default",
}, "bare probe")

# 2. App-like probe — with the CURRENT running update-id (df5e's updateId)
#    This is what expo-updates sends: it tells the server what it's running.
fetch({
    "expo-project": "pixiesprout",
    "expo-channel-name": "default",
    "Expo-Platform": "android",
    "Expo-Runtime-Version": "1.0.116",
    "Expo-Channel": "default",
    "expo-update-id": "05128ad9-95f7-0c42-fe85-ebbefb44fe98",  # df5e (what app runs)
    "expo-protocol-version": "0",
}, "app-like (running df5e)")

# 3. App-like probe with df60's updateId (what if app thinks it's on df60?)
fetch({
    "expo-project": "pixiesprout",
    "expo-channel-name": "default",
    "Expo-Platform": "android",
    "Expo-Runtime-Version": "1.0.116",
    "Expo-Channel": "default",
    "expo-update-id": "51bd6af0-00fe-3cbd-5c2d-348c4c2579d4",  # df60
    "expo-protocol-version": "0",
}, "app-like (running df60)")
