"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.register = register;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const zod_1 = require("zod");
const defaults_1 = require("../../../lib/defaults");
function register(server, mgr) {
    server.registerTool('tasks_add_attachment', {
        description: 'Attach a file to a task. Provide the absolute path to a local file. ' +
            'The file is copied into the task directory (.tasks/{taskId}/). ' +
            'Returns attachment metadata (filename, mimeType, size).',
        inputSchema: {
            taskId: zod_1.z.string().min(1).max(500).describe('Task ID to attach the file to (slug)'),
            filePath: zod_1.z.string().min(1).max(4096).describe('Absolute path to the file on disk'),
        },
    }, async ({ taskId, filePath }) => {
        const resolved = path_1.default.resolve(filePath);
        const projectDir = mgr.projectDir;
        if (!projectDir) {
            return { content: [{ type: 'text', text: JSON.stringify({ error: 'No project directory configured' }) }], isError: true };
        }
        let realResolved;
        try {
            realResolved = fs_1.default.realpathSync(resolved);
        }
        catch {
            return { content: [{ type: 'text', text: JSON.stringify({ error: 'File not found' }) }], isError: true };
        }
        let realProject;
        try {
            realProject = fs_1.default.realpathSync(path_1.default.resolve(projectDir));
        }
        catch {
            return { content: [{ type: 'text', text: JSON.stringify({ error: 'Project directory not found' }) }], isError: true };
        }
        const normalizedProject = realProject + path_1.default.sep;
        if (!realResolved.startsWith(normalizedProject) && realResolved !== realProject) {
            return { content: [{ type: 'text', text: JSON.stringify({ error: 'File path must be within the project directory' }) }], isError: true };
        }
        let stat;
        try {
            stat = fs_1.default.statSync(realResolved);
        }
        catch {
            return { content: [{ type: 'text', text: JSON.stringify({ error: 'File not found' }) }], isError: true };
        }
        if (!stat.isFile()) {
            return { content: [{ type: 'text', text: JSON.stringify({ error: 'Path is not a regular file' }) }], isError: true };
        }
        if (stat.size > defaults_1.MAX_UPLOAD_SIZE) {
            return { content: [{ type: 'text', text: JSON.stringify({ error: 'File exceeds 50 MB limit' }) }], isError: true };
        }
        const data = fs_1.default.readFileSync(realResolved);
        const filename = path_1.default.basename(resolved);
        const meta = mgr.addAttachment(taskId, filename, data);
        if (!meta) {
            return { content: [{ type: 'text', text: JSON.stringify({ error: 'Task not found or no project dir' }) }], isError: true };
        }
        return { content: [{ type: 'text', text: JSON.stringify(meta, null, 2) }] };
    });
}
