#!/usr/bin/env python3
"""
Script to verify the schedule fix
"""

import os
import re

def check_file_exists(filepath):
    """Check if file exists"""
    return os.path.exists(filepath)

def check_schedule_form_placement(filepath):
    """Check if schedule form is outside the conditional block"""
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # Check if schedule form is after the endif tag
    endif_pos = content.find('{% endif %}')
    form_pos = content.find('<div id="schedule-form"')
    
    if endif_pos == -1 or form_pos == -1:
        return False
    
    return form_pos > endif_pos

def check_javascript_functions(filepath):
    """Check if JavaScript functions have proper null checks"""
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # Check for null checks in addNewSchedule function
    add_new_schedule_match = re.search(r'function addNewSchedule\(\).*?form = document.getElementById\(\'schedule-form\'\);.*?if \(!form\)', content, re.DOTALL)
    
    # Check for beach ID handling in loadBeachSchedules function
    load_schedules_match = re.search(r'function loadBeachSchedules\(\).*?const beachId = \'{{ beach\.beach_place_id if beach else "" }}\';', content, re.DOTALL)
    
    return add_new_schedule_match is not None and load_schedules_match is not None

def main():
    """Main verification function"""
    print("Verifying schedule fix...")
    
    # Check if edit_beach.html exists
    edit_beach_path = 'backend/templates/admin/edit_beach.html'
    if not check_file_exists(edit_beach_path):
        print(f"❌ {edit_beach_path} not found")
        return False
    
    # Check if schedule form is properly placed
    if not check_schedule_form_placement(edit_beach_path):
        print("❌ Schedule form is not properly placed outside conditional block")
        return False
    
    # Check if JavaScript functions have proper null checks
    if not check_javascript_functions(edit_beach_path):
        print("❌ JavaScript functions don't have proper null checks")
        return False
    
    print("✅ All checks passed! Schedule fix is properly implemented.")
    return True

if __name__ == "__main__":
    main()