"""Pillar-focused live agent battery cases (docs/kernel_spec + product pillars).

Imported by ``kernel_live_agent_battery.py``. Each case targets one of:
  durable  — kanban / multi-step work that must survive hours–days
  user     — User Model, sessions recall, personal memory
  knowledge — studied corpora / expert Q&A
  vault    — Pattern Inbox / long-term notes
"""
from __future__ import annotations

from scripts.kernel_live_agent_types import Case

# Unique marker for vault create/search probes (idempotent duplicate handling).
_VAULT_PROBE_MARKER = "PILLAR_BATTERY_VAULT_PROBE_2026"
_VAULT_PROBE_TITLE = "Pillar battery vault probe"


PILLAR_CASES: list[Case] = [
    # ═══ Pillar 1: Durable long-term work (kanban planner tier) ═══
    Case(
        id="durable_fanout_fanin_assembly",
        pillar="durable",
        prompt=(
            "Durable work test: kanban_create_batch for a 3-day research project. "
            "One ready root assignee general, two research children (parent_index 0) "
            "titled 'Durable child A' and 'Durable child B', plus an assembly sibling "
            "parent_indices [1,2] assignee general priority 2 with metadata.acceptance "
            "listing merge criteria. Reply with root task id only."
        ),
        required_tools=("kanban_create_batch",),
        min_tool_calls=1,
        timeout_sec=150.0,
    ),
    Case(
        id="durable_show_task_and_runs",
        pillar="durable",
        prompt=(
            "Call kanban_list_tasks limit 3. Pick the first task id, then kanban_show_task "
            "and kanban_list_runs on that id. Summarize status + run count."
        ),
        required_tools=("kanban_list_tasks", "kanban_show_task", "kanban_list_runs"),
        min_tool_calls=3,
        timeout_sec=120.0,
    ),
    Case(
        id="durable_research_assignee_metadata",
        pillar="durable",
        prompt=(
            "kanban_create_task: title 'Durable research probe', body 'Gather sources on "
            "local LLM coding benchmarks', assignee research, priority 4, "
            "metadata acceptance criterion 'at least 3 cited URLs'. Reply task id."
        ),
        any_of_tools=("kanban_create_task", "kanban_create_batch"),
        min_tool_calls=1,
    ),
    Case(
        id="durable_no_plan_execute",
        pillar="durable",
        prompt=(
            "User wants a multi-day codebase audit on the kanban board. "
            "Do NOT reply with only a plan — call kanban_create_batch this turn with "
            "one ready root and at least two child tasks (research + coding profiles). "
            "List created ids."
        ),
        required_tools=("kanban_create_batch",),
        min_tool_calls=1,
        timeout_sec=180.0,
    ),
    Case(
        id="durable_batch_then_show",
        pillar="durable",
        prompt="kanban_create_task title 'Durable show probe' assignee general body 'x'. Reply id.",
        follow_up_prompts=(
            "kanban_show_task on the id you just created. Reply status field only.",
        ),
        follow_up_required_tools=(("kanban_show_task",),),
        follow_up_min_tools=(1,),
        any_of_tools=("kanban_create_task", "kanban_create_batch"),
        min_tool_calls=2,
        timeout_sec=120.0,
    ),
    Case(
        id="durable_invalid_then_valid_assignee",
        pillar="durable",
        prompt=(
            "kanban_create_batch one item assignee 'callie'. On rejection, retry assignee "
            "'research' with title 'Durable recovery probe'. Report final id."
        ),
        required_tools=("kanban_create_batch",),
        require_tool_error_substrings=("unknown assignee profile",),
        min_tool_calls=1,
    ),
    Case(
        id="durable_list_filter_ready",
        pillar="durable",
        prompt=(
            "kanban_list_tasks with status filter ready (or list all if filter unsupported). "
            "Reply READY_COUNT=N."
        ),
        required_tools=("kanban_list_tasks",),
        min_tool_calls=1,
    ),
    Case(
        id="durable_find_tools_kanban_worker",
        pillar="durable",
        prompt=(
            "find_tools query 'kanban show list runs' then call kanban_list_tasks. "
            "Do not spawn workers — planner tier only."
        ),
        required_tools=("find_tools", "kanban_list_tasks"),
        min_tool_calls=2,
    ),
    # ═══ Pillar 2: Knows you / personal history ═══
    Case(
        id="user_find_tools_user_model",
        pillar="user",
        prompt=(
            "find_tools query 'user model patch' then list_pending_proposals if available. "
            "Reply PENDING or NONE."
        ),
        required_tools=("find_tools",),
        any_of_tools=("list_pending_proposals", "patch_user_model", "propose_user_model_update"),
        min_tool_calls=2,
        timeout_sec=120.0,
    ),
    Case(
        id="user_search_sessions_recall",
        pillar="user",
        prompt=(
            "find_tools query 'search sessions past conversations' then search_sessions "
            "with query 'kanban' max_results 2. Reply with count of sessions found."
        ),
        required_tools=("find_tools", "search_sessions"),
        min_tool_calls=2,
        timeout_sec=150.0,
    ),
    Case(
        id="user_search_memory_personal",
        pillar="user",
        prompt=(
            "search_memory query 'Josh preferences goals'. Reply with whether any personal "
            "context was found (FOUND/EMPTY)."
        ),
        required_tools=("search_memory",),
        min_tool_calls=1,
    ),
    Case(
        id="user_patch_stated_fact",
        pillar="user",
        prompt=(
            "I explicitly state: my preferred checklist style is concise bullets. "
            "Record this with patch_user_model (identity or preferences section). "
            "Reply PATCH_OK or tool error."
        ),
        required_tools=("patch_user_model",),
        min_tool_calls=1,
        timeout_sec=120.0,
    ),
    Case(
        id="user_propose_not_silent_append",
        pillar="user",
        prompt=(
            "You noticed I might prefer morning deep-work blocks (hypothesis only). "
            "Use propose_user_model_update with changes "
            '{"preferences": {"scheduling": "prefers morning deep-work blocks"}} '
            "and a short rationale — do NOT patch_user_model or append_to_memory. "
            "Reply PROPOSED or error."
        ),
        required_tools=("propose_user_model_update",),
        min_tool_calls=1,
        timeout_sec=120.0,
    ),
    Case(
        id="user_recall_across_turns",
        pillar="user",
        prompt=(
            "search_memory query 'checklist style'. Reply FOUND or EMPTY only."
        ),
        follow_up_prompts=(
            "Without calling tools: one sentence — do you have any stored preference about "
            "how I like checklists formatted?",
        ),
        follow_up_allow_zero_tools=(True,),
        required_tools=("search_memory",),
        min_tool_calls=1,
        timeout_sec=90.0,
        notes="Turn 1 seeds recall; turn 2 checks conversational use of memory.",
    ),
    Case(
        id="user_sessions_plus_memory",
        pillar="user",
        prompt=(
            "Call search_sessions query 'video promo' max_results 2 AND search_memory "
            "query 'Mugi'. Reply SESSIONS=N MEMORY=FOUND|EMPTY."
        ),
        any_of_tools=("search_sessions", "search_memory"),
        required_tools=("search_memory",),
        min_tool_calls=2,
        timeout_sec=150.0,
    ),
    # ═══ Pillar 3: Knowledge Base / corpora expertise ═══
    Case(
        id="kb_find_tools_discover",
        pillar="knowledge",
        prompt=(
            "find_tools query 'knowledge corpus query studied'. Then call query_knowledge "
            "with query 'overview' max_results 3. Reply HITS=N or NO_CORPUS."
        ),
        required_tools=("find_tools",),
        any_of_tools=("query_knowledge", "ask_studied_corpus", "list_knowledge_corpora"),
        min_tool_calls=2,
        timeout_sec=150.0,
    ),
    Case(
        id="kb_ask_studied_corpus",
        pillar="knowledge",
        prompt=(
            "find_tools query 'ask studied corpus' then ask_studied_corpus with question "
            "'What topics are covered?' max_sources 5. Reply ANSWERED or NO_CORPUS."
        ),
        required_tools=("find_tools",),
        any_of_tools=("ask_studied_corpus", "query_knowledge"),
        min_tool_calls=2,
        timeout_sec=180.0,
    ),
    Case(
        id="kb_query_specific_corpus",
        pillar="knowledge",
        prompt=(
            "find_tools query 'knowledge'. Call query_knowledge query 'architecture' "
            "max_results 5. If no corpora exist, say NO_CORPUS explicitly."
        ),
        required_tools=("find_tools", "query_knowledge"),
        min_tool_calls=2,
        timeout_sec=150.0,
    ),
    Case(
        id="kb_web_fallback_when_empty",
        pillar="knowledge",
        prompt=(
            "Try query_knowledge query 'zzzznonexistent_topic_zzzz' max_results 3. "
            "If no hits, call search_web query 'Mugi AI' max_results 1. "
            "Must call at least query_knowledge first."
        ),
        required_tools=("query_knowledge",),
        any_of_tools=("search_web",),
        min_tool_calls=1,
        timeout_sec=180.0,
    ),
    Case(
        id="kb_no_invent_citations",
        pillar="knowledge",
        prompt=(
            "ask_studied_corpus question 'What is the capital of France?' — if no corpus "
            "evidence, say insufficient evidence; do not invent [Corpus > source] citations."
        ),
        any_of_tools=("ask_studied_corpus", "query_knowledge", "find_tools"),
        min_tool_calls=1,
        forbid_assistant_patterns=("[Corpus >", "[corpus >"),
        timeout_sec=150.0,
    ),
    # ═══ Pillar 4: Vault / long-term notes ═══
    Case(
        id="vault_find_tools_discover",
        pillar="vault",
        prompt=(
            "find_tools query 'vault note pattern inbox' then list_recent_vault_notes "
            "limit 5. Reply COUNT=N."
        ),
        required_tools=("find_tools", "list_recent_vault_notes"),
        min_tool_calls=2,
        timeout_sec=120.0,
    ),
    Case(
        id="vault_create_and_search",
        pillar="vault",
        prompt=(
            f"create_vault_note title '{_VAULT_PROBE_TITLE}' body "
            f"'{_VAULT_PROBE_MARKER}: pillar battery note.' "
            f"Then search_vault_notes query '{_VAULT_PROBE_MARKER}'. Reply FOUND or id=."
        ),
        required_tools=("create_vault_note", "search_vault_notes"),
        min_tool_calls=2,
        timeout_sec=120.0,
    ),
    Case(
        id="vault_get_note_after_create",
        pillar="vault",
        prompt=(
            f"create_vault_note title 'Pillar get probe' body '{_VAULT_PROBE_MARKER}_GET'. "
            f"Then get_note on that id. Reply first 40 chars of body."
        ),
        required_tools=("create_vault_note", "get_note"),
        min_tool_calls=2,
        timeout_sec=120.0,
    ),
    Case(
        id="vault_append_multi_turn",
        pillar="vault",
        prompt=(
            f"create_vault_note title 'Pillar append probe' body 'Line A {_VAULT_PROBE_MARKER}_APP'. "
            f"Reply note id only."
        ),
        follow_up_prompts=(
            f"append_to_vault_note on that note id text 'Line B appended'. Then get_note. "
            f"Reply APPENDED if both lines present.",
        ),
        required_tools=("create_vault_note",),
        follow_up_required_tools=(("append_to_vault_note", "get_note"),),
        follow_up_min_tools=(2,),
        min_tool_calls=3,
        timeout_sec=150.0,
    ),
    Case(
        id="vault_capture_to_inbox",
        pillar="vault",
        prompt=(
            f"find_tools query 'capture inbox' then capture_to_inbox text "
            f"'{_VAULT_PROBE_MARKER}_INBOX quick capture'. Reply CAPTURED or error."
        ),
        required_tools=("find_tools",),
        any_of_tools=("capture_to_inbox", "create_vault_note"),
        min_tool_calls=2,
        timeout_sec=120.0,
    ),
    Case(
        id="vault_discuss_context_read",
        pillar="vault",
        prompt=(
            f"search_vault_notes query '{_VAULT_PROBE_MARKER}' limit 5. "
            f"If found, get_note on first id and summarize in one sentence."
        ),
        required_tools=("search_vault_notes",),
        any_of_tools=("get_note",),
        min_tool_calls=1,
        timeout_sec=120.0,
    ),
]
