#!/usr/bin/env python3
"""Get full deployment logs."""
import json, sys, urllib.request

BASE = "http://localhost:8000/api/v1"
TOKEN = "2|IqbpR94lOiexx5gXHV3BO31utUhuqwi9Y5T2lLCe1505d5d4"

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}")
logs = r.get('logs', '')
log_list = json.loads(logs)
# Find error lines
for entry in log_list:
    out = entry.get('output','')
    low = out.lower()
    if any(k in low for k in ['error', 'failed', 'fatal', 'not found', 'cannot', 'could not', 'npx', 'next build', 'command']):
        if 'REDACTED' not in out:
            print(f"[{entry.get('type')}] {out[:300]}")
