from __future__ import annotations

from io import BytesIO

from openpyxl import Workbook

from lawrisk.services import licensing_repo


def _build_workbook() -> bytes:
    wb = Workbook()
    ws = wb.active
    ws.title = "市级"
    ws.append(["许可事项", "主题", "风险提示"])
    ws.append([
        "成品油、危险化学品经营许可",
        "市场监管、应急管理",
        "测试风险",
    ])
    buf = BytesIO()
    wb.save(buf)
    return buf.getvalue()


def test_split_multi_value_does_not_use_dunhao_by_default() -> None:
    assert licensing_repo._split_multi_value("甲、乙") == ["甲、乙"]
    assert licensing_repo._split_multi_value("甲、乙", allow_dunhao=True) == ["甲", "乙"]


def test_parse_workbook_keeps_permit_name_with_dunhao() -> None:
    file_bytes = _build_workbook()
    parsed = licensing_repo._parse_import_workbook(file_bytes, "sample.xlsx")
    sheet = parsed["sheets"]["市级"]
    rows = sheet["rows"]
    assert len(rows) == 1
    row = rows[0]
    assert row["permit_name"] == "成品油、危险化学品经营许可"
    # Themes still split because allow_dunhao=True for that field
    assert row["theme_names"] == ["市场监管", "应急管理"]


def test_normalize_theme_binding_value_maps_all_theme_marker() -> None:
    assert licensing_repo._normalize_theme_binding_value("__ALL_THEMES__") == licensing_repo.ALL_THEMES_SENTINEL
    assert licensing_repo._normalize_theme_binding_value("所有主题") == licensing_repo.ALL_THEMES_SENTINEL


def test_normalize_permit_token_lowercases_ascii_names() -> None:
    assert licensing_repo._normalize_permit_token(" PermitX ") == "permitx"


def test_normalize_sheet_token_canonicalizes_alias_names() -> None:
    assert licensing_repo._normalize_sheet_token("禅城区（无意见）") == "禅城区"


def test_permit_name_aliases_cover_canonical_and_token() -> None:
    aliases = licensing_repo._permit_name_aliases(" 许可A ")
    assert "许可A" in aliases
    assert "许可a" in aliases
