#!/usr/bin/env node

import { createRequire } from "node:module";

const require = createRequire(import.meta.url);
const packageJson = require("../package.json") as { version: string };

// Handle `orchard-mcp setup` subcommand before starting MCP server.
if (process.argv[2] === "setup") {
  const { runSetup } = await import("./setup.js");
  await runSetup(process.argv.includes("--non-interactive"));
  process.exit(0);
}

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { registerCalendarTools } from "./tools/calendar.js";
import { registerMailTools } from "./tools/mail.js";
import { registerReminderTools } from "./tools/reminders.js";
import { registerSystemTools } from "./tools/system.js";
import { registerFileTools } from "./tools/files.js";
import { registerNumbersTools } from "./tools/numbers.js";
import { registerPagesTools } from "./tools/pages.js";
import { registerKeynoteTools } from "./tools/keynote.js";
import { registerNotesTools } from "./tools/notes.js";
import { registerContactsTools } from "./tools/contacts.js";

const server = new McpServer({
  name: "orchard-mcp",
  version: packageJson.version,
});

registerCalendarTools(server);
registerMailTools(server);
registerReminderTools(server);
registerSystemTools(server);
registerFileTools(server);
registerNumbersTools(server);
registerPagesTools(server);
registerKeynoteTools(server);
registerNotesTools(server);
registerContactsTools(server);

const transport = new StdioServerTransport();
await server.connect(transport);

// Reason: stderr only -- stdout is reserved for JSON-RPC in stdio transport.
console.error("[orchard-mcp] Server started via stdio transport.");
