/**
 * NoteLett MCP tools — notelett.notes.*, notelett.workspaces.*
 *
 * Backed by: notelett-backend (port 4016).
 * All tools require admin role.
 */

import { z } from 'zod';
import { registerTool } from '../tools/registry.js';
import { config } from '../../lib/config.js';
import {
  noteLettNotesList,
  noteLettNoteGet,
  noteLettNoteCreate,
  noteLettNoteUpdate,
  noteLettNoteDelete,
  noteLettNoteSummarize,
  noteLettWorkspacesList,
  noteLettWorkspaceCreate,
  noteLettNoteTasksList,
  noteLettNoteArtifactsList,
} from '../../lib/notelett-client.js';
import type { McpToolRequest } from '../tools/types.js';

const tokenOf = (req: McpToolRequest) => req.headers.authorization?.slice(7);

// ── notelett.notes.list ─────────────────────────────────────────────────

registerTool({
  name: 'notelett.notes.list',
  description:
    'List notes in a workspace. Supports text search, pagination. Returns id, title, status, createdAt. Requires admin role.',
  requiredRole: 'admin',
  inputSchema: z.object({
    workspaceId: z.string().min(1).describe('Workspace ID'),
    search: z.string().optional().describe('Full-text search query'),
    limit: z.coerce.number().min(1).max(config.QUERY_MAX_LIMIT).default(config.QUERY_DEFAULT_LIMIT),
    offset: z.coerce.number().min(0).default(0),
  }),
  async execute(args, req) {
    return noteLettNotesList(args, { token: tokenOf(req), requestId: req.id });
  },
});

// ── notelett.notes.get ──────────────────────────────────────────────────

registerTool({
  name: 'notelett.notes.get',
  description:
    'Get a single note by ID including full content, status, and timestamps. Requires admin role.',
  requiredRole: 'admin',
  inputSchema: z.object({
    noteId: z.string().min(1).describe('Note ID'),
    workspaceId: z.string().min(1).describe('Workspace ID the note belongs to'),
  }),
  async execute(args, req) {
    return noteLettNoteGet(args.noteId, args.workspaceId, { token: tokenOf(req), requestId: req.id });
  },
});

// ── notelett.notes.create ───────────────────────────────────────────────

registerTool({
  name: 'notelett.notes.create',
  description:
    'Create a new note in a workspace. Returns the new note document. Requires admin role.',
  requiredRole: 'admin',
  inputSchema: z.object({
    workspaceId: z.string().min(1).describe('Workspace ID'),
    title: z.string().min(1).describe('Note title'),
    content: z.string().optional().describe('Initial note content (plain text or HTML)'),
  }),
  async execute(args, req) {
    return noteLettNoteCreate(args, { token: tokenOf(req), requestId: req.id });
  },
});

// ── notelett.notes.update ───────────────────────────────────────────────

registerTool({
  name: 'notelett.notes.update',
  description:
    'Update an existing note (title and/or content). Returns the updated document. Requires admin role.',
  requiredRole: 'admin',
  inputSchema: z.object({
    noteId: z.string().min(1).describe('Note ID'),
    workspaceId: z.string().min(1).describe('Workspace ID the note belongs to'),
    title: z.string().optional().describe('New title'),
    content: z.string().optional().describe('New content'),
  }),
  async execute(args, req) {
    const { noteId, workspaceId, ...updates } = args;
    return noteLettNoteUpdate(noteId, workspaceId, updates, { token: tokenOf(req), requestId: req.id });
  },
});

// ── notelett.notes.delete ───────────────────────────────────────────────

registerTool({
  name: 'notelett.notes.delete',
  description:
    'Soft-delete a note (sets status to archived). Requires admin role.',
  requiredRole: 'admin',
  inputSchema: z.object({
    noteId: z.string().min(1).describe('Note ID'),
    workspaceId: z.string().min(1).describe('Workspace ID the note belongs to'),
  }),
  async execute(args, req) {
    await noteLettNoteDelete(args.noteId, args.workspaceId, { token: tokenOf(req), requestId: req.id });
    return { success: true };
  },
});

// ── notelett.notes.summarize ────────────────────────────────────────────

registerTool({
  name: 'notelett.notes.summarize',
  description:
    'Generate an AI summary of a note using the extraction service. Returns { summary }. Requires admin role.',
  requiredRole: 'admin',
  inputSchema: z.object({
    noteId: z.string().min(1).describe('Note ID'),
    workspaceId: z.string().min(1).describe('Workspace ID the note belongs to'),
  }),
  async execute(args, req) {
    return noteLettNoteSummarize(args.noteId, args.workspaceId, { token: tokenOf(req), requestId: req.id });
  },
});

// ── notelett.workspaces.list ────────────────────────────────────────────

registerTool({
  name: 'notelett.workspaces.list',
  description:
    'List all workspaces accessible to the authenticated user. Returns id, name, description, createdAt. Requires admin role.',
  requiredRole: 'admin',
  inputSchema: z.object({}),
  async execute(_args, req) {
    return noteLettWorkspacesList({ token: tokenOf(req), requestId: req.id });
  },
});

// ── notelett.workspaces.create ──────────────────────────────────────────

registerTool({
  name: 'notelett.workspaces.create',
  description:
    'Create a new workspace. Returns the new workspace document. Requires admin role.',
  requiredRole: 'admin',
  inputSchema: z.object({
    name: z.string().min(1).describe('Workspace name'),
    description: z.string().optional().describe('Workspace description'),
  }),
  async execute(args, req) {
    return noteLettWorkspaceCreate(args, { token: tokenOf(req), requestId: req.id });
  },
});

// ── notelett.notes.tasks ────────────────────────────────────────────────

registerTool({
  name: 'notelett.notes.tasks',
  description:
    'List tasks extracted from a note. Returns id, title, status, createdAt. Requires admin role.',
  requiredRole: 'admin',
  inputSchema: z.object({
    noteId: z.string().min(1).describe('Note ID'),
  }),
  async execute(args, req) {
    return noteLettNoteTasksList(args.noteId, { token: tokenOf(req), requestId: req.id });
  },
});

// ── notelett.notes.artifacts ────────────────────────────────────────────

registerTool({
  name: 'notelett.notes.artifacts',
  description:
    'List artifacts associated with a note (file uploads, links, etc). Requires admin role.',
  requiredRole: 'admin',
  inputSchema: z.object({
    noteId: z.string().min(1).describe('Note ID'),
  }),
  async execute(args, req) {
    return noteLettNoteArtifactsList(args.noteId, { token: tokenOf(req), requestId: req.id });
  },
});
