#!/usr/bin/env python3
"""
Ron Paul Curriculum Scraper
Fetches Xavier's course assignments and progress from RPC.
Saves to JSON for Tommy's morning brief.

Usage: python3 rpc-scraper.py
Output: ~/.openclaw/workspace/data/xavier-curriculum.json
"""

import json
import os
import subprocess
from datetime import datetime
from pathlib import Path

# Output location
OUTPUT_FILE = Path.home() / ".openclaw/workspace/data/xavier-curriculum.json"

def get_credentials():
    """Get RPC credentials from macOS Keychain."""
    try:
        password = subprocess.check_output(
            ['security', 'find-generic-password', '-s', 'RonPaulCurriculum', '-w'],
            stderr=subprocess.DEVNULL
        ).decode().strip()
        
        # Get account name
        result = subprocess.check_output(
            ['security', 'find-generic-password', '-s', 'RonPaulCurriculum', '-g'],
            stderr=subprocess.STDOUT
        ).decode()
        
        for line in result.split('\n'):
            if '"acct"' in line:
                username = line.split('"')[-2]
                break
        else:
            username = 'ohmandd'
        
        return username, password
    except Exception as e:
        print(f"Error getting credentials: {e}")
        return None, None

def scrape_rpc():
    """
    Scrape RPC using requests/BeautifulSoup.
    Note: RPC uses session cookies, so we need to maintain a session.
    """
    try:
        import requests
        from bs4 import BeautifulSoup
    except ImportError:
        print("Installing required packages...")
        subprocess.run(['pip3', 'install', 'requests', 'beautifulsoup4', '-q'])
        import requests
        from bs4 import BeautifulSoup
    
    username, password = get_credentials()
    if not username or not password:
        return {"error": "Could not get credentials"}
    
    session = requests.Session()
    
    # Login
    login_url = "https://www.ronpaulcurriculum.com/members/login.cfm"
    login_data = {
        'username': username,
        'password': password,
        'submit': 'Log In'
    }
    
    # Get login page first to get any CSRF tokens
    session.get(login_url)
    
    # Submit login
    response = session.post(login_url, data=login_data, allow_redirects=True)
    
    if "Welcome back" not in response.text and "Parent Dashboard" not in response.text:
        return {"error": "Login failed"}
    
    # Parse the dashboard page
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # Find Xavier's assignments table
    assignments = []
    table = soup.find('table')
    
    if table:
        rows = table.find_all('tr')[1:]  # Skip header row
        for row in rows:
            cells = row.find_all('td')
            if len(cells) >= 5:
                course_link = cells[0].find('a')
                course_name = course_link.text.strip() if course_link else cells[0].text.strip()
                course_url = course_link['href'] if course_link else None
                
                assigned = cells[1].text.strip()
                completed = cells[3].text.strip()
                expires = cells[4].text.strip()
                
                # Determine if in progress
                in_progress = '%' in completed
                is_complete = completed and not in_progress and completed != ''
                
                assignments.append({
                    'course': course_name,
                    'url': f"https://www.ronpaulcurriculum.com{course_url}" if course_url else None,
                    'assigned': assigned,
                    'progress': completed if in_progress else None,
                    'completed': not in_progress and bool(completed),
                    'expires': expires
                })
    
    # Logout
    session.get("https://www.ronpaulcurriculum.com/members/logout.cfm")
    
    # Build output
    result = {
        'student': 'Xavier Ohman',
        'scraped_at': datetime.now().isoformat(),
        'active_courses': [a for a in assignments if a['progress']],
        'completed_courses': [a for a in assignments if a['completed']],
        'all_assignments': assignments
    }
    
    return result

def main():
    print("Scraping Ron Paul Curriculum...")
    
    data = scrape_rpc()
    
    if 'error' in data:
        print(f"Error: {data['error']}")
        return 1
    
    # Ensure output directory exists
    OUTPUT_FILE.parent.mkdir(parents=True, exist_ok=True)
    
    # Save to JSON
    with open(OUTPUT_FILE, 'w') as f:
        json.dump(data, f, indent=2)
    
    print(f"Saved to {OUTPUT_FILE}")
    print(f"\nActive Courses:")
    for course in data['active_courses']:
        print(f"  - {course['course']}: {course['progress']}")
    
    return 0

if __name__ == '__main__':
    exit(main())

# TONY-APPROVED: 2026-03-01 | sha:0d901831
