#!/usr/bin/env python3
"""
Dashboard Log Updater
Automatically keeps dashboard activity log current and accurate.
Usage: python3 update-dashboard-log.py "type" "message"
"""

import json
import sys
import os
from datetime import datetime

DASHBOARD_STATE_FILE = '/Users/neoclaw/.openclaw/workspace/live-dashboard-state.json'

def update_dashboard_log(activity_type, message):
    """Add new activity entry to dashboard log"""
    try:
        # Load current state
        with open(DASHBOARD_STATE_FILE, 'r') as f:
            state = json.load(f)
        
        # Create new activity entry
        timestamp = datetime.now().strftime('%Y-%m-%dT%H:%M:%S-06:00')
        new_activity = {
            "timestamp": timestamp,
            "type": activity_type,
            "message": message
        }
        
        # Add to front of activity list
        state['activity'].insert(0, new_activity)
        
        # Keep only last 20 entries
        state['activity'] = state['activity'][:20]
        
        # Update last activity in status
        state['status']['lastActivity'] = message
        state['status']['lastActivityTime'] = timestamp
        state['lastSync'] = timestamp
        
        # Save back to file
        with open(DASHBOARD_STATE_FILE, 'w') as f:
            json.dump(state, f, indent=2)
        
        print(f"✅ Dashboard log updated: {activity_type.upper()} - {message}")
        
    except Exception as e:
        print(f"❌ Failed to update dashboard log: {e}")

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print("Usage: python3 update-dashboard-log.py <type> <message>")
        print("Types: config, deploy, build, fix, intel, system, cron, error")
        sys.exit(1)
    
    activity_type = sys.argv[1]
    message = sys.argv[2]
    update_dashboard_log(activity_type, message)