#!/usr/bin/env python3
"""Create a custom hello workflow with the StackGen SDK (coding-style).

Usage:
  python examples/create-workflow/create-hello-workflow.py \\
    --base-url https://app.stackgen.com \\
    --api-token stackgen_… \\
    --workspace-id <workspace-uuid>
"""

from __future__ import annotations

import argparse
import os
import sys

from stackgen import (
    StackgenClient,
    StackgenConfig,
    create_agent,
    create_stage,
    create_workflow,
)


def main() -> int:
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument("--base-url", default=os.environ.get("STACKGEN_URL", ""))
    p.add_argument("--api-token", default=os.environ.get("STACKGEN_TOKEN", ""))
    p.add_argument("--workspace-id",
        "--org-id", default=os.environ.get("STACKGEN_WORKSPACE_ID", "") or os.environ.get("STACKGEN_PROJECT", ""))
    p.add_argument("--agent-name", default="hello-agent")
    p.add_argument("--workflow-name", default="hello-workflow")
    args = p.parse_args()
    if not args.base_url or not args.api_token:
        print("Need --base-url and --api-token (or STACKGEN_URL / STACKGEN_TOKEN)", file=sys.stderr)
        return 1

    client = StackgenClient(
        StackgenConfig(
            base_url=args.base_url,
            api_token=args.api_token,
            workspace_id=args.workspace_id,
        )
    )

    hello_agent = create_agent(
        name=args.agent_name,
        persona=(
            "You turn a short topic into a clear three-bullet summary.\n"
            "Stay concrete. Do not invent sources."
        ),
        always_allowed_tools=["note"],
        auto_approve_tools=[{"tool": "note"}],
    )

    summarize = create_stage(
        id="summarize",
        agent=args.agent_name,
        description="Summarize the topic in three bullets.",
        note=(
            "Read the workflow required input `topic`.\n"
            "Reply with exactly three bullets, then stop."
        ),
    )

    workflow = (
        create_workflow(
            name=args.workflow_name,
            domain="product",
            required_inputs=["topic"],
            tags=["example"],
        )
        .then(summarize)
        .commit()
    )

    published = client.aiden.publish_workflow(
        workflow,
        agents=[hello_agent],
        approve=True,
    )
    print(
        f"published {published.workflow_name} v{published.version} ({published.status})"
    )
    print(f"run with: stackgen ai run workflow/{args.workflow_name} --input 'topic=…'")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
