from features.trigger import IDLE, MINUTES, Trigger
from features.ask_questions.interceptors import FILED, UNPICKED
from features.base import Behaviour, FeatureDetails, Line
from features.settings import Setting
from features.groups import Group


class AskQuestionsDetails(FeatureDetails):
    explains = 'The agent can ask you a question with clear choices. You can answer in the viewer or on your phone.'
    name = "ask_questions"
    group = Group.QUESTIONS
    when = "a decision only the user can make comes up, or before offering the user choices"

    title = "Ask choices as questions"

    speaks_while_waiting = True

    abstract = """
        A choice only you can make is asked as a question with options, not written into a reply. A
        picked answer can be undone for a few seconds.
    """

    help = """
        A question or a suggestion is answered by clicking a choice; the choice is held for a
        moment before it is saved, and clicking it again in that moment takes it back.
        questions.hold sets the moment in seconds, three by default. The card marks the answer
        as the user's; when you answer a question yourself, journal question answer <n>
        --how "<choice>" --set reason="<why>" is required, and the card shows your answer with
        that reason beneath it. Elaborate on an open question sends you a message about it:
        delete that question with journal question delete <n> --why "<asked again>" and ask it
        again, with more context in each option's text and which you would pick in its brief,
        and leave the choice to the user. A question the user dismisses is closed as no longer
        needed: do not act on it and do not ask it again. A row waits on one open question at a
        time; asking a second one about it is refused and names the one it waits on.
        A question about a document or report (--set about=doc:<n>) shows in the chat and inside
        that document as one question, and an answer in either place answers both; a row that
        already asks through its own choice buttons is not asked again as a question. Ask each
        question as a command of its own, never chained or piped, and never one still open.

        A message with two or more listed options and a question, or the language of putting a
        decision to the user, tells you to use journal question ask --set options=…; your
        writes wait until a question is created. A line naming a question by number points at
        one already asked and does not count.

        Each option carries its own title and text; the question's title, abstract and brief
        give the context only. A question that lists its options again, as A/B/C or numbered
        lines or one line per option name, is refused. Every question with options names the one
        you would pick, --set pick=<its number>, which the card marks as the agent's pick; one
        without it is refused.

        A question tool the provider offers, such as Claude Code's AskUserQuestion, never opens
        in the terminal: each question in the call is filed as a journal question with its
        options and the one labelled (Recommended) as your pick, and the call is refused with the
        numbers, so you carry on and hear the answer as an event.

        A question is open, without options, only for a name, a secret or a free description,
        and says so with --set free=name|secret|description. A yes or no, a which, or
        alternatives joined by or carry their options and your pick, journal todo ask included;
        one an earlier answer already settles is yours to decide. A status you could check or
        wait on is no question: wait on the row and say so in the chat.
    """

    fixed = True

    aliases = (("choices", "asking"), "questions")

    settings = [
        Setting(
            name="hold",
            default=3,
            title="Time to undo a picked answer",
            unit="seconds",
        ),
    ]

    behaviours = [
        Behaviour(
            name="asking",
            title="Require choices to be asked as questions before file changes",
            trigger=Trigger(on=IDLE),
        ),
        Behaviour(
            name="settled",
            title="Ask whether a question open for a day is still needed",
            trigger=Trigger(every=1440, unit=MINUTES),
        ),
    ]

    lines = [
        Line(
            name="answered",
            title="question {{n}} is answered - act on the answer",
            brief="""
                the answer: {{answer}}. Carry it out now, or file what it asks for later with journal todo
                create
            """,
            until=("work.created", "work.updated", "work.completed", "todo.created", "question.updated", "question.deleted"),
            while_waiting=True,
        ),
        Line(
            name="settled",
            title="question {{n}}, {{title}}, has waited a day for an answer",
            brief="""
                if the project or the code has settled it since, dismiss it with journal question dismiss {{n}} --why
                "<what settled it>"; if it still needs the user, leave it as it is.
            """,
        ),
        Line(
            name=UNPICKED,
            title="question {{question}} names no pick",
            brief="label the option you would pick (Recommended) and ask again; the card marks it as the agent's pick",
        ),
        Line(
            name=FILED,
            title="asked in the journal as question {{numbers}}",
            brief="the user answers it in the viewer and the answer reaches you as an event; carry on with what does not depend on it",
        ),
        Line(
            name="prose",
            title="your last message offers choices in prose",
            brief="""
                ask through journal question ask "<one line>" --set options='[{"title": …,
                "description": …}]' --set pick=<n>, so the viewer renders it; your writes wait
                until you do
            """,
        ),
        Line(
            name="prose held",
            title="""
                your last message offered the user choices in prose: ask them through journal
                question ask --set options=… --set pick=<n> before any other write
            """,
        ),
    ]
