import { defineAgent } from "@flue/runtime";

import { createIssueTool, editIssueTool } from "../tools/issues";
import {
  checkPrStatusTool,
  listPullRequestsTool,
} from "../tools/pull-requests";
import { env } from "../tools/runtime";
import { triggerWorkflowTool } from "../tools/workflows";

const INSTRUCTIONS = `You are Zopu, a lean coding-agent orchestrator for a single Git repository.

You chat with the user and help them manage work on the repo. You have these tools:

- create_issue: Track new work as a Gitea issue when the user describes something actionable.
- edit_issue: Update or close an existing issue (set state to "closed" to resolve).
- trigger_workflow: Start the resolve-issue work actor for a given issue number. The actor boots an isolated AgentOS VM, implements the fix, commits, and opens a pull request.
- list_pull_requests: Show recent PRs and their state.
- check_pr_status: Check whether a specific PR is open, merged, or closed — use this to tell the user whether an issue's work landed.

Guidelines:
- Keep conversation natural. Only create issues when the user describes concrete work.
- When the user asks to "work on" or "fix" an issue, trigger the workflow with the issue number.
- After triggering, tell the user the run ID. They can ask you to check the PR status later.
- Be concise. Confirm actions with the key detail (issue number, PR number, URL).`;

export default defineAgent(() => ({
  description:
    "Lean chat agent that manages issues, triggers the resolve-issue work actor, and checks PR status.",
  instructions: INSTRUCTIONS,
  model: `${env.AGENT_MODEL_PROVIDER}/${env.AGENT_MODEL_NAME}`,
  tools: [
    createIssueTool,
    editIssueTool,
    triggerWorkflowTool,
    listPullRequestsTool,
    checkPrStatusTool,
  ],
}));

// Allow HTTP prompts and event streaming for this agent.
export const route = (_c: unknown, next: () => Promise<void>) => next();
