"""Find sitemap containers in 부여군 main page and 아산시 desktop GNB."""
import warnings
import requests
from bs4 import BeautifulSoup

warnings.filterwarnings('ignore')

UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
H = {'User-Agent': UA}


def fetch(url):
    r = requests.get(url, headers=H, timeout=15, verify=False, allow_redirects=True)
    r.encoding = r.apparent_encoding
    return r.url, r.text


def outline(el, depth=0, max_lines=120, lines=None):
    if lines is None:
        lines = []
    if len(lines) >= max_lines:
        return lines
    name = el.name
    cls = ' '.join(el.get('class', []))
    eid = el.get('id', '')
    label = name
    if eid:
        label += f'#{eid}'
    if cls:
        label += '.' + cls.replace(' ', '.')
    if name == 'a':
        txt = el.get_text(strip=True)[:50]
        href = el.get('href', '')[:80]
        lines.append('  ' * depth + f'{label} "{txt}" → {href}')
    else:
        lines.append('  ' * depth + label)
    for c in el.find_all(recursive=False):
        if c.name in ('script', 'style'):
            continue
        outline(c, depth + 1, max_lines, lines)
        if len(lines) >= max_lines:
            return lines
    return lines


# 부여군 — scan ALL container types for menu-like content
print('=== 부여군: scan all containers ===')
real, html = fetch('https://www.buyeo.go.kr/html/kr/')
soup = BeautifulSoup(html, 'html.parser')
# Find divs with most anchors
candidates = []
for d in soup.find_all(['div', 'nav', 'ul']):
    a_count = len(d.find_all('a'))
    if a_count >= 100:
        cls = ' '.join(d.get('class', []))
        eid = d.get('id', '')
        candidates.append((a_count, d.name, eid, cls, d))
candidates.sort(reverse=True, key=lambda x: x[0])
for ac, name, eid, cls, _ in candidates[:10]:
    print(f'  {name}#{eid}.{cls[:60]} a={ac}')

# Outline top container
if candidates:
    print('\n  Top container outline:')
    for line in outline(candidates[0][4], max_lines=60):
        print('   ', line)

# 아산시 — desktop GNB
print('\n\n=== 아산시: desktop GNB ===')
real, html = fetch('https://www.asan.go.kr/main/')
soup = BeautifulSoup(html, 'html.parser')
# Look for desktop GNB sub-lists
for el in soup.select('[id^=mGnb-anchor], .gnb-sub-list, .submenu-wrap, .gnb-wrap'):
    ac = len(el.find_all('a'))
    if ac >= 30:
        cls = ' '.join(el.get('class', []))
        eid = el.get('id', '')
        print(f'  {el.name}#{eid}.{cls} a={ac}')

# Try the main desktop nav specifically
el = soup.select_one('nav.krds-gnb:not(#mobile-nav)')
if el:
    print('\n  Desktop nav outline:')
    for line in outline(el, max_lines=80):
        print('   ', line)
