#!/usr/bin/env python3
"""Get full deployment logs - focus on the actual error."""
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())

d = sys.argv[1]
s, r = api("GET", f"/deployments/{d}")
logs = r.get('logs', '')
log_list = json.loads(logs)
# Print everything that looks like an error or docker build
for entry in log_list:
    out = entry.get('output','')
    if 'REDACTED' in out or 'x-access-token' in out:
        continue
    low = out.lower()
    if any(k in low for k in ['error', 'failed', 'fatal', 'not found', 'cannot', 'could not', 'denied', 'build', 'dockerfile', 'npm', 'step', 'step 1', '=>', 'exit code']):
        print(f"[{entry.get('type')}] {out[:250]}")
