#!/usr/bin/env python3
"""
Script to help populate the 2026 calendar with events
"""

import pandas as pd
from datetime import datetime
import os

def add_events_to_calendar():
    """
    Add events to the calendar - you can modify this function to add your events
    """
    
    # Dictionary to store events by date
    events_data = {
        # January 2026 events (from your image)
        '2026-01-01': 'NEW YEARS DAY SCRAMBLE',
        '2026-01-02': 'EPIC LADIES - Q MEN',
        '2026-01-03': 'MENS 60 TEES LADIES 52\'S',
        '2026-01-04': 'SCRAMBLE',
        '2026-01-05': 'EPIC LADIES (a.m), 321 (Draw), MEN- Q (p.m)',
        '2026-01-07': 'MATCHPLAY PRESIDENT VS CAPTAINS',
        '2026-01-09': 'EPIC LADIES - Q MEN',
        '2026-01-10': 'MENS 60 TEES LADIES 52\'S',
        '2026-01-11': 'SCRAMBLE',
        '2026-01-12': 'COURSE CLOSED',
        '2026-01-13': 'COURSE CLOSED',
        '2026-01-14': 'COURSE CLOSED',
        '2026-01-16': 'EPIC LADIES MEN',
        '2026-01-17': 'MENS 60 TEES LADIES 52\'S',
        '2026-01-18': 'SCRAMBLE',
        '2026-01-19': 'EPIC LADIES (p.m), Yellow ball MEN (a.m)',
        '2026-01-21': 'MEDAL STABLEFORD 9-HOLE QUALIFIERS',
        '2026-01-23': 'EPIC LADIES MEN',
        '2026-01-24': 'MENS 60 TEES LADIES 52\'S',
        '2026-01-25': 'SCRAMBLE',
        '2026-01-26': 'EPIC - Q LADIES (a.m)- Q MEN (p.m)',
        '2026-01-28': 'TEAM AM/AM (DRAW)',
        '2026-01-30': 'EPIC LADIES MEN',
        '2026-01-31': 'MENS 60 TEES LADIES 52\'S',
        
        # Add more months here as you extract them from your Word document
        # Example for February (you'll need to add the actual events):
        # '2026-02-01': 'EVENT NAME',
        # '2026-02-02': 'ANOTHER EVENT',
        
        # Example for March:
        # '2026-03-01': 'EVENT NAME',
        # '2026-03-02': 'ANOTHER EVENT',
    }
    
    # Event types for categorization
    event_types = {
        '2026-01-01': 'Tournament',
        '2026-01-02': 'Regular Event',
        '2026-01-03': 'Regular Event',
        '2026-01-04': 'Regular Event',
        '2026-01-05': 'Regular Event',
        '2026-01-07': 'Match Play',
        '2026-01-09': 'Regular Event',
        '2026-01-10': 'Regular Event',
        '2026-01-11': 'Regular Event',
        '2026-01-12': 'Course Maintenance',
        '2026-01-13': 'Course Maintenance',
        '2026-01-14': 'Course Maintenance',
        '2026-01-16': 'Regular Event',
        '2026-01-17': 'Regular Event',
        '2026-01-18': 'Regular Event',
        '2026-01-19': 'Regular Event',
        '2026-01-21': 'Qualifier',
        '2026-01-23': 'Regular Event',
        '2026-01-24': 'Regular Event',
        '2026-01-25': 'Regular Event',
        '2026-01-26': 'Regular Event',
        '2026-01-28': 'Team Event',
        '2026-01-30': 'Regular Event',
        '2026-01-31': 'Regular Event',
    }
    
    return events_data, event_types

def update_calendar_with_events():
    """
    Update the calendar Excel file with events
    """
    
    # Get events data
    events_data, event_types = add_events_to_calendar()
    
    # Read the existing calendar
    calendar_file = '/var/www/html/wordpress6/wordpress/EP/2026_complete_calendar.xlsx'
    
    try:
        # Read the main calendar sheet
        df = pd.read_excel(calendar_file, sheet_name='Full Calendar')
        
        # Update with events
        for date, event_name in events_data.items():
            mask = df['Date'] == date
            df.loc[mask, 'Event Name'] = event_name
            if date in event_types:
                df.loc[mask, 'Event Type'] = event_types[date]
        
        # Save updated calendar
        output_file = '/var/www/html/wordpress6/wordpress/EP/2026_calendar_with_events.xlsx'
        
        with pd.ExcelWriter(output_file, engine='openpyxl') as writer:
            # Main calendar sheet
            df.to_excel(writer, sheet_name='Full Calendar', index=False)
            
            # Create individual month sheets
            months = ['January', 'February', 'March', 'April', 'May', 'June',
                     'July', 'August', 'September', 'October', 'November', 'December']
            
            for month in months:
                month_data = df[df['Month'] == month].copy()
                month_data.to_excel(writer, sheet_name=month, index=False)
            
            # Create summary sheet
            summary_data = []
            for month in months:
                month_events = df[df['Month'] == month]
                events_count = len(month_events[month_events['Event Name'] != ''])
                summary_data.append({
                    'Month': month,
                    'Total Days': len(month_events),
                    'Events Scheduled': events_count,
                    'Days Available': len(month_events) - events_count
                })
            
            summary_df = pd.DataFrame(summary_data)
            summary_df.to_excel(writer, sheet_name='Summary', index=False)
        
        print(f"Updated calendar created: {output_file}")
        print(f"Events added: {len(events_data)}")
        
        return output_file
        
    except Exception as e:
        print(f"Error reading calendar file: {e}")
        return None

def create_event_entry_template():
    """
    Create a template for easy event entry
    """
    
    template = """
# Event Entry Template
# Copy this template and fill in your events for each month

events_data = {
    # January 2026 (already filled)
    '2026-01-01': 'NEW YEARS DAY SCRAMBLE',
    '2026-01-02': 'EPIC LADIES - Q MEN',
    # ... (rest of January events)
    
    # February 2026 - ADD YOUR EVENTS HERE
    # '2026-02-01': 'EVENT NAME',
    # '2026-02-02': 'ANOTHER EVENT',
    
    # March 2026 - ADD YOUR EVENTS HERE
    # '2026-03-01': 'EVENT NAME',
    # '2026-03-02': 'ANOTHER EVENT',
    
    # Continue for all months...
}

event_types = {
    # January 2026 (already filled)
    '2026-01-01': 'Tournament',
    '2026-01-02': 'Regular Event',
    # ... (rest of January events)
    
    # February 2026 - ADD YOUR EVENT TYPES HERE
    # '2026-02-01': 'Tournament',
    # '2026-02-02': 'Regular Event',
    
    # Continue for all months...
}
"""
    
    with open('/var/www/html/wordpress6/wordpress/EP/event_entry_template.txt', 'w') as f:
        f.write(template)
    
    print("Event entry template created: event_entry_template.txt")

if __name__ == "__main__":
    print("Updating calendar with events...")
    
    # Update the calendar with events
    updated_file = update_calendar_with_events()
    
    if updated_file:
        print(f"\n✅ SUCCESS!")
        print(f"Updated calendar: {updated_file}")
        print("\nThe calendar now contains:")
        print("- All 365 days of 2026")
        print("- January events populated")
        print("- Empty slots for other months")
        print("\nTo add more months:")
        print("1. Open your Word document")
        print("2. Copy events for each month")
        print("3. Add them to the Excel file")
    else:
        print("❌ Error updating calendar")
    
    # Create template for easy event entry
    create_event_entry_template()


