# -*- coding: utf-8 -*-
"""지오영 API 엔드포인트 분석"""

import asyncio
from playwright.async_api import async_playwright

async def capture_network():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()
        
        # 네트워크 요청 캡처
        requests_log = []
        
        def log_request(request):
            if 'geoweb' in request.url:
                requests_log.append({
                    'url': request.url,
                    'method': request.method,
                    'post_data': request.post_data
                })
        
        page.on('request', log_request)
        
        # 로그인
        print("로그인 중...")
        await page.goto('https://gwn.geoweb.kr/Member/Login')
        await page.fill('input[type="text"]', '7390')
        await page.fill('input[type="password"]', 'trajet6640')
        await page.click('button, input[type="submit"]')
        await page.wait_for_load_state('networkidle')
        print("로그인 완료")
        
        # 메인 페이지
        await page.goto('https://gwn.geoweb.kr/Home/Index')
        await page.wait_for_load_state('networkidle')
        
        # 검색
        print("검색 중...")
        search_input = await page.query_selector('input#srchText, input[name="srchText"]')
        if search_input:
            await search_input.fill('643104281')
            
        # 검색 버튼
        search_btn = await page.query_selector('button:has-text("검색"), input[type="submit"]')
        if search_btn:
            await search_btn.click()
        else:
            await page.keyboard.press('Enter')
            
        await page.wait_for_timeout(3000)
        
        # 제품 행 클릭
        print("제품 선택 중...")
        rows = await page.query_selector_all('table tbody tr')
        if rows:
            await rows[0].click()
            await page.wait_for_timeout(2000)
        
        # 담기 버튼
        print("담기 버튼 클릭...")
        add_btn = await page.query_selector('button:has-text("담기")')
        if add_btn:
            await add_btn.click()
            await page.wait_for_timeout(3000)
        
        await browser.close()
        
        print("\n" + "="*60)
        print("캡처된 요청들:")
        print("="*60)
        for r in requests_log:
            if r['method'] == 'POST' or 'cart' in r['url'].lower() or 'order' in r['url'].lower():
                print(f"\n[{r['method']}] {r['url']}")
                if r['post_data']:
                    print(f"    Data: {r['post_data'][:200]}")

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