#!/usr/bin/env python3
"""
怀宁县人民政府 - 建设项目环评文件（报告书、报告表）审批
https://www.ahhn.gov.cn/grassroots/column/2000000951?catId=1002621
蓝汛(Lonsun) CMS - 基层政务公开目录
"""
import re
import sys
import time
import json
import hashlib
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin

DB_PATH = "/root/search.db"
SITE_NAME = "怀宁县-建设项目环评文件审批"
CATEGORY = "安徽"
BASE_URL = "https://www.ahhn.gov.cn/grassroots/column/2000000951?catId=1002621"
MAX_PAGES = 15  # 共15页

HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
    "Referer": "https://www.ahhn.gov.cn/grassroots/column/2000000951?catId=1002621",
}

INSERT_SQL = """
INSERT OR IGNORE INTO gov_raw 
    (site_name, page_url, title, publish_date, summary, content, category, attachments)
    VALUES (?, ?, ?, ?, '', ?, ?, ?)
"""


# ─── 正文取文本（2026-09-11）：行内节点直接拼接，只在块级边界 / <br> 处换行 ───
# ⚠️ 不要用 el.get_text("\n") 取正文 —— 它是「每个**文本节点**之间插 \n」，Word 粘贴的
#    公文把一行拆成 <span>提取码：</span>pwaj<span>。查阅…</span>，这些行内节点于是各自
#    成行（福泉 id=2095080103703914437 实例：`提取码：`/`pwaj`/`。查阅…` 各占一行）。
_BLOCK_TAGS = {'address', 'article', 'aside', 'blockquote', 'details', 'dialog', 'dd', 'div',
               'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2',
               'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'li', 'main', 'nav', 'ol',
               'p', 'pre', 'section', 'table', 'tbody', 'thead', 'tfoot', 'tr', 'td', 'th',
               'ul', 'center', 'caption'}


def body_text(el):
    """块级边界出换行、行内节点直接拼接、<br> 出换行（≈ 浏览器看到的换行结构）。"""
    if el is None:
        return ''
    import re as _re
    from bs4 import NavigableString
    out = []

    def walk(node):
        for ch in node.children:
            if isinstance(ch, NavigableString):
                out.append(str(ch))
            elif getattr(ch, 'name', None) == 'br':
                out.append('\n')
            elif getattr(ch, 'name', None) in _BLOCK_TAGS:
                out.append('\n')
                walk(ch)
                out.append('\n')
            else:
                walk(ch)
    walk(el)
    t = ''.join(out)
    t = _re.sub(r'[ \t\r\f\v]*\n[ \t\r\f\v]*', '\n', t)
    t = _re.sub(r'\n{3,}', '\n\n', t)
    return t.strip()


def init_db():
    import sqlite3
    conn = sqlite3.connect(DB_PATH, timeout=60)
    conn.execute("PRAGMA journal_mode=WAL")
    conn.execute("PRAGMA busy_timeout=5000")
    return conn


def safe_text(elem):
    """Extract text from an element, handling None."""
    if elem is None:
        return ""
    return elem.get_text(strip=True)


def extract_list_page(page_num):
    """Fetch a list page and extract article URLs + titles + dates."""
    if page_num == 1:
        url = BASE_URL
    else:
        url = f"{BASE_URL}&pageIndex={page_num}"

    try:
        r = requests.get(url, headers=HEADERS, timeout=30)
        r.encoding = "utf-8"
    except Exception as e:
        print(f"  [ERROR] Failed to fetch page {page_num}: {e}", flush=True)
        return []

    if r.status_code != 200:
        print(f"  [ERROR] HTTP {r.status_code} for page {page_num}", flush=True)
        return []

    soup = BeautifulSoup(r.text, "html.parser")
    items = []

    for li in soup.select("li.clearfix"):
        a_tag = li.find("a", class_="title")
        if not a_tag:
            continue

        # Title from title attribute (full title, no ellipsis)
        title = a_tag.get("title", "").strip()
        if not title:
            title = a_tag.get_text(strip=True)

        href = a_tag.get("href", "").strip()
        if not href or href.startswith("javascript"):
            continue
        full_url = urljoin(BASE_URL, href)

        # Date
        date_span = li.find("span", class_="date")
        date_str = safe_text(date_span).strip()

        items.append({
            "title": title,
            "url": full_url,
            "date": date_str,
        })

    print(f"  Page {page_num}: {len(items)} items", flush=True)
    return items


