/**
 * MCP tools for CrowdSurf (community activity prompts with swipe commitment).
 *
 * Tools: crowdsurf_list_prompts, crowdsurf_get_prompt
 */

import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import type { SyncServer } from "../local-first/sync-server";
import { crowdsurfDocId, getRightSwipeCount, getUrgency } from "../../modules/crowdsurf/schemas";
import type { CrowdSurfDoc } from "../../modules/crowdsurf/schemas";
import { resolveAccess, accessDeniedResponse } from "./_auth";

export function registerCrowdSurfTools(server: McpServer, syncServer: SyncServer) {
	server.tool(
		"crowdsurf_list_prompts",
		"List activity prompts with swipe counts and urgency",
		{
			space: z.string().describe("Space slug"),
			token: z.string().optional().describe("JWT auth token"),
			active_only: z.boolean().optional().describe("Exclude triggered/expired prompts (default true)"),
			limit: z.number().optional().describe("Max results (default 50)"),
		},
		async ({ space, token, active_only, limit }) => {
			const access = await resolveAccess(token, space, false);
			if (!access.allowed) return accessDeniedResponse(access.reason!);

			const doc = syncServer.getDoc<CrowdSurfDoc>(crowdsurfDocId(space));
			if (!doc) return { content: [{ type: "text", text: JSON.stringify({ error: "No crowdsurf data found" }) }] };

			let prompts = Object.values(doc.prompts || {});
			if (active_only !== false) {
				prompts = prompts.filter(p => !p.triggered && !p.expired);
			}

			prompts.sort((a, b) => b.elo - a.elo);
			prompts = prompts.slice(0, limit || 50);

			const summary = prompts.map(p => ({
				id: p.id, text: p.text, location: p.location,
				threshold: p.threshold, duration: p.duration,
				rightSwipes: getRightSwipeCount(p),
				urgency: getUrgency(p),
				elo: p.elo, comparisons: p.comparisons,
				triggered: p.triggered, expired: p.expired,
				createdAt: p.createdAt,
			}));

			return { content: [{ type: "text", text: JSON.stringify(summary, null, 2) }] };
		},
	);

	server.tool(
		"crowdsurf_get_prompt",
		"Get full prompt details with swipe breakdown and contributions",
		{
			space: z.string().describe("Space slug"),
			token: z.string().optional().describe("JWT auth token"),
			prompt_id: z.string().describe("Prompt ID"),
		},
		async ({ space, token, prompt_id }) => {
			const access = await resolveAccess(token, space, false);
			if (!access.allowed) return accessDeniedResponse(access.reason!);

			const doc = syncServer.getDoc<CrowdSurfDoc>(crowdsurfDocId(space));
			const prompt = doc?.prompts?.[prompt_id];
			if (!prompt) return { content: [{ type: "text", text: JSON.stringify({ error: "Prompt not found" }) }] };

			const swipes = Object.entries(prompt.swipes || {}).map(([did, s]) => ({
				did, direction: s.direction, timestamp: s.timestamp,
				contribution: s.contribution || null,
			}));

			return {
				content: [{
					type: "text",
					text: JSON.stringify({
						...prompt,
						swipes,
						rightSwipeCount: getRightSwipeCount(prompt),
						urgency: getUrgency(prompt),
					}, null, 2),
				}],
			};
		},
	);
}
