"""
Test script for the complete appointment scheduling flow
"""

import asyncio
import uuid
import json
import os
from utils.appointment_state import update_appointment_state
from utils.appointment_scheduler import schedule_appointment_from_state

# Test data
test_conversation_id = str(uuid.uuid4())

async def test_appointment_flow():
    """Test the complete appointment scheduling flow"""
    print("\n=== Testing Complete Appointment Flow ===")
    
    # Step 1: Create an appointment state with all required information
    print("\nStep 1: Creating appointment state with all required information")
    
    appointment_state = {
        "meeting_request": True,
        "meeting_request_stage": "ready_to_schedule",
        "missing_fields": [],
        "extracted_info": {
            "name": "Test User",
            "email": "test@example.com",
            "phone": "123-456-7890",
            "company": "Test Company",
            "topic": "Website Development Project",
            "preferred_date": "2025-10-01",
            "preferred_time": "14:00",
            "additional_notes": "This is a test appointment"
        },
        "suggested_topic": "Website Development Project"
    }
    
    # Update the appointment state
    update_result = update_appointment_state(test_conversation_id, appointment_state)
    print(f"Update appointment state result: {update_result}")
    
    # Step 2: Schedule the appointment using the appointment scheduler
    print("\nStep 2: Scheduling the appointment")
    
    scheduling_result = await schedule_appointment_from_state(test_conversation_id)
    print("Scheduling result:")
    print(json.dumps(scheduling_result, indent=2))
    
    # Step 3: Verify that appointments.json was created
    print("\nStep 3: Verifying appointments.json was created")
    
    appointments_file = "data/appointments.json"
    if os.path.exists(appointments_file):
        with open(appointments_file, "r") as f:
            appointments = json.load(f)
            print(f"Found {len(appointments)} appointments in {appointments_file}")
            print("First appointment:")
            first_appointment_id = next(iter(appointments))
            print(json.dumps(appointments[first_appointment_id], indent=2))
    else:
        print(f"Error: {appointments_file} was not created")
    
    print("\nTest completed!")

if __name__ == "__main__":
    asyncio.run(test_appointment_flow())