def extract_detail(url):
    """Fetch a detail page and extract full content."""
    try:
        r = requests.get(url, headers=HEADERS, timeout=30)
        r.encoding = "utf-8"
    except Exception as e:
        print(f"  [ERROR] Failed to fetch {url}: {e}", flush=True)
        return None

    if r.status_code != 200:
        print(f"  [ERROR] HTTP {r.status_code} for {url}", flush=True)
        return None

    soup = BeautifulSoup(r.text, "html.parser")

    # Get date from meta
    pub_date = ""
    meta_date = soup.find("meta", attrs={"name": "PubDate"})
    if meta_date and meta_date.get("content"):
        pub_date = meta_date["content"].strip()

    # Get content from j-fontContent
    content_div = soup.select_one("div.j-fontContent.newscontnet.minh500")
    if not content_div:
        content_div = soup.select_one("div.xxgk_content")
    if not content_div:
        content_div = soup.select_one("div.content_main")

    content_html = ""
    if content_div:
        content_html = str(content_div)

    # Extract text content
    content_text = ""
    if content_div:
        # Build content preserving tables as structured text
        content_html_str = str(content_div)
        
        # Find all tables and replace with placeholders
        all_tables = content_div.find_all("table")
        for ti, table in enumerate(all_tables):
            marker = f"\n__TABLE_{ti}__\n"
            content_html_str = content_html_str.replace(str(table), marker, 1)
        
        # Get clean text (non-table parts)
        clean_soup = BeautifulSoup(content_html_str, "html.parser")
        clean_text = body_text(clean_soup)
        
        # Reconstruct: interleave table renderings with surrounding text
        parts = []
        segments = clean_text.split("\n")
        table_idx = 0
        for seg in segments:
            seg = seg.strip()
            if not seg:
                continue
            if seg.startswith("__TABLE_") and seg.endswith("__"):
                ti = table_idx
                table_idx += 1
                if ti < len(all_tables):
                    table = all_tables[ti]
                    rows = table.find_all("tr")
                    if rows:
                        # Check if 2-column key-value table or multi-column data table
                        max_cols = 0
                        for row in rows:
                            cells = row.find_all(["td", "th"])
                            max_cols = max(max_cols, len(cells))
                        
                        if max_cols == 2:
                            # Key-value table: render as 2-column Markdown pipe table
                            table_lines = ['| 项 目 | 内 容 |', '|------|------|']
                            for row in rows:
                                cells = row.find_all(["td", "th"])
                                if len(cells) >= 2:
                                    label = cells[0].get_text(" ", strip=True)
                                    value = cells[1].get_text(" ", strip=True)
                                    if label:
                                        rowspan = int(cells[0].get("rowspan", 1))
                                        if rowspan > 1:
                                            table_lines.append(f"| {label} | {value} |")
                                        else:
                                            table_lines.append(f"| {label} | {value} |")
                                    else:
                                        table_lines.append(f"| | {value} |")
                            if table_lines:
                                parts.append("\n" + "\n".join(table_lines) + "\n")
                        else:
                            # Multi-column data table: render as Markdown table
                            num_cols = max_cols
                            col_widths = [0] * num_cols
                            for row in rows:
                                cells = row.find_all(["td", "th"])
                                for i, cell in enumerate(cells):
                                    cell_text = cell.get_text(" ", strip=True)
                                    col_widths[i] = max(col_widths[i], len(cell_text))
                            col_widths = [max(6, min(w + 2, 40)) for w in col_widths]
                            table_lines = []
                            for ri, row in enumerate(rows):
                                cells = row.find_all(["td", "th"])
                                cell_texts = []
                                for ci in range(num_cols):
                                    txt = cells[ci].get_text(" ", strip=True) if ci < len(cells) else ""
                                    cell_texts.append(txt.ljust(col_widths[ci]))
                                sep = "| "
                                table_lines.append(sep + sep.join(cell_texts) + " |")
                                if ri == 0:
                                    sep_line = []
                                    for w in col_widths:
                                        sep_line.append("-" * w)
                                    table_lines.append("|-" + "-|-".join(sep_line) + "-|")
                            if table_lines:
                                parts.append("\n" + "\n".join(table_lines) + "\n")
            else:
                parts.append(seg)
        
        content_text = "\n".join(parts)

    # Extract attachments
    attachments = []
    for a_tag in content_div.find_all("a", href=True) if content_div else []:
        href = a_tag["href"]
        if re.search(r'\.(pdf|doc|docx|xls|xlsx|rar|zip)$', href, re.IGNORECASE):
            att_title = a_tag.get_text(strip=True) or href.split("/")[-1]
            full_url = urljoin(url, href)
            attachments.append({"name": att_title, "url": full_url})

    return {
        "content": content_text,
        "content_html": content_html,
        "pub_date": pub_date,
        "attachments": json.dumps(attachments, ensure_ascii=False) if attachments else "",
    }


def crawl(test_mode=False, max_pages=MAX_PAGES):
    """Main crawl function."""
    print(f"[{SITE_NAME}] Starting crawl, max_pages={max_pages}", flush=True)
    conn = init_db()
    cur = conn.cursor()

    total_inserted = 0
    total_skipped = 0
    pages_crawled = 0

    for page_num in range(1, max_pages + 1):
        items = extract_list_page(page_num)
        if not items:
            if page_num > 1:
                print(f"  Page {page_num} empty, stopping", flush=True)
                break
            continue

        pages_crawled += 1
        for item in items:
            title = item["title"]
            url = item["url"]
            date = item["date"]

            # Check if already exists
            cur.execute("SELECT id FROM gov_raw WHERE page_url = ?", (url,))
            if cur.fetchone():
                total_skipped += 1
                continue

            # Fetch detail
            detail = extract_detail(url)
            if detail is None:
                total_skipped += 1
                continue

            content = detail["content"]

            # Handle empty content (PDF only pages)
            if len(content.strip()) < 20:
                content = f'<p><a href="{url}">{title}</a></p>\n（本文为PDF附件）'

            attachments = detail["attachments"]

            cur.execute(INSERT_SQL, (
                SITE_NAME, url, title, date or detail["pub_date"],
                content, CATEGORY, attachments,
            ))
            total_inserted += 1

            if test_mode and total_inserted >= 10:
                break

        conn.commit()

        if test_mode and total_inserted >= 10:
            print(f"  Test mode: stopping after {total_inserted} inserts", flush=True)
            break

        # Rate limit
        time.sleep(1)

    conn.close()
    print(f"[{SITE_NAME}] Done. Inserted={total_inserted}, Skipped={total_skipped}, Pages={pages_crawled}", flush=True)
    return total_inserted


if __name__ == "__main__":
    test_mode = "--test" in sys.argv
    mp = 5 if test_mode else MAX_PAGES
    if "--max-pages" in sys.argv:
        idx = sys.argv.index("--max-pages")
        if idx + 1 < len(sys.argv):
            mp = int(sys.argv[idx + 1])
    crawl(test_mode=test_mode, max_pages=mp)
