#!/usr/bin/env python3
"""Re-apply date filter and engagement thresholds to scan results.
Fixes the silent date filter bug where all tweets with parsable dates
passed the 'recent' check regardless of actual date."""

import json
from datetime import datetime, timezone

CUTOFF = datetime(2026, 6, 13, 0, 0, 0, tzinfo=timezone.utc)

def parse_date(created_at):
    if not created_at:
        return None
    try:
        return datetime.strptime(created_at, "%a %b %d %H:%M:%S %z %Y")
    except:
        return None

for chunk in ["CHUNK1", "CHUNK2"]:
    path = f"/home/kiro/trend-engine/trends/x/account-monitor/scan_results_2026-06-16_{chunk}.json"
    with open(path) as f:
        data = json.load(f)
    
    total_before = 0
    total_after = 0
    
    for acc in data["accounts"]:
        old_tweets = acc.get("tweets", [])
        total_before += len(old_tweets)
        
        # Re-filter: date cutoff + engagement thresholds
        filtered = []
        for t in old_tweets:
            created_at = t.get("createdAt", "")
            dt = parse_date(created_at)
            if dt and dt >= CUTOFF:
                # Check engagement thresholds
                if t.get("likes", 0) >= 50 or t.get("retweets", 0) >= 10 or t.get("views", 0) >= 1000:
                    filtered.append(t)
        
        # Re-sort by score
        filtered.sort(key=lambda x: x.get("score", 0), reverse=True)
        total_after += len(filtered)
        
        old_q = acc.get("qualifying_count", 0)
        acc["tweets"] = filtered
        acc["qualifying_count"] = len(filtered)
        acc["recent_count"] = len(filtered)  # after re-filter, recent = qualifying
        
        if old_q != len(filtered):
            print(f"  @{acc['handle']}: {old_q} → {len(filtered)} (removed {old_q - len(filtered)})")
    
    print(f"  {chunk} TOTAL: {total_before} → {total_after} (removed {total_before - total_after})")
    
    with open(path, "w") as f:
        json.dump(data, f, indent=2)
    
    print(f"  Saved {path}\n")
