from __future__ import annotations

from datetime import date

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from govcrawler.models import Article, ArticleStandardMeta, Base
from govcrawler.storage.repo import upsert_article_standard_meta

from tests._v2fixtures import make_site, make_target


def test_upsert_article_standard_meta_extracts_openstd_public_meta(tmp_path):
    engine = create_engine("sqlite:///" + str(tmp_path / "std.db"), future=True)
    Base.metadata.create_all(engine)
    SM = sessionmaker(bind=engine, expire_on_commit=False)

    with SM() as s:
        site = make_site(
            s,
            site_code="openstd_samr",
            cms_adapter="openstd_samr",
            yaml_path=None,
        )
        target = make_target(
            s,
            site=site,
            column_id="mandatory_national_standards",
        )
        article = Article(
            site_id=site.id,
            target_id=target.id,
            url="https://openstd.samr.gov.cn/bzgk/std/newGbInfo?hcno=ABC",
            url_hash="c" * 64,
            title="室内装饰装修材料 地毯、地毯衬垫和聚氯乙烯地板中有害物质限量",
            status="ready",
            content_text="标准元数据正文",
            has_attachment=False,
            doc_no="GB 18586-2026",
            publisher="国家市场监督管理总局、国家标准化管理委员会",
            publish_date=date(2026, 3, 31),
            effective_date=date(2027, 10, 1),
            open_category="即将实施",
            content_category="国家标准",
            content_subcategory="强制性国家标准",
            topic_words="59.080.60;83.140",
            metadata_json={
                "public_meta": {
                    "std_no": "GB 18586-2026",
                    "title": "室内装饰装修材料 地毯、地毯衬垫和聚氯乙烯地板中有害物质限量",
                    "english_title": (
                        "Indoor decorating and refurbishing materials-"
                        "Limitations of harmful substances"
                    ),
                    "standard_status": "即将实施",
                    "ccs": "W 56;Y 28",
                    "ics": "59.080.60;83.140",
                    "publish_date": "2026-03-31",
                    "effective_date": "2027-10-01",
                    "主管部门": "工业和信息化部",
                    "归口部门": "工业和信息化部",
                    "发布单位": "国家市场监督管理总局、国家标准化管理委员会",
                    "openstd_hcno": "ABC",
                }
            },
        )
        s.add(article)
        s.flush()

        row = upsert_article_standard_meta(s, article)
        s.commit()

    with SM() as s:
        rows = s.query(ArticleStandardMeta).all()
        assert len(rows) == 1
        row = rows[0]
        assert row.article_id == article.id
        assert row.standard_no == "GB 18586-2026"
        assert row.standard_status == "即将实施"
        assert row.ccs_codes == ["W 56", "Y 28"]
        assert row.ics_codes == ["59.080.60", "83.140"]
        assert row.implementation_date == date(2027, 10, 1)
        assert row.competent_department == "工业和信息化部"
        assert row.technical_committee == "工业和信息化部"
        assert row.issuing_body == "国家市场监督管理总局、国家标准化管理委员会"
