from .models import Conversation


def create_conversation_for_batch(batch_id: str, product_name: str) -> Conversation:
    """
    为资料包创建主会话。

    会话标题固定优先使用解析出的产品名称，
    缺失时回退到批次号，确保前台始终有稳定标题。
    """
    conversation = Conversation.objects.create(
        conversation_id=_generate_conversation_id(),
        title=product_name or f"未命名资料包-{batch_id}",
        product_name=product_name,
        batch_id=batch_id,
        task_status=Conversation.STATUS_PENDING,
        node_results=_build_initial_node_results(),
    )
    return conversation


def _generate_conversation_id() -> str:
    return f"conv-{Conversation.objects.count() + 1:03d}"


def _build_initial_node_results() -> list[dict]:
    return [
        {"code": "package_import", "label": "资料包导入", "status": "已完成"},
        {"code": "overview", "label": "目录汇总", "status": "处理中"},
        {"code": "completeness", "label": "法规完整性检查", "status": "待处理"},
        {"code": "field_extraction", "label": "字段抽取", "status": "待处理"},
        {"code": "consistency", "label": "一致性核查", "status": "待处理"},
        {"code": "risk", "label": "风险预警", "status": "待处理"},
        {"code": "word_export", "label": "Word 回填导出", "status": "待处理"},
        {"code": "feishu_notify", "label": "飞书通知", "status": "待处理"},
    ]
