import { z } from 'zod';
import { makeCodecontextTool } from './factory.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.';

const { toolDef: getDependencies, execute: executeGetDependencies } =
  makeCodecontextTool<GetDependenciesInputT>({
    name: 'get_dependencies',
    schema: GetDependenciesInput,
    description: DESCRIPTION,
    jsonParameters: {
      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,
    },
    mapArgs: (input) => {
      const args: Record<string, unknown> = { direction: input.direction ?? 'both' };
      if (input.file_path) args['file_path'] = input.file_path;
      return args;
    },
  });

export { getDependencies, executeGetDependencies };
