import requests
import time

# Test updating and deleting a schedule
beach_id = "9264fc4e-14f4-475d-ac96-4cb689a63783"
schedules_url = f"http://localhost:8080/api/beach-schedules/{beach_id}"

print("--- Testing Schedule Update ---")

# First, get the existing schedule ID
try:
    response = requests.get(schedules_url)
    if response.status_code == 200:
        schedules = response.json()
        if schedules:
            schedule_id = schedules[0]['schedule_id']
            print(f"Found schedule ID: {schedule_id}")
            
            # Test updating the schedule
            updated_schedule = {
                "from_date": "2025-11-01",
                "to_date": "2026-01-31",
                "from_time": "10:00",
                "to_time": "19:00",
                "valid_dates": "12345",
                "min_hours": 3,
                "currency_id": 2,  # USD
                "price": 20.00,
                "price_vip": 35.00,
                "can_refund": True,
                "refund_before_hours": 48,
                "extras": "Umbrella +10$",
                "extras_vip": "Premium Umbrella +20$"
            }
            
            update_url = f"http://localhost:8080/api/beach-schedules/{schedule_id}"
            response = requests.put(update_url, json=updated_schedule)
            print(f"Update schedule status code: {response.status_code}")
            
            if response.status_code == 200:
                result = response.json()
                if result.get('success'):
                    print("✅ Schedule updated successfully")
                else:
                    print(f"❌ Failed to update schedule: {result.get('error', 'Unknown error')}")
            else:
                print(f"❌ Failed to update schedule: {response.status_code}")
        else:
            print("❌ No schedules found to update")
    else:
        print(f"❌ Failed to retrieve schedules: {response.status_code}")
        
except Exception as e:
    print(f"❌ Error updating schedule: {e}")

# Wait a moment
time.sleep(1)

# Verify the update
print("\n--- Verifying Schedule Update ---")
try:
    response = requests.get(schedules_url)
    if response.status_code == 200:
        schedules = response.json()
        if schedules:
            schedule = schedules[0]
            print(f"✅ Retrieved updated schedule: {schedule.get('schedule_id')}")
            print(f"   Date range: {schedule.get('from_date')} to {schedule.get('to_date')}")
            print(f"   Time range: {schedule.get('from_time')} to {schedule.get('to_time')}")
            print(f"   Price: {schedule.get('currency_symbol')}{schedule.get('price')}")
            print(f"   VIP Price: {schedule.get('currency_symbol')}{schedule.get('price_vip')}")
            print(f"   Currency: {'USD' if schedule.get('currency_symbol') == '$' else 'EUR'}")
        else:
            print("❌ No schedules found")
    else:
        print(f"❌ Failed to retrieve schedules: {response.status_code}")
        
except Exception as e:
    print(f"❌ Error retrieving updated schedules: {e}")

# Test deleting the schedule
print("\n--- Testing Schedule Deletion ---")
try:
    response = requests.get(schedules_url)
    if response.status_code == 200:
        schedules = response.json()
        if schedules:
            schedule_id = schedules[0]['schedule_id']
            delete_url = f"http://localhost:8080/api/beach-schedules/delete/{schedule_id}"
            response = requests.delete(delete_url)
            print(f"Delete schedule status code: {response.status_code}")
            
            if response.status_code == 200:
                result = response.json()
                if result.get('success'):
                    print("✅ Schedule deleted successfully")
                else:
                    print(f"❌ Failed to delete schedule: {result.get('error', 'Unknown error')}")
            else:
                print(f"❌ Failed to delete schedule: {response.status_code}")
        else:
            print("❌ No schedules found to delete")
    else:
        print(f"❌ Failed to retrieve schedules: {response.status_code}")
        
except Exception as e:
    print(f"❌ Error deleting schedule: {e}")

# Verify the deletion
print("\n--- Verifying Schedule Deletion ---")
try:
    response = requests.get(schedules_url)
    if response.status_code == 200:
        schedules = response.json()
        if len(schedules) == 0:
            print("✅ All schedules deleted successfully")
        else:
            print(f"❌ Schedules still exist: {len(schedules)} schedules found")
    else:
        print(f"❌ Failed to retrieve schedules: {response.status_code}")
        
except Exception as e:
    print(f"❌ Error verifying deletion: {e}")

print("\n--- All tests completed ---")