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

export const GetBlastRadiusInput = z.object({
  file_path: z.string().trim().min(1),
});
export type GetBlastRadiusInputT = z.infer<typeof GetBlastRadiusInput>;

const DESCRIPTION =
  'Returns all files that depend (transitively) on the given file, with depth tracking. ' +
  'Use to assess the impact of changing a file — "what breaks if I modify this?" ' +
  'Traverses the import graph in reverse via BFS. Results sorted by distance (closest dependents first).';

export async function executeGetBlastRadius(
  input: GetBlastRadiusInputT,
  projectPath: string,
  fetcher: typeof fetch = fetch,
): Promise<CodecontextResponse> {
  return callCodecontext(
    { toolName: 'get_blast_radius', args: { file_path: input.file_path }, projectPath },
    fetcher,
  );
}

export const getBlastRadius: ToolDef<GetBlastRadiusInputT> = {
  name: 'get_blast_radius',
  description: DESCRIPTION,
  inputSchema: GetBlastRadiusInput,
  jsonSchema: {
    type: 'function',
    function: {
      name: 'get_blast_radius',
      description: DESCRIPTION,
      parameters: {
        type: 'object',
        properties: {
          file_path: {
            type: 'string',
            description: 'Absolute or project-relative path to the file to analyze.',
          },
        },
        required: ['file_path'],
        additionalProperties: false,
      },
    },
  },
  async execute(input, projectRoot) {
    return await executeGetBlastRadius(input, projectRoot);
  },
};
