import { runRead, VERSION } from './agent-core.mjs';

const tools = [
  ['branddesign_discover','discover','Read Brand Design Ltd. agent onboarding, file URLs and available interfaces.'],
  ['branddesign_services','services','Read the current Brand Design Ltd. service overview and family architecture.'],
  ['branddesign_service','service','Read a service by its catalog id.'],
  ['branddesign_pricing','pricing','Read current indicative prices. This does not create or accept a payable quote.'],
  ['branddesign_catalog','catalog','Read the live commerce catalog and ordering eligibility.'],
  ['branddesign_evidence','evidence','Read public evidence and the qualification of claims.'],
  ['branddesign_capabilities','capabilities','Read the existing commerce manifest, MCP/API capabilities and access requirements.']
];

export async function registerBrandDesignTools(context) {
  const registered = [], failures = [];
  if (!context || typeof context.registerTool !== 'function')
    return {version:VERSION,status:'unavailable',registered,failures};
  for (const [name,command,description] of tools) {
    try {
      await context.registerTool({
        name, description:description + ' Public GET only; grants no permissions or spending authority.',
        inputSchema: {type:'object',properties:command === 'service' ? {
          id:{type:'string',pattern:'^[a-z0-9][a-z0-9-]{0,79}$',maxLength:80,
            description:'Service id from branddesign_services, for example b2a-brand.'}
        } : {},required:command === 'service' ? ['id'] : [],additionalProperties:false},
        annotations:{readOnlyHint:true,untrustedContentHint:true,consequentialHint:false},
        execute:async (input = {}, options = {}) => JSON.stringify(await runRead(command,input,{signal:options.signal}))
      });
      registered.push(name);
    } catch (error) { failures.push({name,error:error.message}); }
  }
  return {version:VERSION,status:failures.length ? 'partial' : 'registered',registered,failures};
}

// Document API follows the current draft; Navigator fallback supports earlier implementations.
if (typeof document !== 'undefined') {
  const context = document.modelContext || globalThis.navigator?.modelContext;
  registerBrandDesignTools(context).then(result => {
    document.documentElement.dataset.branddesignWebmcp = result.status;
    document.documentElement.dataset.branddesignWebmcpTools = String(result.registered.length);
    if (result.failures.length) console.warn('Brand Design Ltd. WebMCP registration incomplete',result.failures);
  });
}
