"""Inspect specific selectors found in probe6 for 아산시, 서산시, 부여군."""
import warnings
from urllib.parse import urljoin

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


# 부여군: find .pc_sitemap
print('\n=== 부여군: .pc_sitemap 또는 inline sitemap ===')
real, html = fetch('https://www.buyeo.go.kr/html/kr/')
soup = BeautifulSoup(html, 'html.parser')
for sel in ['.pc_sitemap', '#pc_sitemap', '.sitemap_grep', '.allMenu', '.allmenu',
            'div[class*=sitemap]', 'div[class*=gnb]']:
    for el in soup.select(sel):
        ac = len(el.find_all('a'))
        if ac >= 30:
            cls = ' '.join(el.get('class', []))
            print(f'  ★ sel={sel} {el.name}.{cls} id={el.get("id","")} a={ac}')

# 아산시: mobile-nav.krds-gnb-mobile
print('\n=== 아산시: mobile-nav ===')
real, html = fetch('https://www.asan.go.kr/main/')
soup = BeautifulSoup(html, 'html.parser')
el = soup.select_one('nav#mobile-nav')
if el:
    for line in outline(el, max_lines=80):
        print('  ', line)

# 서산시: top_menu
print('\n=== 서산시: ul#top_menu ===')
real, html = fetch('https://www.seosan.go.kr/www/index.do')
soup = BeautifulSoup(html, 'html.parser')
el = soup.select_one('ul#top_menu') or soup.select_one('div.menu_wrap')
if el:
    for line in outline(el, max_lines=80):
        print('  ', line)
