#!/usr/bin/env python3
"""Send a chat turn with a file attachment, then rename/star the session and
tidy up the attachment.

``start_agent_chat_with_files`` posts ``multipart/form-data``; Aiden stores
each file and gives the agent an ``attachment://<session_id>/<attachment_id>``
handle instead of the raw bytes (for reference-mode agents) or inlines it
(inline agents, capped at 3 MiB/file). There is no list-attachments endpoint,
so the handle only shows up in the turn's own content -- ask the agent to
repeat it back, or scan the execution debug ZIP. Pass --attachment-id once
you have one to also exercise download + delete.

Usage:
  export STACKGEN_URL=https://app.stackgen.com
  export STACKGEN_TOKEN=stackgen_…
  export STACKGEN_WORKSPACE_ID=<workspace-uuid>
  python examples/automation/chat-session-attachments.py --agent my-ops-agent
  python examples/automation/chat-session-attachments.py \
    --agent my-ops-agent --attachment-id 8f14e45f-…
"""

from __future__ import annotations

import argparse
import os
import tempfile
from pathlib import Path

from stackgen import StackgenClient, StackgenConfig


def env(name: str, *fallbacks: str) -> str:
    for key in (name, *fallbacks):
        value = (os.environ.get(key) or "").strip()
        if value:
            return value
    raise SystemExit(f"missing env {name}")


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--agent", required=True, help="Agent name to chat with")
    parser.add_argument(
        "--attachment-id",
        default="",
        help="UUID from a previously seen attachment:// handle; also exercises download + delete",
    )
    args = parser.parse_args()

    client = StackgenClient(
        StackgenConfig(
            base_url=env("STACKGEN_URL"),
            api_token=env("STACKGEN_TOKEN"),
            workspace_id=env("STACKGEN_WORKSPACE_ID", "STACKGEN_PROJECT"),
        )
    )

    run = client.aiden.start_agent_chat_with_files(
        args.agent,
        message="Summarize the attached file in one sentence.",
        files=[
            {
                "filename": "sdk-sample-notes.txt",
                "content": b"Deploys succeed when the health check passes within 60s.\n",
                "content_type": "text/plain",
            }
        ],
    )
    session_id = str(run.get("session_id") or run.get("sessionId") or "")
    trace_id = str(run.get("trace_id") or run.get("traceId") or "")
    print(f"started chat session_id={session_id} trace_id={trace_id}")

    client.aiden.wait_for_subscribe_complete(trace_id, timeout_ms=5 * 60 * 1000)
    print("turn complete")

    updated = client.aiden.update_session(
        session_id, title="SDK sample: attachment summary", starred=True
    )
    print(f"renamed session title={updated.get('title')} starred={updated.get('starred')}")

    if not args.attachment_id:
        print(
            "no --attachment-id passed; skipping download_session_attachment. "
            "Capture an attachment_id from the agent's reply or the execution debug ZIP, "
            "then rerun with --attachment-id."
        )
        return 0

    with tempfile.TemporaryDirectory(prefix="sdk-attachment-") as tmp:
        dest = client.aiden.download_session_attachment(
            session_id, args.attachment_id, str(Path(tmp) / "downloaded.txt")
        )
        print(f"downloaded attachment to {dest}")

    client.aiden.delete_session_attachments(session_id)
    print(f"deleted all attachments for session_id={session_id}")
    return 0


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