import { createRule, getRule, updateRule, deleteRule, listRules, applyRule, exportRule, } from '../engine/global-rules-manager.js';
// ---------------------------------------------------------------------------
// manage_global_rules handler
// ---------------------------------------------------------------------------
export async function handleManageGlobalRules(input) {
    switch (input.action) {
        case 'create': {
            if (input.slug === undefined || input.name === undefined || input.content === undefined) {
                return {
                    content: [
                        {
                            type: 'text',
                            text: 'Error: slug, name, and content are required for create action.',
                        },
                    ],
                    isError: true,
                };
            }
            const rule = await createRule(input.slug, input.name, input.content, input.tags ?? [], input.author, input.version);
            return {
                content: [
                    {
                        type: 'text',
                        text: `Global rule "${rule.name}" created (slug: ${rule.slug}).\n\nVersion: ${rule.version} | Tags: ${rule.tags.join(', ') || 'none'}`,
                    },
                ],
            };
        }
        case 'list': {
            const summary = await listRules();
            if (summary.totalRules === 0) {
                return { content: [{ type: 'text', text: 'No global rules found.' }] };
            }
            const lines = [`**Global Rules** (${summary.totalRules})`, ''];
            for (const r of summary.rules) {
                const tags = r.tags.length > 0 ? ` [${r.tags.join(', ')}]` : '';
                lines.push(`- **${r.slug}**: ${r.name} v${r.version}${tags}`);
            }
            return { content: [{ type: 'text', text: lines.join('\n') }] };
        }
        case 'get': {
            if (input.slug === undefined) {
                return {
                    content: [{ type: 'text', text: 'Error: slug is required for get action.' }],
                    isError: true,
                };
            }
            const rule = await getRule(input.slug);
            if (rule === null) {
                return {
                    content: [{ type: 'text', text: `Rule "${input.slug}" not found.` }],
                    isError: true,
                };
            }
            const lines = [
                `**${rule.name}** (${rule.slug})`,
                '',
                `Version: ${rule.version} | Tags: ${rule.tags.join(', ') || 'none'}`,
                `Author: ${rule.author ?? 'unknown'} | Created: ${rule.createdAt}`,
                '',
                '---',
                rule.content,
            ];
            return { content: [{ type: 'text', text: lines.join('\n') }] };
        }
        case 'update': {
            if (input.slug === undefined) {
                return {
                    content: [{ type: 'text', text: 'Error: slug is required for update action.' }],
                    isError: true,
                };
            }
            const updated = await updateRule(input.slug, {
                name: input.name,
                content: input.content,
                tags: input.tags,
                version: input.version,
                author: input.author,
            });
            if (updated === null) {
                return {
                    content: [{ type: 'text', text: `Rule "${input.slug}" not found.` }],
                    isError: true,
                };
            }
            return {
                content: [
                    { type: 'text', text: `Rule "${updated.slug}" updated at ${updated.updatedAt}.` },
                ],
            };
        }
        case 'delete': {
            if (input.slug === undefined) {
                return {
                    content: [{ type: 'text', text: 'Error: slug is required for delete action.' }],
                    isError: true,
                };
            }
            const deleted = await deleteRule(input.slug);
            if (!deleted) {
                return {
                    content: [{ type: 'text', text: `Rule "${input.slug}" not found — nothing to delete.` }],
                };
            }
            return { content: [{ type: 'text', text: `Rule "${input.slug}" deleted.` }] };
        }
        case 'apply': {
            if (input.slug === undefined || input.projectPath === undefined) {
                return {
                    content: [
                        {
                            type: 'text',
                            text: 'Error: slug and projectPath are required for apply action.',
                        },
                    ],
                    isError: true,
                };
            }
            try {
                const result = await applyRule(input.slug, input.projectPath);
                return {
                    content: [
                        {
                            type: 'text',
                            text: `Rule "${result.name}" applied to ${result.appliedTo} at ${result.appliedAt}.`,
                        },
                    ],
                };
            }
            catch (err) {
                return {
                    content: [
                        {
                            type: 'text',
                            text: `Error: ${err instanceof Error ? err.message : String(err)}`,
                        },
                    ],
                    isError: true,
                };
            }
        }
        case 'export': {
            if (input.slug === undefined) {
                return {
                    content: [{ type: 'text', text: 'Error: slug is required for export action.' }],
                    isError: true,
                };
            }
            try {
                const content = await exportRule(input.slug);
                return { content: [{ type: 'text', text: content }] };
            }
            catch (err) {
                return {
                    content: [
                        {
                            type: 'text',
                            text: `Error: ${err instanceof Error ? err.message : String(err)}`,
                        },
                    ],
                    isError: true,
                };
            }
        }
    }
}
export async function handleGlobalRulesStatus(_input) {
    const summary = await listRules();
    const lines = ['**Global Rules Status**', '', `Total rules: ${summary.totalRules}`];
    if (summary.totalRules > 0) {
        lines.push('');
        for (const r of summary.rules) {
            lines.push(`- ${r.slug}: ${r.name} (v${r.version})`);
        }
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
//# sourceMappingURL=global-rules-handler.js.map