#!/usr/bin/env python3
"""
Properly extract events from each month image individually
Just like we did for January
"""

import pandas as pd
from datetime import datetime
import os

def extract_all_months_properly():
    """
    Extract events from each month image properly
    Based on actual calendar structure
    """
    
    # Complete events for all months - extracted properly from images
    all_events = {
        # January 2026 (already verified correct)
        '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',
        
        # February 2026 - Extract from february-2026.png
        # (I need to analyze the actual image to get the correct events)
        
        # March 2026 - Extract from march-2026.png
        # (I need to analyze the actual image to get the correct events)
        
        # April 2026 - Extract from april-2026.png
        # (I need to analyze the actual image to get the correct events)
        
        # May 2026 - Extract from may-2026.png
        # (I need to analyze the actual image to get the correct events)
        
        # June 2026 - Extract from june-2026.png
        # (I need to analyze the actual image to get the correct events)
        
        # July 2026 - CORRECTED based on your feedback
        '2026-07-17': 'SCRAMBLE',
        '2026-07-19': 'COURSE CLOSED',
        '2026-07-20': 'COURSE CLOSED',
        '2026-07-21': 'COURSE CLOSED',
        
        # August 2026 - Extract from august-2026.png
        # (I need to analyze the actual image to get the correct events)
        
        # September 2026 - Extract from september-2026.png
        # (I need to analyze the actual image to get the correct events)
        
        # October 2026 - Extract from October-2026.png
        # (I need to analyze the actual image to get the correct events)
        
        # November 2026 - Extract from november-2026.png
        # (I need to analyze the actual image to get the correct events)
        
        # December 2026 - Extract from december-2026.png
        # (I need to analyze the actual image to get the correct events)
    }
    
    return all_events

def categorize_events():
    """
    Categorize events by type
    """
    event_types = {}
    
    all_events = extract_all_months_properly()
    
    for date, event_name in all_events.items():
        if 'SCRAMBLE' in event_name:
            event_types[date] = 'Regular Event'
        elif 'EPIC' in event_name:
            event_types[date] = 'Regular Event'
        elif 'MENS' in event_name and 'LADIES' in event_name:
            event_types[date] = 'Regular Event'
        elif 'MATCHPLAY' in event_name:
            event_types[date] = 'Match Play'
        elif 'COURSE CLOSED' in event_name:
            event_types[date] = 'Course Maintenance'
        elif 'QUALIFIERS' in event_name:
            event_types[date] = 'Qualifier'
        elif 'TEAM' in event_name:
            event_types[date] = 'Team Event'
        elif 'CHRISTMAS' in event_name or 'NEW YEARS' in event_name:
            event_types[date] = 'Special Event'
        else:
            event_types[date] = 'Regular Event'
    
    return event_types

def create_complete_calendar():
    """
    Create the complete 2026 calendar with all properly extracted events
    """
    
    # Get events and types
    all_events = extract_all_months_properly()
    event_types = categorize_events()
    
    # Create calendar structure
    calendar_data = []
    
    # Define months and their days
    months = {
        'January': 31,
        'February': 29,  # 2026 is a leap year
        'March': 31,
        'April': 30,
        'May': 31,
        'June': 30,
        'July': 31,
        'August': 31,
        'September': 30,
        'October': 31,
        'November': 30,
        'December': 31
    }
    
    # Create entries for each day of 2026
    for month_name, days_in_month in months.items():
        month_num = list(months.keys()).index(month_name) + 1
        for day in range(1, days_in_month + 1):
            try:
                date_str = f"2026-{month_num:02d}-{day:02d}"
                date_obj = datetime.strptime(date_str, '%Y-%m-%d')
                day_of_week = date_obj.strftime('%A')
                
                calendar_data.append({
                    'Date': date_str,
                    'Month': month_name,
                    'Day': day,
                    'Day of Week': day_of_week,
                    'Event Name': '',
                    'Event Type': '',
                    'Notes': ''
                })
            except ValueError:
                continue
    
    # Create DataFrame
    df = pd.DataFrame(calendar_data)
    
    # Update with events
    for date, event_name in all_events.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 to Excel with multiple sheets
    output_file = '/var/www/html/wordpress6/wordpress/EP/2026_PROPERLY_EXTRACTED_CALENDAR.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,
                'Event Percentage': f"{(events_count/len(month_events)*100):.1f}%"
            })
        
        summary_df = pd.DataFrame(summary_data)
        summary_df.to_excel(writer, sheet_name='Summary', index=False)
    
    print(f"Calendar created: {output_file}")
    print(f"Total days: {len(calendar_data)}")
    print(f"Events populated: {len(all_events)}")
    
    return output_file

if __name__ == "__main__":
    print("Creating properly extracted 2026 calendar...")
    print("This version includes:")
    print("✅ January 2026: 24 events (verified correct)")
    print("✅ July 2026: 4 events (corrected)")
    print("⏳ Other months: Need to be extracted from images")
    
    # Create calendar
    excel_file = create_complete_calendar()
    
    print(f"\n✅ Calendar created: {excel_file}")
    print("\nNext steps:")
    print("1. I need to analyze each month image individually")
    print("2. Extract the actual events from each image")
    print("3. Update the calendar with the correct data")
    
    print("\nWhich month should I start with?")
    print("Available month images:")
    print("- february-2026.png")
    print("- march-2026.png") 
    print("- april-2026.png")
    print("- may-2026.png")
    print("- june-2026.png")
    print("- july-2026.png (already corrected)")
    print("- august-2026.png")
    print("- september-2026.png")
    print("- October-2026.png")
    print("- november-2026.png")
    print("- december-2026.png")


