from __future__ import annotations

import json

from core.models import Post
from decode_content.payloads import build_payload, build_payloads


def _post() -> Post:
    return Post(
        id="xhs_1",
        platform="xiaohongshu",
        url="https://xhs/1",
        content_id="1",
        title="脚本教程",
        author_name="作者",
    )


def test_build_how_payload_uses_formal_dim_attribute_and_scope_union():
    knowledge = {
        "id": "k1",
        "type": "how",
        "title": "先定受众再写脚本",
        "purpose": "写出稳定脚本",
        "业务阶段": ["脚本"],
        "steps": [
            {
                "input": "选题",
                "directive": "收窄受众",
                "output": "受众画像",
                "创作阶段": "定向",
                "动作": "收窄",
                "作用域": [
                    {"scope_type": "intent", "value": "吸引注意", "link": "复用"},
                    {"scope_type": "intent", "value": "吸引注意", "link": "复用"},
                ],
            }
        ],
    }

    payload = build_payload(_post(), knowledge)

    assert payload["dim_attributes"] == ["how工序"]
    assert "输入：选题" in payload["content"]
    assert "指引：收窄受众" in payload["content"]
    assert payload["scopes"] == [{"scope_type": "intent", "value": "吸引注意"}]
    assert {"key": "创作阶段", "type": "str", "value": "定向"} in payload["custom_ext"]


def test_build_what_and_why_payload_content_is_string_contract():
    what = {
        "id": "w1",
        "type": "what",
        "title": "脚本三要素",
        "kind": "子集",
        "概要": "脚本由开头中段结尾构成",
        "维度拆分规则": "按表达顺序",
        "body": [{"item_name": "开头", "item_desc": "抓注意"}],
        "业务阶段": ["脚本"],
        "作用域": [{"scope_type": "form", "value": "脚本结构", "top": []}],
    }
    why = {
        "id": "y1",
        "type": "why",
        "title": "冲突带来停留",
        "阐述": "冲突能制造期待，所以提升停留。",
        "业务阶段": ["选题"],
        "作用域": [{"scope_type": "effect", "value": "提升停留", "score": 0.95}],
    }

    what_payload, why_payload = build_payloads(_post(), [what, why])

    assert what_payload["dim_attributes"] == ["what构成"]
    assert json.loads(what_payload["content"])["body"][0]["item_name"] == "开头"
    assert {"key": "概要", "type": "str", "value": "脚本由开头中段结尾构成"} in what_payload["custom_ext"]
    assert why_payload["dim_attributes"] == ["why原理"]
    assert why_payload["content"] == "冲突能制造期待，所以提升停留。"
    assert "score" not in why_payload["scopes"][0]
