#!/usr/bin/env python3
"""
Daily reset script for FuelTrack
Moves today's data to history and starts fresh for a new day
Run this at the start of each day or when first requested
"""
import json
import os
from datetime import datetime, date

DATA_FILE = os.path.join(os.path.dirname(__file__), 'data.json')

def daily_reset():
    with open(DATA_FILE, 'r') as f:
        data = json.load(f)
    
    today_date = date.today().strftime('%Y-%m-%d')
    today_data = data.get('today', {})
    
    # Check if we already have today's data
    if today_data.get('date') == today_date:
        print(f"Today's data already exists ({today_date})")
        return
    
    # Move yesterday's "today" to history
    if today_data:
        # Calculate date - if data is from yesterday, add it
        data['meals'].append(today_data)
        print(f"Moved {today_data.get('date')} to history")
    
    # Reset for new day
    data['today'] = {
        "date": today_date,
        "meals": [],
        "exercise": [],
        "totals": {
            "consumed": 0,
            "burned": 0,
            "net": 0,
            "remaining": data['user']['daily_target']
        }
    }
    
    with open(DATA_FILE, 'w') as f:
        json.dump(data, f, indent=2)
    
    print(f"Reset for new day: {today_date}")

if __name__ == "__main__":
    daily_reset()