#!/usr/bin/env python3
"""Check deployment status by UUID."""
import json, sys, urllib.request

BASE = "http://localhost:8000/api/v1"
TOKEN = "3|gTHVGM97WUNZmrjUYjAS7xiLrKZrkNWmNwBRbv9f6e36272c"

def api(method, path):
    req = urllib.request.Request(BASE + path, method=method)
    req.add_header("Authorization", f"Bearer {TOKEN}")
    req.add_header("Accept", "application/json")
    try:
        with urllib.request.urlopen(req, timeout=60) as resp:
            return resp.status, json.loads(resp.read().decode())
    except urllib.error.HTTPError as e:
        return e.code, json.loads(e.read().decode())

deploy_uuid = sys.argv[1]
s, r = api("GET", f"/deployments/{deploy_uuid}")
print(f"STATUS: {r.get('status')}")
print(f"commit: {r.get('commit')}")
logs = r.get('logs', '')
try:
    log_list = json.loads(logs)
    for entry in log_list[-10:]:
        out = entry.get('output','')
        if 'REDACTED' not in out and 'x-access-token' not in out:
            print(f"[{entry.get('type')}] {out[:200]}")
except Exception as e:
    print(f"parse err: {e}")
    print(str(logs)[-800:] if logs else "no logs")
