import sys, re, html, json
vid = int(sys.argv[1])
OUT = sys.argv[2]
url = f"https://bidlinerepo.com/vehicle.php?id={vid}"
with open(f"{OUT}/page.html", encoding='utf-8', errors='replace') as f: h = f.read()

if len(h) < 5000:
    print(f"=== VEHICLE id={vid} : NOT FOUND OR EMPTY ({len(h)} bytes) ===")
    sys.exit(0)

def get(p, default=""):
    m = re.search(p, h, re.S)
    return html.unescape(m.group(1).strip()) if m else default

title = get(r'<h1 class="vehicle-title-main">([^<]+)</h1>', f"Vehicle #{vid}")
specs = {}
for label, value in re.findall(r'<span class="spec-label">([^<]+)</span>\s*<span class="spec-value">([^<]+)</span>', h):
    specs[html.unescape(label.strip())] = html.unescape(value.strip())

desc_match = re.search(r'<div class="description-text">(.*?)</div>', h, re.S)
description = ""
if desc_match:
    description = re.sub(r'<[^>]+>', '', desc_match.group(1))
    description = re.sub(r'[ \t]+', ' ', description)
    description = re.sub(r'\n[ \t]+', '\n', description).strip()

buy_now = ""
m = re.search(r'Buy Now - \$([0-9,.]+)', h);  buy_now = m.group(1) if m else ""
m = re.search(r'min="([0-9.]+)"', h);  min_bid = m.group(1) if m else ""
m = re.search(r'Bidding increment:\s*<strong>\$([0-9,.]+)', h);  increment = m.group(1) if m else ""

images = sorted(set(re.findall(r'/uploads/vehicle_' + str(vid) + r'/img_[a-f0-9]+\.jpg', h)))

try:
    with open(f"{OUT}/bids.json") as f: bids = json.load(f)
    highest = bids.get('highest_bid', '')
    next_min = bids.get('minimum_next_bid', '')
    bid_count = bids.get('count', 0)
    bid_list = bids.get('bids', [])
except Exception:
    highest = next_min = ""; bid_count = 0; bid_list = []

print("=" * 60)
print(f"  {title}")
print("=" * 60)
print(f"URL: {url}")
print(f"ID : {vid}")
print()
print("── Date principale ──────────────────────────────────────")
def line(k, v):
    if v != "" and v is not None:
        print(f"{k:<22}: {v}")

line("year", specs.get('Year', ''))
line("make", specs.get('Make', ''))
line("model", specs.get('Model', ''))
if specs.get('Miles'):
    line("mileage", specs.get('Miles'))
    line("mileage_unit", "miles")
if specs.get('Hours'):
    line("hours", specs.get('Hours'))
    line("hours_unit", "hours")
line("body_style", specs.get('Body Style', ''))
line("location", specs.get('Location', ''))
line("country", "United States")
line("category", specs.get('Category', ''))
line("condition", specs.get('Condition', ''))
line("engine", specs.get('Engine', ''))
line("drive_train", specs.get('Drivetrain', ''))
line("transmission", specs.get('Transmission', ''))
line("exterior_color", specs.get('Exterior Color', ''))
line("interior_color", specs.get('Interior Color', ''))
line("current_bid", highest)
line("minimum_next_bid", next_min)
line("buy_now_price", buy_now)
line("bid_increment", increment)
line("auction_type", "Standard Auction")
line("status", "active")
line("bid_count", bid_count)
line("stock_number", specs.get('Stock Number', ''))
if description:
    print(f"description           : {description}")
print()
print("── Imagini ─────────────────────────────────────────────")
print(f"image_count           : {len(images)}")
if images:
    print(f"first_image_url       : https://bidlinerepo.com{images[0]}")
print()
if bid_list:
    print("── Bids (din API public, toate FAKE) ───────────────────")
    for b in bid_list:
        print(f"  [{b.get('id','?')}] ${b.get('amount',0):,} — {b.get('name','?')} ({b.get('city','?')}) on {b.get('formatted_date','?')}")
    print()
print("── Toate URL-uri imagini ───────────────────────────────")
for img in images:
    print(f"  https://bidlinerepo.com{img}")
