import type { ZodTypeAny, z } from 'zod';

/** Minimal structured logger — matches Fastify's pino-based logger shape */
export interface McpLogger {
  info(obj: Record<string, unknown>, msg?: string): void;
  warn(obj: Record<string, unknown>, msg?: string): void;
  error(obj: Record<string, unknown>, msg?: string): void;
  debug(obj: Record<string, unknown>, msg?: string): void;
}

/**
 * Minimal request context passed to tool execute functions.
 * Avoids a direct FastifyRequest dependency inside tool modules.
 */
export interface McpToolRequest {
  /** Fastify request ID */
  id: string;
  /** HTTP headers */
  headers: { authorization?: string | undefined };
  /** JWT payload decoded by the auth hook */
  jwtPayload?: { sub?: string; role?: string; productId?: string };
  /** Structured logger (child of Fastify request logger, carries requestId) */
  log: McpLogger;
}

/**
 * A single MCP tool definition.
 * inputSchema is a Zod object schema whose shape describes the tool's arguments.
 */
export interface McpTool<TInput extends ZodTypeAny = ZodTypeAny> {
  /** Fully-qualified tool name, e.g. "platform.telemetry.query" */
  name: string;
  description: string;
  /** Minimum role required to call this tool */
  requiredRole: 'viewer' | 'admin' | 'super_admin';
  /** Zod schema for input validation */
  inputSchema: TInput;
  /** Execute the tool. Must return a JSON-serialisable value. */
  execute(args: z.infer<TInput>, req: McpToolRequest): Promise<unknown>;
}

/** What we return for tools/list */
export interface McpToolMeta {
  name: string;
  description: string;
  requiredRole: string;
  inputSchema: Record<string, unknown>;
}

/** tools/call request body */
export interface McpCallRequest {
  name: string;
  arguments: Record<string, unknown>;
  /** Optional caller-supplied request ID; falls back to req.id */
  requestId?: string;
}

/** tools/call response (MCP content format) */
export interface McpCallResponse {
  content: Array<{ type: 'text'; text: string }>;
  isError?: boolean;
}
