"""단양·진천 구조 outline + 보은군 alternative."""
import warnings
import socket
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, timeout=20):
    try:
        r = requests.get(url, headers=H, timeout=timeout, verify=False, allow_redirects=True)
        r.encoding = r.apparent_encoding
        return r.status_code, r.url, r.text
    except Exception as e:
        return 0, str(e), ''


def outline(el, d=0, max_lines=80, 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', '')
    lbl = name
    if eid: lbl += f'#{eid}'
    if cls: lbl += '.' + cls.replace(' ', '.')
    if name == 'a':
        t = el.get_text(strip=True)[:40]
        h = el.get('href', '')[:80]
        lines.append('  ' * d + f'{lbl} "{t}" → {h}')
    else:
        lines.append('  ' * d + lbl)
    for c in el.find_all(recursive=False):
        if c.name in ('script', 'style'): continue
        outline(c, d+1, max_lines, lines)
        if len(lines) >= max_lines: return lines
    return lines


# 단양군 - outline #menu_sitemap
print('='*70)
print('단양군 — #menu_sitemap outline')
print('='*70)
_, _, html = fetch('https://www.danyang.go.kr/dy21/98')
soup = BeautifulSoup(html, 'html.parser')
el = soup.select_one('#menu_sitemap') or soup.select_one('#contents_sitemap')
if el:
    print(f'  Container a={len(el.find_all("a"))}')
    for line in outline(el, max_lines=60):
        print('   ', line)


# 진천군 - outline nav#gnb on sub.do?menukey=445
print('\n' + '='*70)
print('진천군 — sub.do?menukey=445 nav#gnb outline')
print('='*70)
_, _, html = fetch('https://www.jincheon.go.kr/home/sub.do?menukey=445')
soup = BeautifulSoup(html, 'html.parser')
el = soup.select_one('nav#gnb') or soup.select_one('#gnb')
if el:
    print(f'  Container a={len(el.find_all("a"))}')
    for line in outline(el, max_lines=60):
        print('   ', line)


# 보은군 — try alternate hosts
print('\n' + '='*70)
print('보은군 — DNS/ alternate hosts')
print('='*70)
for host in ['www.boeun.go.kr', 'boeun.go.kr', 'boeun.chungbuk.go.kr']:
    try:
        ip = socket.gethostbyname(host)
        print(f'  {host} → {ip}')
    except Exception as e:
        print(f'  {host}: {e}')

# Try fetching via curl-style direct
for url in ['https://www.boeun.go.kr/www/index.do',
            'http://www.boeun.go.kr/www/index.do',
            'https://boeun.go.kr/www/index.do',
            'https://www.boeun.go.kr/']:
    code, real, _ = fetch(url, timeout=10)
    print(f'  {url} → HTTP {code} ({real[:60]})')
