// v1.12 Track B.2: codecontext wrapper — get_codebase_overview.
// Pattern mirrors services/web_search.ts: pure executor + ToolDef wrapper.
// target_dir is supplied by callCodecontext from the resolved project root.

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

export const GetCodebaseOverviewInput = z.object({
  include_stats: z.boolean().optional(),
});
export type GetCodebaseOverviewInputT = z.infer<typeof GetCodebaseOverviewInput>;

const DESCRIPTION =
  'Returns a structured overview of the codebase: file count, symbol count, primary languages, and top-level architecture. ' +
  'Use this before deeper investigation to orient yourself in an unfamiliar codebase. ' +
  'Tree-sitter coverage: full for JS/Python/Java/Go/Rust/C++. TypeScript symbols are approximate (uses JS grammar). ' +
  'PHP and SQL are not supported — fall back to view_file/grep for those.';

export async function executeGetCodebaseOverview(
  input: GetCodebaseOverviewInputT,
  projectPath: string,
  fetcher: typeof fetch = fetch,
): Promise<CodecontextResponse> {
  return callCodecontext(
    {
      toolName: 'get_codebase_overview',
      args: { include_stats: input.include_stats ?? true },
      projectPath,
    },
    fetcher,
  );
}

export const getCodebaseOverview: ToolDef<GetCodebaseOverviewInputT> = {
  name: 'get_codebase_overview',
  description: DESCRIPTION,
  inputSchema: GetCodebaseOverviewInput,
  jsonSchema: {
    type: 'function',
    function: {
      name: 'get_codebase_overview',
      description: DESCRIPTION,
      parameters: {
        type: 'object',
        properties: {
          include_stats: {
            type: 'boolean',
            description: 'Include file count, symbol count, language stats. Defaults to true.',
          },
        },
        additionalProperties: false,
      },
    },
  },
  async execute(input, projectRoot) {
    return await executeGetCodebaseOverview(input, projectRoot);
  },
};
