"""問題レコードの作成ヘルパー。必須項目の欠落を防ぐ。"""
from __future__ import annotations

FIELDS = ["計算", "数の性質", "規則性", "場合の数", "割合と比", "文章題", "速さ", "平面図形", "立体図形", "図形の移動", "水そう・グラフ", "論理・推理", "統計"]
BARRIERS = ["発想", "読解", "条件整理", "場合分け", "計算", "作図"]
DIFFICULTY_SCALE = {
    "A": "典型問題の知識をそのまま使える (1〜2段階)",
    "B": "典型解法を組み合わせる・条件整理が必要 (3〜4段階)",
    "C": "発想の転換か丁寧な場合分けが必要 (独自の着眼が1つ)",
    "D": "複数の着眼を要し、時間内に完答しにくい",
}

def rec(pid, *, transcript, answer, first_insight, insight_basis, steps, why, check, common_errors, minimal_points, hints,
        primary_field, cores, prerequisites, difficulty, est_minutes, difficulty_basis="", secondary_fields=(), barrier=(),
        insight_triggers="", core_explanation="", approach_comparison="", background="", alternatives="", intent_estimate="",
        confidence="中", related=(), depends_on=(), lecture_notes="", teaching_notes_refs=(), transcript_status="agent_visual_confirmed",
        transcript_notes="", error_types=(), prereq_weight=0, verification_methods=(), notes="", unit=None, defective=False):
    assert primary_field in FIELDS, (pid, primary_field)
    for b in barrier: assert b in BARRIERS, (pid, b)
    assert difficulty in DIFFICULTY_SCALE, (pid, difficulty)
    return {
        "problem_id": pid,
        "transcript": {"text": transcript, "status": transcript_status, "notes": transcript_notes or None},
        "depends_on": list(depends_on),
        "solution": {"final_answer": answer, "unit": unit, "first_insight": first_insight, "insight_basis": insight_basis,
                     "steps": list(steps), "why": why, "check": check,
                     "common_errors": [e if isinstance(e, dict) else {"error": e} for e in common_errors],
                     "minimal_points": list(minimal_points), "related": [r if isinstance(r, dict) else {"problem_id": r} for r in related]},
        "hints": list(hints),
        "analysis": {"primary_field": primary_field, "secondary_fields": list(secondary_fields), "cores": list(cores),
                     "prerequisites": list(prerequisites), "insight_triggers": insight_triggers, "core_explanation": core_explanation,
                     "barrier": "・".join(barrier), "approach_comparison": approach_comparison},
        "learning": {"difficulty": difficulty, "difficulty_basis": difficulty_basis, "difficulty_scale": DIFFICULTY_SCALE[difficulty],
                     "est_minutes": est_minutes, "est_basis": "推定 (分析者の暫定値。実測値は学習履歴で別管理)",
                     "error_types": list(error_types), "prereq_weight": prereq_weight},
        "teacher": {"background": background, "alternatives": alternatives, "intent_estimate": intent_estimate, "confidence": confidence},
        "lecture_notes": lecture_notes or None,
        "teaching_notes_refs": list(teaching_notes_refs),
        "verification_methods_declared": list(verification_methods),
        "notes": notes or None,
        "defective": defective,
    }
