"""Detailed structural look at 논산시, 아산시, 부여군 main sitemaps."""
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}')
    elif name == 'button':
        txt = el.get_text(strip=True)[:50]
        lines.append('  ' * depth + f'{label} BTN "{txt}"')
    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


# 논산시 - look at div.sitemap.type1
print('=== 논산시 div.sitemap.type1 ===')
real, html = fetch('https://nonsan.go.kr/kor/html/sub07/0701.html')
soup = BeautifulSoup(html, 'html.parser')
el = soup.select_one('div.sitemap.type1') or soup.select_one('div.sitemap')
if el:
    cls = ' '.join(el.get('class', []))
    print(f'  Container: {el.name}.{cls} a={len(el.find_all("a"))}')
    for line in outline(el, max_lines=70):
        print('  ', line)

# 부여군 - try /html/kr/sitemap_pop.html or similar
print('\n\n=== 부여군 alternate sitemap probes ===')
for path in [
    '/html/kr/sitemap_pop.html',
    '/html/kr/sitemap.html',
    '/html/kr/html/sub06/0601.html',
    '/html/kr/html/sub05/0501.html',
    '/html/kr/html/sub04/0401.html',
    '/html/kr/html/sub09/0901.html',
    '/html/kr/popup/sitemap.html',
    '/html/kr/include/sitemap.html',
]:
    url = f'https://www.buyeo.go.kr{path}'
    try:
        r = requests.get(url, headers=H, timeout=8, verify=False, allow_redirects=True)
        r.encoding = r.apparent_encoding
        if r.status_code == 200:
            s = BeautifulSoup(r.text, 'html.parser')
            best_count = 0
            best_sel = ''
            for sel in ['.sitemap', '#sitemap', '.sitemap_grep', '.allMenu', '.pc_sitemap', 'div[class*=sitemap]']:
                for el in s.select(sel):
                    ac = len(el.find_all('a'))
                    if ac > best_count:
                        best_count = ac
                        best_sel = sel
            print(f'  {path}: HTTP 200, best_sel={best_sel}, a={best_count}')
    except Exception as e:
        pass

# 부여군 main page - look for inline pc_sitemap or modal
print('\n\n=== 부여군 main page: look for inline allMenu / pc_sitemap modal ===')
real, html = fetch('https://www.buyeo.go.kr/html/kr/')
soup = BeautifulSoup(html, 'html.parser')
# Search for elements with id or class containing "sitemap"
for el in soup.select('[class*=sitemap], [id*=sitemap], [id*=allMenu], [class*=allMenu], [id*=allmenu], [class*=allmenu]'):
    cls = ' '.join(el.get('class', []))
    eid = el.get('id', '')
    ac = len(el.find_all('a'))
    print(f'  {el.name}#{eid}.{cls} a={ac}')

# 아산시 - look at the .gnb-menu inline structure (use sectioned anchors mGnb-anchor1..6)
print('\n\n=== 아산시: all gnb-sub-list sections combined ===')
real, html = fetch('https://www.asan.go.kr/main/')
soup = BeautifulSoup(html, 'html.parser')
all_a = []
for sec in soup.select('div[id^=mGnb-anchor]'):
    eid = sec.get('id', '')
    section_a = sec.find_all('a', href=True)
    print(f'  {eid}: a={len(section_a)}')
    for a in section_a[:3]:
        print(f'    "{a.get_text(strip=True)[:40]}" → {a["href"][:80]}')
