/**
 * VLLint Tool - static validation via lintPjt API
 * Faster than full compile (parsePjt) — no preview/case generation.
 * Syncs files to cloud workspace then runs lintPjt.
 */

export function createVLLintTool(config) {
  return {
    name: 'VLLint',
    description: `Run static validation (lintPjt) on the VL project. This is faster than full compile — it checks syntax, component types, structure, and expressions without generating previews. Use this for quick validation during development. Returns errList with error details (level, type, message, lineNumber, path).`,
    parameters: {
      type: 'object',
      properties: {
        gid: {
          type: 'number',
          description: 'Optional workspace GID override. If not provided, uses the stored compileGid from project.json.',
        },
      },
      required: [],
    },
    execute: async (input) => {
      try {
        const port = config.port || 3200;
        const body = input.gid ? { gid: input.gid } : {};
        const res = await fetch(`http://localhost:${port}/api/cloud/lint`, {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify(body),
        });
        const data = await res.json();

        if (data.error) {
          return JSON.stringify({ success: false, error: data.error });
        }

        const result = {
          success: true,
          gid: data.gid,
          summary: data.summary || { errors: 0, warnings: 0 },
        };
        if (data.errList?.length) {
          result.errList = data.errList;
        } else {
          result.message = 'No errors or warnings found.';
        }
        return JSON.stringify(result, null, 2);
      } catch (e) {
        return JSON.stringify({ success: false, error: e.message });
      }
    },
  };
}
