"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.register = register;
const zod_1 = require("zod");
const manager_types_1 = require("../../../graphs/manager-types");
const defaults_1 = require("../../../lib/defaults");
function register(server, mgr) {
    server.registerTool('tasks_update', {
        description: 'Update an existing task. Only provided fields are changed. ' +
            'Re-embeds automatically when title or description changes. ' +
            'Status changes auto-manage completedAt (set on done/cancelled, cleared on reopen). ' +
            'Use move_task for a simpler status-only change. ' +
            'Pass expectedVersion to enable optimistic locking.',
        inputSchema: {
            taskId: zod_1.z.string().min(1).max(500).describe('Task ID to update (slug, e.g. "fix-auth-redirect-loop")'),
            title: zod_1.z.string().max(defaults_1.MAX_TITLE_LEN).optional().describe('New title'),
            description: zod_1.z.string().max(defaults_1.MAX_DESCRIPTION_LEN).optional().describe('New description (markdown)'),
            status: zod_1.z.enum(['backlog', 'todo', 'in_progress', 'review', 'done', 'cancelled']).optional()
                .describe('New status: "backlog", "todo", "in_progress", "review", "done", or "cancelled"'),
            priority: zod_1.z.enum(['critical', 'high', 'medium', 'low']).optional().describe('New priority: "critical", "high", "medium", or "low"'),
            tags: zod_1.z.array(zod_1.z.string().max(defaults_1.MAX_TAG_LEN)).max(defaults_1.MAX_TAGS_COUNT).optional().describe('Replace entire tags array — include all tags you want to keep'),
            dueDate: zod_1.z.number().nullable().optional().describe('Due date as Unix timestamp in ms, or null to clear'),
            estimate: zod_1.z.number().nullable().optional().describe('Effort estimate in hours, or null to clear'),
            assignee: zod_1.z.string().max(defaults_1.MAX_ASSIGNEE_LEN).nullable().optional().describe('Team member ID to assign, or null to unassign'),
            expectedVersion: zod_1.z.number().int().positive().optional().describe('Current version for optimistic locking — request fails with version_conflict if the task has been updated since'),
        },
    }, async ({ taskId, title, description, status, priority, tags, dueDate, estimate, assignee, expectedVersion }) => {
        const patch = {};
        if (title !== undefined)
            patch.title = title;
        if (description !== undefined)
            patch.description = description;
        if (status !== undefined)
            patch.status = status;
        if (priority !== undefined)
            patch.priority = priority;
        if (tags !== undefined)
            patch.tags = tags;
        if (dueDate !== undefined)
            patch.dueDate = dueDate;
        if (estimate !== undefined)
            patch.estimate = estimate;
        if (assignee !== undefined)
            patch.assignee = assignee;
        try {
            const updated = await mgr.updateTask(taskId, patch, expectedVersion);
            if (!updated) {
                return { content: [{ type: 'text', text: 'Task not found' }], isError: true };
            }
            return { content: [{ type: 'text', text: JSON.stringify({ taskId, updated: true }, null, 2) }] };
        }
        catch (err) {
            if (err instanceof manager_types_1.VersionConflictError) {
                return { content: [{ type: 'text', text: JSON.stringify({ error: 'version_conflict', current: err.current, expected: err.expected }) }], isError: true };
            }
            throw err;
        }
    });
}
