import pytest
from playwright.sync_api import Page, expect
import time

BASE_URL = "https://curaflow.applaude.net"

def test_login_flow(page: Page):
    """Test the login page loads and allows entry"""
    page.goto(f"{BASE_URL}/login.html")
    
    # Check that we are on the login page
    expect(page.locator("h2")).to_contain_text("Welcome")
    
    # Login as Doctor
    page.locator("#clinicId").fill("doctor@sharma.clinic")
    page.locator("#password").fill("doc123")
    page.locator("button[type='submit']").click()
    
    # Should navigate to doctor dashboard
    page.wait_for_url(f"{BASE_URL}/dashboard.html")
    expect(page.locator("body")).to_contain_text("Overview")

def test_doctor_queue_and_visit(page: Page):
    """Test that a doctor can view the queue in Zen mode and start a visit"""
    # Setup: Login
    page.goto(f"{BASE_URL}/login.html")
    page.locator("#clinicId").fill("doctor@sharma.clinic")
    page.locator("#password").fill("doc123")
    page.locator("button[type='submit']").click()
    page.wait_for_url(f"{BASE_URL}/dashboard.html")
    
    # In Zen mode, the Up Next queue should be visible immediately
    expect(page.locator("h3:has-text('Up Next')")).to_be_visible()
    
    # Click on Anjali Singh in the pipeline
    page.locator(".pipeline-item:has-text('Anjali Singh')").click()
    
    # The header should update to show Anjali Singh
    expect(page.locator("body")).to_contain_text("Anjali Singh")
    
    # The consultation notes area should be visible
    expect(page.locator("#consultationNotes")).to_be_visible()

def test_lab_technician_flow(page: Page):
    """Test the Lab Tech flow for submitting results"""
    page.goto(f"{BASE_URL}/login.html")
    page.locator("#clinicId").fill("doctor@sharma.clinic")
    page.locator("#password").fill("doc123")
    page.locator("button[type='submit']").click()
    
    # Need to wait for login logic to redirect
    page.wait_for_url(f"{BASE_URL}/dashboard.html")
    
    # Manually navigate to lab
    page.goto(f"{BASE_URL}/lab.html")
    expect(page.locator("body")).to_contain_text("Lab")
    
    # Verify the structure has a pending tests table
    expect(page.locator("h2:has-text('Live Test Queue')")).to_be_visible()
    
def test_pharmacy_technician_flow(page: Page):
    """Test the Pharmacy flow"""
    page.goto(f"{BASE_URL}/login.html")
    page.locator("#clinicId").fill("doctor@sharma.clinic")
    page.locator("#password").fill("doc123")
    page.locator("button[type='submit']").click()
    
    page.wait_for_url(f"{BASE_URL}/dashboard.html")
    
    # Manually navigate to pharmacy
    page.goto(f"{BASE_URL}/pharmacy.html")
    expect(page.locator("body")).to_contain_text("Pharmacy")
    
    # Should have a fulfill prescriptions list
    expect(page.locator("h2:has-text('Pending Prescriptions')")).to_be_visible()
