#!/usr/bin/env python3
"""
Extract Gemini chat 33edddd2e796d6a9 content
Login as kinan@agileadapt.com and extract all conversation content
"""

import asyncio
import time
import os
from playwright.async_api import async_playwright

EMAIL = "kinan@agileadapt.com"
PASSWORD = "Systema55"
CHAT_URL = "https://gemini.google.com/app/33edddd2e796d6a9"
OUTPUT_PATH = "/mnt/e/genesis-system/Conversations/gemini_chat_33edddd2e796d6a9_FULL.md"
SCREENSHOT_DIR = "/mnt/e/genesis-system/hive/progress"

async def extract_chat():
    async with async_playwright() as p:
        browser = await p.chromium.launch(
            headless=True,
            args=['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage']
        )
        context = await browser.new_context(
            viewport={"width": 1280, "height": 900},
            user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
        )
        page = await context.new_page()

        print("Step 1: Navigating to Google login...")
        await page.goto("https://accounts.google.com", wait_until='domcontentloaded', timeout=30000)
        await page.wait_for_timeout(2000)

        # Take screenshot of login page
        await page.screenshot(path=f"{SCREENSHOT_DIR}/gemini33edd_01_login.png")
        print("  Screenshot saved: gemini33edd_01_login.png")

        print("Step 2: Entering email...")
        try:
            await page.locator('input[type="email"]').fill(EMAIL)
            await page.wait_for_timeout(500)
            await page.keyboard.press("Enter")
            await page.wait_for_timeout(3000)
        except Exception as e:
            print(f"  Email fill error: {e}")
            # Try alternate selector
            await page.fill('#identifierId', EMAIL)
            await page.wait_for_timeout(500)
            await page.click('#identifierNext')
            await page.wait_for_timeout(3000)

        await page.screenshot(path=f"{SCREENSHOT_DIR}/gemini33edd_02_after_email.png")
        print("  Screenshot saved: gemini33edd_02_after_email.png")

        print("Step 3: Entering password...")
        try:
            await page.locator('input[type="password"]').fill(PASSWORD)
            await page.wait_for_timeout(500)
            await page.keyboard.press("Enter")
            await page.wait_for_timeout(5000)
        except Exception as e:
            print(f"  Password fill error: {e}")

        await page.screenshot(path=f"{SCREENSHOT_DIR}/gemini33edd_03_after_password.png")
        print("  Screenshot saved: gemini33edd_03_after_password.png")

        current_url = page.url
        print(f"  Current URL after login: {current_url}")

        # Check if we need 2FA or other challenge
        page_content = await page.inner_text('body')
        if "2-Step" in page_content or "2FA" in page_content or "verification" in page_content.lower():
            print("  WARNING: 2FA/verification required")

        print("Step 4: Navigating to Gemini chat...")
        await page.goto(CHAT_URL, wait_until='domcontentloaded', timeout=30000)
        await page.wait_for_timeout(5000)

        await page.screenshot(path=f"{SCREENSHOT_DIR}/gemini33edd_04_chat_page.png")
        print("  Screenshot saved: gemini33edd_04_chat_page.png")

        current_url = page.url
        print(f"  Current URL: {current_url}")

        # Scroll to load all content
        print("Step 5: Scrolling to load all content...")
        for i in range(5):
            await page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
            await page.wait_for_timeout(1000)
            await page.evaluate("window.scrollTo(0, 0)")
            await page.wait_for_timeout(500)

        # Final scroll to bottom
        await page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
        await page.wait_for_timeout(2000)

        print("Step 6: Extracting page content...")
        full_text = await page.inner_text('body')
        page_title = await page.title()

        print(f"  Page title: {page_title}")
        print(f"  Full text length: {len(full_text)} chars")

        # Try to get structured messages
        print("Step 7: Trying to get structured message elements...")

        # Try various selectors for Gemini messages
        structured_content = []

        selectors_to_try = [
            'model-response',
            'user-query',
            '[data-message-author-role]',
            '.conversation-turn',
            'message-content',
            '[class*="message"]',
            '[class*="turn"]',
            '[class*="response"]',
            '[class*="query"]',
            'p',
        ]

        for selector in selectors_to_try[:6]:  # Try first 6
            try:
                elements = await page.locator(selector).all()
                if elements:
                    print(f"  Found {len(elements)} elements with selector: {selector}")
                    texts = []
                    for el in elements:
                        try:
                            text = await el.inner_text()
                            if text.strip() and len(text.strip()) > 10:
                                texts.append(text.strip())
                        except:
                            pass
                    if texts:
                        structured_content.append(f"\n## Selector: {selector}\n" + "\n---\n".join(texts[:50]))
            except Exception as e:
                print(f"  Selector {selector} failed: {e}")

        # Get page HTML for further analysis
        page_html = await page.content()
        print(f"  Page HTML length: {len(page_html)} chars")

        # Save screenshot of final state
        await page.screenshot(path=f"{SCREENSHOT_DIR}/gemini33edd_05_final.png", full_page=True)
        print("  Screenshot saved: gemini33edd_05_final.png")

        # Build output markdown
        output_content = f"""# Gemini Chat Extraction: 33edddd2e796d6a9
**Extracted**: 2026-02-20
**URL**: {CHAT_URL}
**Page Title**: {page_title}
**Final URL**: {current_url}
**Total Body Text Length**: {len(full_text)} characters

---

## FULL BODY TEXT

{full_text}

---

## STRUCTURED ELEMENTS

{chr(10).join(structured_content) if structured_content else "No structured elements found with tested selectors"}

"""

        # Save to file
        os.makedirs("/mnt/e/genesis-system/Conversations", exist_ok=True)
        with open(OUTPUT_PATH, 'w', encoding='utf-8') as f:
            f.write(output_content)

        print(f"\nStep 8: Content saved to {OUTPUT_PATH}")
        print(f"  Output file size: {os.path.getsize(OUTPUT_PATH)} bytes")

        # Also save raw HTML for analysis
        html_path = OUTPUT_PATH.replace('.md', '_raw.html')
        with open(html_path, 'w', encoding='utf-8') as f:
            f.write(page_html)
        print(f"  Raw HTML saved to {html_path}")

        await browser.close()

        return {
            'full_text': full_text,
            'page_title': page_title,
            'current_url': current_url,
            'text_length': len(full_text),
            'structured_sections': len(structured_content)
        }

if __name__ == "__main__":
    result = asyncio.run(extract_chat())
    print(f"\n=== EXTRACTION COMPLETE ===")
    print(f"Text length: {result['text_length']}")
    print(f"Page title: {result['page_title']}")
    print(f"Final URL: {result['current_url']}")
    print(f"Structured sections found: {result['structured_sections']}")
