// v1.12 Track B.2: codecontext wrapper — get_dependencies.

import { z } from 'zod';
import type { ToolDef } from '../../tools.js';
import { callCodecontext, type CodecontextResponse } from '../../codecontext_client.js';

export const GetDependenciesInput = z.object({
  file_path: z.string().trim().optional(),
  direction: z.enum(['incoming', 'outgoing', 'both']).optional(),
});
export type GetDependenciesInputT = z.infer<typeof GetDependenciesInput>;

const DESCRIPTION =
  'Returns the import/dependency graph either for a single file (when file_path is set) or for the whole project. ' +
  'Direction "outgoing" = what this file imports; "incoming" = what imports this file; "both" = the union. ' +
  'Tree-sitter coverage: full for JS/Python/Java/Go/Rust/C++. TypeScript dependencies are approximate. ' +
  'PHP and SQL are not supported.';

export async function executeGetDependencies(
  input: GetDependenciesInputT,
  projectPath: string,
  fetcher: typeof fetch = fetch,
): Promise<CodecontextResponse> {
  const args: Record<string, unknown> = {
    direction: input.direction ?? 'both',
  };
  if (input.file_path) args['file_path'] = input.file_path;
  return callCodecontext({ toolName: 'get_dependencies', args, projectPath }, fetcher);
}

export const getDependencies: ToolDef<GetDependenciesInputT> = {
  name: 'get_dependencies',
  description: DESCRIPTION,
  inputSchema: GetDependenciesInput,
  jsonSchema: {
    type: 'function',
    function: {
      name: 'get_dependencies',
      description: DESCRIPTION,
      parameters: {
        type: 'object',
        properties: {
          file_path: {
            type: 'string',
            description: 'Narrow to a single file. Omit for a project-wide graph.',
          },
          direction: {
            type: 'string',
            enum: ['incoming', 'outgoing', 'both'],
            description: 'Which edges to include. Defaults to "both".',
          },
        },
        additionalProperties: false,
      },
    },
  },
  async execute(input, projectRoot) {
    return await executeGetDependencies(input, projectRoot);
  },
};
