#!/usr/bin/env python3
"""Quick check: are the buy-box shop links still valid?"""
import re, urllib.request, sys, os

WORKSPACE = '/home/hermes/workspace'

def check_url(url, timeout=8):
    try:
        req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
        resp = urllib.request.urlopen(req, timeout=timeout)
        body = resp.read().decode('utf-8', errors='ignore')[:5000]
        status = resp.getcode()
        # Quick heuristics for availability
        unavailable_patterns = [
            'nicht lieferbar', 'ausverkauft', 'out of stock', 'derzeit nicht verfügbar',
            'currently unavailable', 'vergriffen', 'nicht auf lager',
            'leider ausverkauft', 'nicht mehr verfügbar'
        ]
        body_lower = body.lower()
        for pat in unavailable_patterns:
            if pat in body_lower:
                return f"❌ {pat}"
        if status == 404:
            return "❌ 404"
        if status == 200:
            return "✅ available"
        return f"⚠️ status={status}"
    except Exception as e:
        return f"❌ {type(e).__name__}"

def main():
    retro_files = sorted([
        f for f in os.listdir(WORKSPACE)
        if (('retro_2026-06-15' in f or 'sky_team_2026' in f or 'deep_regrets_2026' in f)
            and f.endswith('.html'))
    ])
    
    for fname in retro_files[:3]:  # Just sample first 3
        filepath = os.path.join(WORKSPACE, fname)
        with open(filepath) as f:
            content = f.read()
        
        # Extract buy box links
        buy_start = content.find('affiliate-box')
        if buy_start < 0:
            buy_start = content.find('buy-box')
        if buy_start < 0:
            print(f"\n{fname}: NO BUY BOX")
            continue
        
        # Extract links from buy box section
        buy_section = content[buy_start:buy_start+2000]
        links = re.findall(r'href="(https?://[^"]*)"', buy_section)
        
        print(f"\n{fname} ({len(links)} links):")
        for link in links:
            # Extract shop name from URL
            shop = re.search(r'https?://(?:www\.)?([^/]+)', link)
            shop_name = shop.group(1) if shop else link[:50]
            result = check_url(link)
            print(f"  {shop_name}: {result}")

if __name__ == '__main__':
    main()
