import requests
import time

# Test the beach edit page to see if our JavaScript fix works
beach_id = "9264fc4e-14f4-475d-ac96-4cb689a63783"
url = f"http://localhost:8080/admin/beaches/edit/{beach_id}"
schedules_url = f"http://localhost:8080/api/beach-schedules/{beach_id}"

print(f"Testing access to beach edit page: {url}")

try:
    # Test accessing the beach edit page
    response = requests.get(url)
    print(f"Beach edit page status code: {response.status_code}")
    
    if response.status_code == 200:
        print("✅ Beach edit page loaded successfully")
        # Check if the page contains the schedule form
        if 'schedule-form' in response.text:
            print("✅ Schedule form found in the page")
        else:
            print("❌ Schedule form not found in the page")
    else:
        print(f"❌ Failed to load beach edit page: {response.status_code}")
        
except Exception as e:
    print(f"❌ Error accessing beach edit page: {e}")

# Test the beach schedules API endpoints
print("\n--- Testing Beach Schedules API ---")

# Test getting schedules (should return empty array if no schedules)
try:
    response = requests.get(schedules_url)
    print(f"Get schedules status code: {response.status_code}")
    
    if response.status_code == 200:
        schedules = response.json()
        print(f"✅ Successfully retrieved schedules: {len(schedules)} schedules found")
    else:
        print(f"❌ Failed to retrieve schedules: {response.status_code}")
        
except Exception as e:
    print(f"❌ Error getting schedules: {e}")

# Test creating a new schedule
print("\n--- Testing Schedule Creation ---")

new_schedule = {
    "from_date": "2025-10-15",
    "to_date": "2025-12-15",
    "from_time": "09:00",
    "to_time": "18:00",
    "valid_dates": "1234567",
    "min_hours": 2,
    "currency_id": 1,
    "price": 15.00,
    "price_vip": 25.00,
    "can_refund": True,
    "refund_before_hours": 24,
    "extras": "Umbrella +5€",
    "extras_vip": "Premium Umbrella +10€"
}

try:
    response = requests.post(schedules_url, json=new_schedule)
    print(f"Create schedule status code: {response.status_code}")
    
    if response.status_code == 200:
        result = response.json()
        if result.get('success'):
            schedule_id = result.get('schedule_id')
            print(f"✅ Schedule created successfully with ID: {schedule_id}")
        else:
            print(f"❌ Failed to create schedule: {result.get('error', 'Unknown error')}")
    else:
        print(f"❌ Failed to create schedule: {response.status_code}")
        
except Exception as e:
    print(f"❌ Error creating schedule: {e}")

# Wait a moment for the schedule to be created
time.sleep(1)

# Test retrieving the created schedule
print("\n--- Testing Schedule Retrieval ---")
try:
    response = requests.get(schedules_url)
    print(f"Get schedules status code: {response.status_code}")
    
    if response.status_code == 200:
        schedules = response.json()
        if schedules:
            schedule = schedules[0]
            print(f"✅ Retrieved schedule: {schedule.get('schedule_id')} - {schedule.get('from_date')} to {schedule.get('to_date')}")
            print(f"   Price: {schedule.get('currency_symbol')}{schedule.get('price')}")
            print(f"   VIP Price: {schedule.get('currency_symbol')}{schedule.get('price_vip')}")
        else:
            print("❌ No schedules found")
    else:
        print(f"❌ Failed to retrieve schedules: {response.status_code}")
        
except Exception as e:
    print(f"❌ Error retrieving schedules: {e}")

print("\n--- Test completed ---")