/**
 * @file agent-capabilities.ts
 * @description MCP tools for agent capability reporting and activity history viewing.
 *   Tool 1 lets an agent self-report its technical/domain capabilities.
 *   Tool 2 lets an agent view its own activity stats and history.
 * @structure
 *   - registerAgentCapabilityTools() -- registers capability + activity tools on an McpServer instance
 * @usage
 *   import { registerAgentCapabilityTools } from './agent-capabilities.js';
 *   registerAgentCapabilityTools(mcp, storage, config, getAgentGaii, emitResourceUpdated, emitResourceListChanged);
 * @version-history
 *   v1.0.0 -- 2026-05-20 -- Initial creation for Agent Dashboard Phase 2
 *   v1.1.0 -- 2026-05-29 -- Add tool annotations (title + read/destructive/idempotent/openWorld hints)
 *     from shared annotations.ts for Connectors Directory compliance.
 *   v1.2.0 -- 2026-05-30 -- MCP audit Phase 1: tool descriptions sourced from canonical catalog via descriptionFor().
 */

import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import type { AimeatConfig } from '../config.js';
import type { Storage, AgentTechnicalCapability } from '../storage/interface.js';
import { annotationsFor } from './annotations.js';
import { descriptionFor } from './catalog/shape.js';

export function registerAgentCapabilityTools(
    mcp: McpServer,
    storage: Storage,
    config: AimeatConfig,
    getAgentGaii: () => string,
    emitResourceUpdated: (agentGaii: string, uri: string) => void,
    _emitResourceListChanged: (agentGaii: string) => void,
): void {
    const agentGaii = getAgentGaii();

    // ── Tool 1: aimeat_agent_capabilities_report ──
    mcp.tool(
        'aimeat_agent_capabilities_report',
        descriptionFor('aimeat_agent_capabilities_report'),
        {
            technical: z.array(z.object({
                name: z.string().describe('Capability name (e.g. "playwright", "git", "python")'),
                type: z.enum(['mcp', 'skill', 'tool']).describe('Capability type'),
            })).optional().describe('Technical capabilities (MCP servers, skills, tools)'),
            domain: z.array(z.string()).optional().describe('Domain expertise areas (e.g. "web development", "data analysis")'),
            languages: z.array(z.string()).optional().describe('Human languages the agent can work in (e.g. "fi", "en", "de")'),
        },
        annotationsFor('aimeat_agent_capabilities_report'),
        async ({ technical, domain, languages }) => {
            const agent = await storage.getAgent(agentGaii);
            if (!agent) {
                return { content: [{ type: 'text' as const, text: 'Agent not found' }], isError: true };
            }

            // Agent is connected via MCP -- MCP-type capabilities are verified
            const technicalCapabilities: AgentTechnicalCapability[] = (technical ?? []).map(cap => ({
                name: cap.name,
                type: cap.type,
                verified: cap.type === 'mcp',
            }));

            // Build domain capabilities, merging language entries as "Language: fi" etc.
            const domainCapabilities = [...(domain ?? [])];
            if (languages) {
                for (const lang of languages) {
                    domainCapabilities.push(`Language: ${lang}`);
                }
            }

            const updated = await storage.updateAgent(agentGaii, {
                technicalCapabilities,
                domainCapabilities,
            });

            if (!updated) {
                return { content: [{ type: 'text' as const, text: 'Failed to update capabilities' }], isError: true };
            }

            emitResourceUpdated(agentGaii, `aimeat://agents/${agentGaii}/capabilities`);

            return {
                content: [{
                    type: 'text' as const,
                    text: JSON.stringify({
                        technical_capabilities: updated.technicalCapabilities ?? [],
                        domain_capabilities: updated.domainCapabilities ?? [],
                    }, null, 2),
                }],
            };
        },
    );

    // ── Tool 2: aimeat_agent_activity ──
    mcp.tool(
        'aimeat_agent_activity',
        descriptionFor('aimeat_agent_activity'),
        {
            days: z.number().optional().describe('Number of days of history to retrieve (default 30)'),
            granularity: z.enum(['daily', 'hourly']).optional().describe('Granularity of history records (default daily)'),
        },
        annotationsFor('aimeat_agent_activity'),
        async ({ days, granularity }) => {
            const agent = await storage.getAgent(agentGaii);
            if (!agent) {
                return { content: [{ type: 'text' as const, text: 'Agent not found' }], isError: true };
            }

            const history = await storage.getActivityHistory(agentGaii, {
                days: days ?? 30,
                granularity: granularity ?? 'daily',
            });

            return {
                content: [{
                    type: 'text' as const,
                    text: JSON.stringify({
                        stats: agent.activityStats ?? null,
                        history,
                    }, null, 2),
                }],
            };
        },
    );
}
