# -*- coding: utf-8 -*-
"""
网页抓取工具
直接获取网页文本内容，适用于国内可直接访问的网站（如百度新闻等）
"""
import re
from .base import Tool, coerce_int


class WebFetchTool(Tool):
    """抓取网页内容，返回纯文本。适用于国内可直接访问的网站。"""

    @property
    def name(self) -> str:
        return "web_fetch"

    @property
    def description(self) -> str:
        return (
            "直接获取网页的文本内容。适用于访问国内网站（百度、知乎、新闻门户等）。"
            "输入 URL（http/https），返回页面文本。"
        )

    @property
    def parameters(self) -> dict:
        return {
            "type": "object",
            "properties": {
                "url": {
                    "type": "string",
                    "description": "要抓取的网页 URL（http/https）"
                },
                "max_chars": {
                    "type": "integer",
                    "description": "返回文本最大字符数，默认 8000"
                }
            },
            "required": ["url"]
        }

    def execute(self, url: str, max_chars: int = 8000) -> dict:
        """抓取网页并返回纯文本内容"""
        # 兼容 LLM 将 max_chars 序列化为字符串的情形
        max_chars = coerce_int(max_chars, default=8000)

        try:
            import httpx
        except ImportError:
            return {"success": False, "error": "请安装 httpx: pip install httpx"}

        try:
            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",
                "Accept-Language": "zh-CN,zh;q=0.9",
            }

            with httpx.Client(timeout=15, follow_redirects=True, headers=headers) as client:
                response = client.get(url)
                response.raise_for_status()
                html = response.text
                text = self._html_to_text(html)

                # 截断过长内容
                if len(text) > max_chars:
                    text = text[:max_chars] + f"\n...[截断，原文共 {len(text)} 字符]"

                return {
                    "success": True,
                    "url": str(response.url),
                    "status_code": response.status_code,
                    "content": text
                }
        except Exception as e:
            return {"success": False, "error": str(e)}

    def _html_to_text(self, html: str) -> str:
        """将 HTML 转为可读的纯文本"""
        # 移除 script/style 标签及其内容
        html = re.sub(r'<script[^>]*>.*?</script>', '', html, flags=re.DOTALL | re.IGNORECASE)
        html = re.sub(r'<style[^>]*>.*?</style>', '', html, flags=re.DOTALL | re.IGNORECASE)

        # 将块级元素替换为换行
        html = re.sub(r'<br\s*/?>', '\n', html, flags=re.IGNORECASE)
        html = re.sub(r'</?(?:div|p|h[1-6]|li|tr|article|section|header|footer)[^>]*>', '\n', html, flags=re.IGNORECASE)

        # 移除所有其他 HTML 标签
        html = re.sub(r'<[^>]+>', '', html)

        # 解码 HTML 实体
        html = html.replace('&nbsp;', ' ').replace('&lt;', '<').replace('&gt;', '>')
        html = html.replace('&amp;', '&').replace('&quot;', '"').replace('&#39;', "'")

        # 合并连续空白
        html = re.sub(r'[ \t]+', ' ', html)
        html = re.sub(r'\n{3,}', '\n\n', html)

        return html.strip()
