from __future__ import annotations

from govcrawler.adapters import openstd_samr


class _Resp:
    status_code = 200

    def __init__(self, text: str):
        self.text = text

    def raise_for_status(self):
        pass


LIST_HTML = """
<html><body>
<script>var pageSize = 10; var count = 610;</script>
<table>
  <tr>
    <td>1</td>
    <td style="text-align: left;"><a href="javascript:void(0)"
      onclick="showInfo('8DFBEDE88D831247BF30901B1B42E8D0');">GB 47372-2026</a></td>
    <td></td>
    <td class="mytxt"><a href="javascript:void(0)" style="color: gray;"
      onclick="showInfo('8DFBEDE88D831247BF30901B1B42E8D0');">移动电源安全技术规范</a></td>
    <td><span class="text-warning">即将实施</span></td>
    <td>2026-03-31 00:00:00.0</td>
    <td>2027-04-01 00:00:00.0</td>
    <td><button onclick="showInfo('8DFBEDE88D831247BF30901B1B42E8D0');">查看详细</button></td>
  </tr>
</table>
</body></html>
"""


DETAIL_HTML = """
<html><body>
<h1>标准号：GB 47372-2026</h1>
<table>
  <tr><td>中文标准名称：<b>移动电源安全技术规范</b></td></tr>
  <tr><td>英文标准名称：Safety technical specification for power bank</td></tr>
  <tr><td>标准状态：<span class="text-warning">即将实施</span></td></tr>
</table>
<div class="row detail">
  <div class="col-xs-12 col-md-2 title">中国标准分类号（CCS）</div>
  <div class="col-xs-12 col-md-4 content">L 09</div>
  <div class="col-xs-12 col-md-2 title">国际标准分类号（ICS）</div>
  <div class="col-xs-12 col-md-4 content">35.180</div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-2 title">发布日期</div>
  <div class="col-xs-12 col-md-4 content">2026-03-31</div>
  <div class="col-xs-12 col-md-2 title">实施日期</div>
  <div class="col-xs-12 col-md-4 content">2027-04-01</div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-2 title">主管部门</div>
  <div class="col-xs-12 col-md-4 content">工业和信息化部</div>
  <div class="col-xs-12 col-md-2 title">归口部门</div>
  <div class="col-xs-12 col-md-4 content">工业和信息化部</div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-2 title">发布单位</div>
  <div class="col-xs-12 col-md-10 content">国家市场监督管理总局、国家标准化管理委员会</div>
</div>
</body></html>
"""


def test_fetch_list_page_maps_mandatory_standard(monkeypatch):
    seen = {}

    def _fake_get(url, **_kw):
        seen["url"] = url
        return _Resp(LIST_HTML)

    monkeypatch.setattr(openstd_samr.httpx, "get", _fake_get)

    list_url, items, fr = openstd_samr.fetch_list_page(
        None,
        page_num=2,
        params={"std_type": "1", "std_type_label": "强制性国家标准", "page_size": 10},
    )

    assert fr.status == 200
    assert "page=2" in seen["url"]
    assert list_url == seen["url"]
    assert len(items) == 1
    item = items[0]
    assert item.native_post_id == "8DFBEDE88D831247BF30901B1B42E8D0"
    assert item.doc_no == "GB 47372-2026"
    assert item.title == "移动电源安全技术规范"
    assert item.publish_date.isoformat() == "2026-03-31"
    assert item.effective_date.isoformat() == "2027-04-01"
    assert item.is_effective is False
    assert item.content_category == "国家标准"
    assert item.content_subcategory == "强制性国家标准"


def test_fetch_detail_extracts_metadata_and_no_attachments(monkeypatch):
    def _fake_get(url, **_kw):
        return _Resp(DETAIL_HTML)

    monkeypatch.setattr(openstd_samr.httpx, "get", _fake_get)

    fr, fields = openstd_samr.fetch_detail(
        None,
        url="https://openstd.samr.gov.cn/bzgk/std/newGbInfo?hcno=8DFBEDE88D831247BF30901B1B42E8D0",
    )

    assert fr.status == 200
    assert fields.title == "移动电源安全技术规范"
    assert fields.doc_no == "GB 47372-2026"
    assert fields.publisher == "国家市场监督管理总局、国家标准化管理委员会"
    assert fields.publish_date.isoformat() == "2026-03-31"
    assert fields.effective_date.isoformat() == "2027-04-01"
    assert fields.is_effective is False
    assert fields.attachment_urls == []
    assert "主管部门：工业和信息化部" in fields.content_text
    assert fields.public_meta["fulltext_download_note"]
