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

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

export const GetFrameworkAnalysisInput = z.object({
  framework: z.string().optional(),
  include_stats: z.boolean().optional(),
});
export type GetFrameworkAnalysisInputT = z.infer<typeof GetFrameworkAnalysisInput>;

const DESCRIPTION =
  'Returns framework-specific structural analysis: component relationships (React), hook usage patterns, store wiring (Vue/Pinia), service registration (Angular/Nest), etc. ' +
  'When framework is omitted, codecontext auto-detects from the project files. ' +
  'Tree-sitter coverage: full for JS/Python/Java/Go/Rust/C++. TypeScript is approximate. ' +
  'PHP and SQL are not supported.';

export async function executeGetFrameworkAnalysis(
  input: GetFrameworkAnalysisInputT,
  projectPath: string,
  fetcher: typeof fetch = fetch,
): Promise<CodecontextResponse> {
  const args: Record<string, unknown> = {};
  if (input.framework) args['framework'] = input.framework;
  if (input.include_stats !== undefined) args['include_stats'] = input.include_stats;
  return callCodecontext({ toolName: 'get_framework_analysis', args, projectPath }, fetcher);
}

export const getFrameworkAnalysis: ToolDef<GetFrameworkAnalysisInputT> = {
  name: 'get_framework_analysis',
  description: DESCRIPTION,
  inputSchema: GetFrameworkAnalysisInput,
  jsonSchema: {
    type: 'function',
    function: {
      name: 'get_framework_analysis',
      description: DESCRIPTION,
      parameters: {
        type: 'object',
        properties: {
          framework: {
            type: 'string',
            description: 'Framework name. Auto-detected if omitted.',
          },
          include_stats: {
            type: 'boolean',
            description: 'Include component/hook/service counts.',
          },
        },
        additionalProperties: false,
      },
    },
  },
  async execute(input, projectRoot) {
    return await executeGetFrameworkAnalysis(input, projectRoot);
  },
};
