"""Date verification for scan results"""
import json, os
from datetime import datetime, timezone, timedelta

DATE_STR = datetime.now(timezone.utc).strftime("%Y-%m-%d")
OUT_DIR = "/home/kiro/trend-engine/trends/x/account-monitor"
CUTOFF = datetime.now(timezone.utc) - timedelta(days=3)

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

all_ok = True
total_qual = 0
for chunk_num in [1, 2]:
    path = os.path.join(OUT_DIR, f"scan_results_{DATE_STR}_CHUNK{chunk_num}.json")
    with open(path) as f:
        data = json.load(f)
    for acc in data["accounts"]:
        total_qual += acc.get("qualifying_count", 0)
        for t in acc.get("tweets", []):
            dt_str = t.get("date", "")
            raw = t.get("createdAt", "")
            if dt_str:
                try:
                    d = datetime.strptime(dt_str, "%Y-%m-%d").replace(tzinfo=timezone.utc)
                    if d < CUTOFF:
                        print(f"OUTSIDE: {acc['handle']} date={dt_str}")
                        all_ok = False
                except:
                    print(f"UNPARSEABLE: {acc['handle']} date={dt_str}")
                    all_ok = False
if all_ok:
    print(f"All dates OK. Total qualifying: {total_qual}")
else:
    print("DATE FILTER ISSUE DETECTED")
