"""Availability-filtered GDELT structured events; no full article text is invented."""
from functools import lru_cache
from datetime import datetime,timedelta
import json
from pathlib import Path

def instant(v):
    d=datetime.fromisoformat(v.replace('Z','+00:00'))
    if d.tzinfo is None:raise ValueError('timezone required')
    return d
@lru_cache(maxsize=4)
def _load(path,mtime):
    rows=[]
    with open(path) as f:
        for line in f:
            row=json.loads(line);rows.append((instant(row['available_at']),row))
    return rows

def available_news(path,at):
    path=Path(path)
    if not path.is_file():return {'status':'NOT_INSTALLED','events':[],'available_count':0}
    cutoff=instant(at);rows=[r for t,r in _load(str(path),path.stat().st_mtime_ns) if cutoff-timedelta(days=1)<=t<=cutoff]
    return {'status':'AVAILABLE' if rows else 'NO_RECORDS_IN_WINDOW','window_hours':24,'available_count':len(rows),'events':rows,'scope':'Structured events only. Dataset coverage is partial; absence of a record does not imply absence of geopolitical activity.'}
