//! Tool schema definitions for agent and worktree management.

use serde_json::{Value, json};

/// Returns tool schemas for agent lifecycle and worktree operations.
pub(super) fn agent_tools() -> Vec<Value> {
    vec![
        json!({
            "name": "start_agent",
            "description": "Start an agent for a story. Creates a worktree, runs setup, and spawns the agent process.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "story_id": {
                        "type": "string",
                        "description": "Story identifier (e.g. '28_my_story')"
                    },
                    "agent_name": {
                        "type": "string",
                        "description": "Agent name from project.toml config. If omitted, uses the first coder agent (stage = \"coder\"). Supervisor must be requested explicitly by name."
                    }
                },
                "required": ["story_id"]
            }
        }),
        json!({
            "name": "stop_agent",
            "description": "Stop a running agent. Worktree is preserved for inspection.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "story_id": {
                        "type": "string",
                        "description": "Story identifier"
                    },
                    "agent_name": {
                        "type": "string",
                        "description": "Agent name to stop"
                    }
                },
                "required": ["story_id", "agent_name"]
            }
        }),
        json!({
            "name": "list_agents",
            "description": "List all agents with their current status, story assignment, and worktree path.",
            "inputSchema": {
                "type": "object",
                "properties": {}
            }
        }),
        json!({
            "name": "get_agent_config",
            "description": "Get the configured agent roster from project.toml (names, roles, models, allowed tools, limits).",
            "inputSchema": {
                "type": "object",
                "properties": {}
            }
        }),
        json!({
            "name": "reload_agent_config",
            "description": "Reload project.toml and return the updated agent roster.",
            "inputSchema": {
                "type": "object",
                "properties": {}
            }
        }),
        json!({
            "name": "get_agent_output",
            "description": "Read agent session logs from disk as a human-readable timeline. Stitches all sessions for the story together in chronological order — text output, tool calls, tool results, errors. Works for both running and completed agents. If agent_name is omitted, returns logs from every agent that worked on the story. If the named agent is currently running, live buffered events are appended.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "story_id": {
                        "type": "string",
                        "description": "Story identifier (e.g. '42_story_my_feature')"
                    },
                    "agent_name": {
                        "type": "string",
                        "description": "Optional: filter to a specific agent (e.g. 'mergemaster', 'coder-1'). Omit to see all agents."
                    },
                    "lines": {
                        "type": "integer",
                        "description": "Optional: return only the last N lines (tail). Useful for large logs."
                    },
                    "filter": {
                        "type": "string",
                        "description": "Optional: return only lines containing this substring (e.g. 'ERROR', 'TOOL:', a function name)."
                    }
                },
                "required": ["story_id"]
            }
        }),
        json!({
            "name": "wait_for_agent",
            "description": "Block until the agent reaches a terminal state (completed, failed, stopped). Returns final status and summary including session_id, worktree_path, and any commits made. Use this instead of polling get_agent_output when you want to fire-and-forget and be notified on completion.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "story_id": {
                        "type": "string",
                        "description": "Story identifier"
                    },
                    "agent_name": {
                        "type": "string",
                        "description": "Agent name to wait for"
                    },
                    "timeout_ms": {
                        "type": "integer",
                        "description": "Maximum time to wait in milliseconds (default: 300000 = 5 minutes)"
                    }
                },
                "required": ["story_id", "agent_name"]
            }
        }),
        json!({
            "name": "get_agent_remaining_turns_and_budget",
            "description": "Get remaining turns and budget for a running agent. Returns turns used, max turns, remaining turns, budget used (from completed sessions), max budget, and remaining budget. Only works for agents in running or pending state.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "story_id": {
                        "type": "string",
                        "description": "Story identifier (e.g. '42_story_my_feature')"
                    },
                    "agent_name": {
                        "type": "string",
                        "description": "Agent name (e.g. 'coder-1', 'mergemaster', 'qa')"
                    }
                },
                "required": ["story_id", "agent_name"]
            }
        }),
        json!({
            "name": "create_worktree",
            "description": "Create a git worktree for a story under .huskies/worktrees/{story_id} with deterministic naming. Writes .mcp.json and runs component setup. Returns the worktree path.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "story_id": {
                        "type": "string",
                        "description": "Story identifier (e.g. '42_my_story')"
                    }
                },
                "required": ["story_id"]
            }
        }),
        json!({
            "name": "list_worktrees",
            "description": "List all worktrees under .huskies/worktrees/ for the current project.",
            "inputSchema": {
                "type": "object",
                "properties": {}
            }
        }),
        json!({
            "name": "remove_worktree",
            "description": "Remove a git worktree and its feature branch for a story.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "story_id": {
                        "type": "string",
                        "description": "Story identifier"
                    }
                },
                "required": ["story_id"]
            }
        }),
        json!({
            "name": "cleanup_worktrees",
            "description": "List orphaned worktrees (dry run) or remove them (confirm: true). A worktree is orphaned when its story is missing from the CRDT or is in Done/Archived stage.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "confirm": {
                        "type": "boolean",
                        "description": "When true, remove all orphaned worktrees. When false (default), only list them."
                    }
                }
            }
        }),
        json!({
            "name": "gc",
            "description": "Run a build-directory GC pass on demand (story 1199): reclaims orphaned worktree target/ dirs and a stale merge_workspace, never touching the main project's or any live worktree's target/. Returns bytes reclaimed, duration, and which directories were reclaimed or skipped.",
            "inputSchema": {
                "type": "object",
                "properties": {}
            }
        }),
        json!({
            "name": "get_editor_command",
            "description": "Get the open-in-editor command for a worktree. Returns a ready-to-paste shell command like 'zed /path/to/worktree'. Requires the editor preference to be configured via PUT /api/settings/editor.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "worktree_path": {
                        "type": "string",
                        "description": "Absolute path to the worktree directory"
                    }
                },
                "required": ["worktree_path"]
            }
        }),
    ]
}
