/**
 * MCP tools for rVnb (community vehicle sharing).
 *
 * Tools: rvnb_list_vehicles, rvnb_get_vehicle, rvnb_list_rentals
 */

import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import type { SyncServer } from "../local-first/sync-server";
import { vnbDocId } from "../../modules/rvnb/schemas";
import type { VnbDoc } from "../../modules/rvnb/schemas";
import { resolveAccess, accessDeniedResponse } from "./_auth";

export function registerVnbTools(server: McpServer, syncServer: SyncServer) {
	server.tool(
		"rvnb_list_vehicles",
		"List vehicle listings (RVs, camper vans, etc.)",
		{
			space: z.string().describe("Space slug"),
			token: z.string().optional().describe("JWT auth token"),
			type: z.string().optional().describe("Filter by type (motorhome, camper_van, travel_trailer, skoolie, etc.)"),
			active_only: z.boolean().optional().describe("Only active listings (default true)"),
		},
		async ({ space, token, type, active_only }) => {
			const access = await resolveAccess(token, space, false);
			if (!access.allowed) return accessDeniedResponse(access.reason!);

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

			let vehicles = Object.values(doc.vehicles || {});
			if (active_only !== false) vehicles = vehicles.filter(v => v.isActive);
			if (type) vehicles = vehicles.filter(v => v.type === type);

			const summary = vehicles.map(v => ({
				id: v.id, ownerName: v.ownerName, title: v.title,
				type: v.type, economy: v.economy,
				year: v.year, make: v.make, model: v.model,
				sleeps: v.sleeps, lengthFeet: v.lengthFeet,
				hasSolar: v.hasSolar, hasAC: v.hasAC, hasKitchen: v.hasKitchen,
				petFriendly: v.petFriendly,
				suggestedAmount: v.suggestedAmount, currency: v.currency,
				pickupLocationName: v.pickupLocationName,
			}));

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

	server.tool(
		"rvnb_get_vehicle",
		"Get full vehicle details with trip availability windows",
		{
			space: z.string().describe("Space slug"),
			token: z.string().optional().describe("JWT auth token"),
			vehicle_id: z.string().describe("Vehicle ID"),
		},
		async ({ space, token, vehicle_id }) => {
			const access = await resolveAccess(token, space, false);
			if (!access.allowed) return accessDeniedResponse(access.reason!);

			const doc = syncServer.getDoc<VnbDoc>(vnbDocId(space));
			const vehicle = doc?.vehicles?.[vehicle_id];
			if (!vehicle) return { content: [{ type: "text", text: JSON.stringify({ error: "Vehicle not found" }) }] };

			const availability = Object.values(doc!.availability || {})
				.filter(a => a.vehicleId === vehicle_id);

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

	server.tool(
		"rvnb_list_rentals",
		"List rental requests with status",
		{
			space: z.string().describe("Space slug"),
			token: z.string().optional().describe("JWT auth token"),
			status: z.string().optional().describe("Filter by status (pending, accepted, declined, completed, etc.)"),
		},
		async ({ space, token, status }) => {
			const access = await resolveAccess(token, space, false);
			if (!access.allowed) return accessDeniedResponse(access.reason!);

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

			let rentals = Object.values(doc.rentals || {});
			if (status) rentals = rentals.filter(r => r.status === status);
			rentals.sort((a, b) => b.requestedAt - a.requestedAt);

			const summary = rentals.map(r => ({
				id: r.id, vehicleId: r.vehicleId,
				renterName: r.renterName, status: r.status,
				pickupDate: r.pickupDate, dropoffDate: r.dropoffDate,
				estimatedMiles: r.estimatedMiles,
				messageCount: r.messages?.length ?? 0,
				requestedAt: r.requestedAt,
			}));

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