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

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

export const WatchChangesInput = z.object({
  enable: z.boolean(),
});
export type WatchChangesInputT = z.infer<typeof WatchChangesInput>;

const DESCRIPTION =
  'Turn codecontext\'s file watcher on or off for this project. ' +
  'When on, codecontext re-analyzes files in the background as they change (debounced). Default is on. ' +
  'Disable temporarily if you\'re doing bulk edits and want to avoid analysis churn.';

export async function executeWatchChanges(
  input: WatchChangesInputT,
  projectPath: string,
  fetcher: typeof fetch = fetch,
): Promise<CodecontextResponse> {
  return callCodecontext(
    {
      toolName: 'watch_changes',
      args: { enable: input.enable },
      projectPath,
    },
    fetcher,
  );
}

export const watchChanges: ToolDef<WatchChangesInputT> = {
  name: 'watch_changes',
  description: DESCRIPTION,
  inputSchema: WatchChangesInput,
  jsonSchema: {
    type: 'function',
    function: {
      name: 'watch_changes',
      description: DESCRIPTION,
      parameters: {
        type: 'object',
        properties: {
          enable: {
            type: 'boolean',
            description: 'true = enable the watcher; false = disable.',
          },
        },
        required: ['enable'],
        additionalProperties: false,
      },
    },
  },
  async execute(input, projectRoot) {
    return await executeWatchChanges(input, projectRoot);
  },
};
