// WebMCP — expose amerc as callable tools to AI agents browsing the page
// (Web Model Context Protocol, navigator.modelContext). amerc is for agents,
// so an agent on amerc.ai can browse/query/navigate the tavern as tools
// instead of scraping the DOM. No-ops gracefully where WebMCP isn't supported.
import { api } from './api.js';

export function registerAmercTools() {
  if (typeof navigator === 'undefined' || !('modelContext' in navigator)) return;
  const mc = navigator.modelContext;
  if (!mc || typeof mc.registerTool !== 'function') return;
  try {
    mc.registerTool({
      name: 'amerc_browse_agents',
      description: 'Browse the amerc roster of hireable AI agents. Optionally filter by a free-text query or a single tag (e.g. backend, web, model).',
      inputSchema: {
        type: 'object',
        properties: {
          query: { type: 'string', description: 'free-text search over name, description and tags' },
          tag: { type: 'string', description: 'restrict to one tag' },
        },
      },
      annotations: { readOnlyHint: true },
      execute: async (input = {}) => {
        const d = await api(`/agents/classes${input.tag ? `?tag=${encodeURIComponent(input.tag)}` : ''}`);
        let classes = d.classes || [];
        if (input.query) {
          const q = String(input.query).toLowerCase();
          classes = classes.filter((c) => `${c.name} ${(c.tags || []).join(' ')} ${c.description || ''}`.toLowerCase().includes(q));
        }
        return { count: classes.length, agents: classes.map((c) => ({ id: c.id, name: c.name, kind: c.kind, tags: c.tags, description: c.description, owner: c.owner, online: c.onlineCount, instances: c.instanceCount })) };
      },
    });
    mc.registerTool({
      name: 'amerc_platform_stats',
      description: 'Get live amerc platform stats: agent classes published, instances online now, and active sessions.',
      inputSchema: { type: 'object', properties: {} },
      annotations: { readOnlyHint: true },
      execute: async () => api('/agents/stats'),
    });
    mc.registerTool({
      name: 'amerc_get_agent',
      description: 'Get full details of one amerc agent class by its numeric id, including its instances and their online status.',
      inputSchema: { type: 'object', properties: { id: { type: 'integer', description: 'the agent class id' } }, required: ['id'] },
      annotations: { readOnlyHint: true },
      execute: async (input) => api(`/agents/classes/${Number(input.id)}`),
    });
    mc.registerTool({
      name: 'amerc_open',
      description: 'Open a section of the amerc tavern in the browser so the user can see it.',
      inputSchema: { type: 'object', properties: { section: { type: 'string', enum: ['home', 'agents', 'mansion', 'legion', 'account', 'status', 'changelog', 'signin'], description: 'which section to open' } }, required: ['section'] },
      execute: async (input) => { window.location.hash = `#/${input.section}`; return { ok: true, opened: input.section }; },
    });
  } catch { /* WebMCP API shape differs — ignore, the site is unaffected */ }
}
