#!/usr/bin/env python3
"""
测试LawRisk界面的截图工具
用于验证更新后的政务风格界面设计
"""
from playwright.sync_api import sync_playwright, expect
import os

def test_login_page(page):
    """测试登录页面"""
    print("📝 Testing login page...")
    page.goto('http://localhost:8000/fs-ai-asistant/api/workflow/lawrisk/login')
    page.wait_for_load_state('networkidle')
    page.screenshot(path='/tmp/login_page.png', full_page=True)
    print("✅ Login page screenshot saved to /tmp/login_page.png")

def test_v2_tester(page):
    """测试V2测试界面"""
    print("📝 Testing V2 tester page...")
    page.goto('http://localhost:8000/static/v2_tester.html')
    page.wait_for_load_state('networkidle')
    page.screenshot(path='/tmp/v2_tester.png', full_page=True)
    print("✅ V2 tester screenshot saved to /tmp/v2_tester.png")

def test_db_admin_page(page):
    """测试数据库管理页面（需要登录）"""
    print("📝 Testing DB admin page...")
    # 先访问登录页面
    page.goto('http://localhost:8000/fs-ai-asistant/api/workflow/lawrisk/login')
    page.wait_for_load_state('networkidle')

    # 尝试登录（如果有默认账户）
    try:
        page.fill('input[name="username"]', 'admin')
        page.fill('input[name="password"]', 'admin123')
        page.click('button[type="submit"]')
        page.wait_for_load_state('networkidle')
        page.wait_for_timeout(2000)
    except Exception as e:
        print(f"⚠️  Login might not work: {e}")

    # 然后访问数据库管理页面
    page.goto('http://localhost:8000/static/db_admin.html')
    page.wait_for_load_state('networkidle')
    page.screenshot(path='/tmp/db_admin.png', full_page=True)
    print("✅ DB admin screenshot saved to /tmp/db_admin.png")

def test_super_admin_page(page):
    """测试超级管理员页面（需要登录）"""
    print("📝 Testing super admin page...")
    page.goto('http://localhost:8000/static/super_admin.html')
    page.wait_for_load_state('networkidle')
    page.screenshot(path='/tmp/super_admin.png', full_page=True)
    print("✅ Super admin screenshot saved to /tmp/super_admin.png")

def main():
    print("🚀 Starting UI screenshot tests...\n")
    print("=" * 60)

    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        context = browser.new_context(
            viewport={'width': 1920, 'height': 1080},
            device_scale_factor=1
        )
        page = context.new_page()

        # 测试所有页面
        try:
            test_login_page(page)
            print()
            test_v2_tester(page)
            print()
            test_db_admin_page(page)
            print()
            test_super_admin_page(page)
            print()
        except Exception as e:
            print(f"❌ Error during testing: {e}")
            import traceback
            traceback.print_exc()

        browser.close()

    print("=" * 60)
    print("\n✅ All tests completed!")
    print("\n📊 Screenshots saved:")
    print("   - /tmp/login_page.png (登录页面)")
    print("   - /tmp/v2_tester.png (V2测试界面)")
    print("   - /tmp/db_admin.png (数据库管理界面)")
    print("   - /tmp/super_admin.png (超级管理员界面)")

if __name__ == '__main__':
    main()
