"""Wait for input and continue the same execution.

Demonstrates two wait patterns:
1. A boolean gate (approve/reject) using ``schema=bool``
2. A structured input wait using a Pydantic schema

Both waits live in the ``@flow`` body. Checkpoints do the restartable work
before and after the human decision, but the wait itself is not inside a
``@checkpoint``.

When running locally, Kitaru prompts for input directly in the terminal.
When running remotely, use the CLI to provide input and resume:

    kitaru executions input <exec_id> --value true
    kitaru executions resume <exec_id>
"""

from pydantic import BaseModel

import kitaru
from kitaru import checkpoint, flow


class ReleaseDetails(BaseModel):
    """Structured input for the release details wait."""

    notes: str
    major_version: int


@checkpoint
def draft_release_note(topic: str) -> str:
    """Create a draft release note for the requested topic."""
    return f"Draft about {topic}."


@checkpoint
def publish_release_note(draft: str, details: ReleaseDetails) -> str:
    """Publish a previously approved draft release note."""
    return f"PUBLISHED v{details.major_version}: {draft}\nNotes: {details.notes}"


@flow
def wait_for_approval_flow(topic: str) -> str:
    """Gate publication behind a durable human-approval wait."""
    draft = draft_release_note(topic)

    exec_id = kitaru.current_execution_id()
    print("\nTo approve remotely, run in another terminal:")
    print(f"  kitaru executions input {exec_id} --value true")
    print(f"  kitaru executions resume {exec_id}")
    print("(Use --value false to reject.)\n")

    approved = kitaru.wait(
        name="approve_release",
        schema=bool,
        question=f"Approve publishing release notes for {topic}?",
        timeout=3600,  # Compute is released after 1 hour; resume via CLI later
        metadata={"topic": topic},
    )

    if approved is False:
        return f"REJECTED: {topic}"

    print("\nProvide release details remotely:")
    example_value = '\'{"notes": "Bug fixes", "major_version": 2}\''
    print(f"  kitaru executions input {exec_id} --value {example_value}")
    print(f"  kitaru executions resume {exec_id}\n")

    details = kitaru.wait(
        name="release_details",
        schema=ReleaseDetails,
        question="Provide the release notes and major version number:",
        timeout=60,  # Compute is released after 1 minute; resume via CLI later
        metadata={"topic": topic},
    )

    return publish_release_note(draft, details)


def run_workflow(topic: str = "v1.0") -> str:
    """Execute the wait/resume workflow and return its output."""
    return wait_for_approval_flow.run(topic).wait()


def main() -> None:
    """Run the example as a script."""
    result = run_workflow()
    print(f"\nResult: {result}")


if __name__ == "__main__":
    main()
