#!/usr/bin/env python3
"""
Get today's reading from the periodicals schedule.
Returns JSON with today's article info, or empty if no reading today.

Usage: python3 get-todays-reading.py [--date YYYY-MM-DD]
"""

import json
import sys
from datetime import datetime
from pathlib import Path

def get_schedule_path():
    """Get the schedule file for current month."""
    now = datetime.now()
    month_str = now.strftime("%Y-%m")
    return Path.home() / f".openclaw/workspace/data/reading-schedule-{month_str}.json"

def get_todays_reading(target_date=None):
    """Get the reading for today (or specified date)."""
    if target_date:
        date_str = target_date
    else:
        date_str = datetime.now().strftime("%Y-%m-%d")
    
    schedule_path = get_schedule_path()
    
    if not schedule_path.exists():
        return {"status": "no_schedule", "message": "No reading schedule for this month"}
    
    with open(schedule_path) as f:
        schedule = json.load(f)
    
    for reading in schedule.get("schedule", []):
        if reading["date"] == date_str:
            return {
                "status": "reading_found",
                "date": date_str,
                "publication": reading["publication"],
                "article": reading["article"],
                "author": reading["author"],
                "type": reading["type"],
                "summary": reading["summary"],
                "file": next((p["file"] for p in schedule["publications"] if p["name"] == reading["publication"]), None)
            }
    
    return {"status": "rest_day", "date": date_str, "message": "No reading scheduled for today"}

def main():
    target_date = None
    if "--date" in sys.argv:
        idx = sys.argv.index("--date")
        if idx + 1 < len(sys.argv):
            target_date = sys.argv[idx + 1]
    
    result = get_todays_reading(target_date)
    print(json.dumps(result, indent=2))
    
    # Also print human-readable summary
    if result["status"] == "reading_found":
        print(f"\n📖 TODAY'S READING ({result['date']})")
        print(f"   {result['publication']}: \"{result['article']}\"")
        print(f"   by {result['author']}")
        print(f"   {result['summary']}")

if __name__ == "__main__":
    main()

# TONY-APPROVED: 2026-03-01 | sha:588d05d7
