import pytest
import playwright.sync_api
from playwright.sync_api import expect
import uuid
import time


def test_full_user_flow(playwright: playwright.sync_api.Playwright):
    """
    Test the complete user flow:
    1. Navigate to localhost:5001
    2. Go to signup page from login
    3. Create a new user
    4. Configure IMAP with specified details
    5. Sync folders
    6. Verify at least 3 folder cards are displayed
    """
    
    # Launch browser
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    
    try:
        # Generate unique data for this test run
        import random
        random_number = random.randint(1000, 9999)
        test_email = f"testuser{random_number}@example.com"
        test_username = f"user{random_number}"
        
        # Step 1: Navigate to localhost:5000 (login page)
        page.goto("http://localhost:5001")
        
        # Wait for page to load
        page.wait_for_load_state("networkidle")

        # Step 2: Navigate to signup page
        # Click the "Sign up" link
        page.click("a:has-text('Sign up')")
        
        # Wait for navigation to complete
        page.wait_for_load_state("networkidle")

        # Step 3: Create a new user
        # Fill out signup form
        page.fill("input[name='first_name']", "Test")
        page.fill("input[name='last_name']", "User")
        page.fill("input[name='email']", test_email)
        page.fill("input[name='password']", "TestPassword123")
        page.fill("input[name='confirm_password']", "TestPassword123")
        page.check("input[name='terms']")
        
        # Submit signup form
        page.click("button:has-text('Create Account')")
        
        # Wait for navigation to complete
        page.wait_for_load_state("networkidle")
        
        # Step 3: Configure IMAP
        # Wait for and click the "Configure IMAP" button
        configure_button = page.locator("button:has-text('Configure IMAP')")
        configure_button.click()
        
        # Wait for modal to appear
        page.wait_for_selector("#imap-modal")
        
        # Fill out IMAP configuration form
        page.fill("input[name='server']", "localhost")
        page.fill("input[name='port']", "5143")
        page.fill("input[name='username']", "user1@example.com")
        page.fill("input[name='password']", "password1")
        
        # Uncheck SSL checkbox
        page.uncheck("input[name='use_ssl']")
        
        # Submit IMAP configuration form
        page.click("button[type='submit']")
        
        # Wait for connection test to complete
        page.wait_for_selector("h3:has-text('Configure IMAP Folders')")
        
        # Step 5: Click "Save and Continue" on the final modal
        page.click("button:has-text('Save and Continue')")
        
        # Wait for modal to close and navigation to complete
        page.wait_for_load_state("networkidle")
        
        # Wait for modal to appear and folders to sync
        page.wait_for_selector("#folders-list .card", timeout=10000)
        
        # Step 6: Verify at least 3 folder cards are displayed
        folder_cards = page.locator(".card.bg-base-100.shadow-xl")
        
        # Wait for at least 3 folder cards to be visible
        page.wait_for_function("document.querySelectorAll('.card.bg-base-100.shadow-xl').length >= 3", timeout=15000)
        
        # Get the count of folder cards
        count = folder_cards.count()
        print(f"Found {count} folder cards")
        
        # Verify we have at least 3 folder cards
        assert count >= 3, f"Expected at least 3 folder cards, but found {count}"
        
        # Verify each folder card has the expected structure
        for i in range(min(3, count)):
            card = folder_cards.nth(i)
            expect(card).to_be_visible()
            
    finally:
        # Close browser
        browser.close()