#!/usr/bin/env bash
set -euo pipefail

export LC_ALL=C.UTF-8
export LANG=C.UTF-8

workdir="$(mktemp -d)"
html="$workdir/trending.html"
json="$workdir/report_items.json"
report="$workdir/report.md"
landing="$workdir/landing.html"
today="$(date -u +%F)"

curl -fsSL --retry 3 --retry-delay 2 'https://github.com/trending' -o "$html"

python3 - "$html" "$json" "$report" "$landing" "$today" <<'PY'
import json, re, sys
from pathlib import Path
from html.parser import HTMLParser

html_path, json_path, report_path, landing_path, today = sys.argv[1:6]
text = Path(html_path).read_text(encoding='utf-8', errors='replace')

class Parser(HTMLParser):
    def __init__(self):
        super().__init__()
        self.items=[]
        self.in_article=False
        self.cur=None
        self.capture=None
        self.buf=[]
    def handle_starttag(self, tag, attrs):
        d = dict(attrs)
        if tag=='article' and d.get('class','').startswith('Box-row'):
            self.in_article=True; self.cur={}
        if not self.in_article: return
        if tag=='h2':
            self.capture='h2'; self.buf=[]; self.cur['url']=''
        elif tag=='a' and self.capture=='h2':
            href = d.get('href','')
            if href:
                self.cur['url'] = 'https://github.com' + href
        elif tag=='p' and self.cur.get('description') is None and 'col-9' in d.get('class',''):
            self.capture='desc'; self.buf=[]
        elif tag=='span' and d.get('itemprop')=='programmingLanguage':
            self.capture='lang'; self.buf=[]
        elif tag=='a' and d.get('href','').endswith('/stargazers'):
            self.capture='stars'; self.buf=[]
    def handle_data(self, data):
        if self.capture: self.buf.append(data)
    def handle_endtag(self, tag):
        if not self.in_article: return
        if self.capture == 'h2':
            if tag == 'a':
                val = ' '.join(''.join(self.buf).split())
                self.cur['name'] = val.replace(' / ', '/').strip()
                self.capture = None; self.buf = []
            return
        if tag in {'p','span','a'} and self.capture:
            val = ' '.join(''.join(self.buf).split())
            if self.capture=='desc': self.cur['description']=val
            elif self.capture=='lang': self.cur['language']=val
            elif self.capture=='stars': 
                self.cur['stars']=val.replace(',','')
            self.capture=None; self.buf=[]
        elif tag=='article':
            if self.cur and self.cur.get('url') and 'sponsors/' not in self.cur['url']:
                self.items.append(self.cur)
            self.in_article=False; self.cur=None; self.capture=None; self.buf=[]

p=Parser(); p.feed(text)
items=[]
for x in p.items:
    if len(items)>=10: break
    items.append({
        'rank': len(items)+1,
        'name': x.get('name','').replace(' / ','/'),
        'url': x.get('url',''),
        'language': x.get('language') or 'Não informado',
        'stars': x.get('stars') or 'N/D',
        'problem': x.get('description') or 'Sem descrição.',
        'why_hot': 'Aparece na página de trending do GitHub.',
        'technologies': x.get('language','') or 'N/D',
    })

Path(json_path).write_text(json.dumps(items, ensure_ascii=False, indent=2), encoding='utf-8')

# Markdown report
lines = [f'# GitHub Trending Diário — {today}', '']
for it in items:
    lines += [
        f"{it['rank']}. [{it['name']}]({it['url']})",
        f"   - Linguagem: {it['language']}",
        f"   - Estrelas: {it['stars']}",
        f"   - Descrição: {it['problem']}",
        f"   - Por que está em alta: {it['why_hot']}",
        ''
    ]
Path(report_path).write_text('\n'.join(lines), encoding='utf-8')

# Landing page HTML
def esc(s):
    return str(s).replace('&','&amp;').replace('<','&lt;').replace('>','&gt;').replace('"','&quot;')

cards = []
for it in items:
    lang_cls = re.sub(r'[^a-z0-9]', '', it['language'].lower())[:12] or 'outro'
    cards.append(f'''
    <div class="card">
      <div class="rank">#{it['rank']}</div>
      <h3><a href="{it['url']}" target="_blank" rel="noopener">{esc(it['name'])}</a></h3>
      <p class="desc">{esc(it['problem'])}</p>
      <div class="meta">
        <span class="tag">{esc(it['language'])}</span>
        <span class="stars">⭐ {it['stars']}</span>
      </div>
    </div>''')

html_content = f'''<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GitHub Trending — {today}</title>
<style>
*{{margin:0;padding:0;box-sizing:border-box}}
body{{background:#0d1117;color:#c9d1d9;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif;padding:2rem 1rem}}
h1{{font-size:1.8rem;margin-bottom:.3rem;color:#58a6ff}}
.sub{{color:#8b949e;margin-bottom:2rem;font-size:.9rem}}
.grid{{display:grid;grid-template-columns:repeat(auto-fill,minmax(320px,1fr));gap:1rem}}
.card{{background:#161b22;border:1px solid #30363d;border-radius:8px;padding:1.2rem;position:relative;transition:border-color .2s}}
.card:hover{{border-color:#58a6ff}}
.rank{{position:absolute;top:.6rem;right:.8rem;font-size:.75rem;color:#484f58;font-weight:600}}
h3{{font-size:1.05rem;margin-bottom:.5rem;padding-right:1.8rem}}
h3 a{{color:#58a6ff;text-decoration:none}}
h3 a:hover{{text-decoration:underline}}
.desc{{font-size:.85rem;color:#8b949e;line-height:1.5;margin-bottom:.8rem}}
.meta{{display:flex;flex-wrap:wrap;gap:.4rem;align-items:center;font-size:.8rem}}
.tag{{background:#1f2937;color:#c9d1d9;padding:.15rem .5rem;border-radius:12px;font-size:.75rem}}
.stars{{color:#d29922;font-weight:500}}
.footer{{margin-top:2rem;padding-top:1rem;border-top:1px solid #21262d;color:#484f58;text-align:center;font-size:.8rem}}
.footer a{{color:#484f58}}
</style>
</head>
<body>
<h1>🔥 GitHub Trending — {today}</h1>
<p class="sub">Top 10 repositórios em alta</p>
<div class="grid">{"".join(cards)}</div>
<div class="footer">Gerado por Hermes Agent · {today} · <a href="https://github.com/trending">github.com/trending</a></div>
</body>
</html>'''

Path(landing_path).write_text(html_content, encoding='utf-8')
print(Path(report_path).read_text(encoding='utf-8'))
PY

# Copy landing page to serving dir
LANDING_DIR="/home/ice/.hermes/landing"
mkdir -p "$LANDING_DIR"
cp "$landing" "$LANDING_DIR/github-trending.html"

# Start HTTP server if not running
if ! pgrep -f "http\.server.*$LANDING_DIR" >/dev/null 2>&1; then
    cd "$LANDING_DIR"
    nohup python3 -m http.server 8080 --directory "$LANDING_DIR" >/dev/null 2>&1 &
    cd "$OLDPWD"
    sleep 0.5
fi

# Get IP for the landing page URL
IP=$(hostname -I 2>/dev/null | awk '{print $1}')
echo "---"
echo "🌐 Landing page: http://$IP:8080/github-trending.html"