import { z } from "zod";
import type { FileInputSpec, ToolDefinition } from "@argent/registry";
import { readWorkspaceSnapshot, type WorkspaceSnapshot } from "../../utils/workspace-reader";

const zodSchema = z.object({
  workspacePath: z
    .string()
    .describe("Absolute path to the project root directory to inspect (e.g. /Users/dev/MyApp)"),
});

/**
 * The snapshot reads dozens of files across the workspace tree, which can't
 * ride along in a tool call — gate on the directory existing on this host so
 * a remote caller gets an actionable error instead of the all-nulls snapshot
 * readWorkspaceSnapshot returns for an unreadable path.
 */
const fileInputs: FileInputSpec[] = [
  { target: "workspacePath", path: "${workspacePath}", kind: "directory" },
];

export const gatherWorkspaceDataTool: ToolDefinition<
  z.infer<typeof zodSchema>,
  WorkspaceSnapshot
> = {
  id: "gather-workspace-data",
  interaction: {
    startedMsg: () => "Gathering workspace data",
    completedMsg: () => "Gathered workspace data",
    failedMsg: ({ failureSignal }) =>
      `Failed to gather workspace data: ${failureSignal.error_code}`,
  },
  description: `Fetch a structured snapshot of a mobile app project's workspace.

Returns package.json contents, metro/babel config text, app.json, eas.json, tsconfig,
platform directory presence (ios/, android/), presence of android/gradlew (android_has_gradle),
iOS .xcworkspace name and Podfile presence, lockfile type, .env file keys (no values),
installed CLI tool versions, scripts/ directory listing, husky hooks, CI config type,
Makefile targets, lint-staged config, and a list of detected config files.

DO NOT RUN THIS TOOL IF YOU ARE THE MAIN AGENT AND THIS TASK CAN BE DELEGATED TO A SUBAGENT.

If you are a subagent tasked with exploring the project environment, run this as the first step. The snapshot
provides the raw data needed to determine the project type (React Native, Expo,
Flutter, native iOS/Android, or other), build commands, startup scripts, platform
support, package manager, and QA tooling. Follow up with Read/Glob/Grep for deeper
exploration of anything the snapshot surfaces.
Use when you need to inspect project configuration without manually reading multiple files.
Returns partial data if workspacePath does not exist or is not readable; missing items are represented as null or empty collections.
Fails if the workspacePath is not an absolute path or the directory cannot be accessed.`,
  zodSchema,
  fileInputs,
  services: () => ({}),
  async execute(_services, params) {
    return readWorkspaceSnapshot(params.workspacePath);
  },
};
