import urllib.request
import json

booking_payload = {
    'vehicleUuid': '03fddaed-39a9-457f-998f-fa1665867717',
    'customerName': 'Tendai Moyo',
    'customerEmail': 'tendai.moyo@example.com',
    'customerPhone': '+263 77 123 4567',
    'pickupLocation': 'Victoria Falls Airport (VFA)',
    'dropoffLocation': 'Victoria Falls Depot',
    'pickupDate': '2026-11-01',
    'dropoffDate': '2026-11-06',
    'insuranceTier': 'PREMIUM',
    'addOns': ['ADDON_EXTRA_DRIVER', 'ADDON_GPS'],
    'specialRequests': 'Airport flight meet and greet'
}

# 1. Customer creates booking
req = urllib.request.Request(
    'http://localhost:8080/api/v1/bookings',
    data=json.dumps(booking_payload).encode('utf-8'),
    headers={'Content-Type': 'application/json'}
)
with urllib.request.urlopen(req) as resp:
    booking = json.loads(resp.read().decode('utf-8'))
    print("1. Created booking:", booking['bookingReference'], "Total: $" + str(booking['grandTotal']))

# 2. Admin retrieves all bookings
admin_req = urllib.request.Request('http://localhost:8080/api/v1/admin/bookings', headers={'Authorization': 'Bearer dev-test-token'})
with urllib.request.urlopen(admin_req) as resp:
    all_bookings = json.loads(resp.read().decode('utf-8'))
    print("2. Admin bookings retrieved:", len(all_bookings))
    assert any(b['bookingReference'] == booking['bookingReference'] for b in all_bookings)

# 3. Admin updates booking status to CONFIRMED
status_req = urllib.request.Request(
    f"http://localhost:8080/api/v1/admin/bookings/{booking['bookingReference']}/status",
    data=json.dumps({'status': 'CONFIRMED'}).encode('utf-8'),
    headers={'Content-Type': 'application/json', 'Authorization': 'Bearer dev-test-token'}
)
with urllib.request.urlopen(status_req) as resp:
    updated = json.loads(resp.read().decode('utf-8'))
    print("3. Admin updated booking status:", updated['reference'], "->", updated['status'])
    assert updated['status'] == 'CONFIRMED'

print("\nAll Customer -> Admin reservation lifecycle tests passed successfully!")
