export interface ActionKeyword {
    action: string;
    target: string;
    description: string;
}

export const ACTION_KEYWORDS: Record<string, string[]> = {
    create: ['create', 'new', 'make'],
    delete: ['delete', 'remove', 'drop'],
    update: ['update', 'modify', 'change', 'edit'],
    show: ['show', 'display', 'list', 'get', 'fetch', 'view'],
    search: ['search', 'find', 'query', 'filter'],
    view: ['view', 'details', 'info', 'expand']
};

export const detectActions = (prompt: string, keywords: string[]): ActionKeyword[] => {
    const actions: ActionKeyword[] = [];
    const promptLower = prompt.toLowerCase().trim();

    // If prompt starts with 'add', treat as 'add' action
    if (promptLower.startsWith('add')) {
        // Try to match 'add ... to [id] slash [path]'
        const match = promptLower.match(/add.*to\s+(\d+)\s+slash\s+([^\s]+)/i);
        if (match) {
            const [, id, pathname] = match;
            actions.push({
                action: 'add',
                target: `id:${id} path:/${pathname.replace(/^\/+|\/+$/g, '')}`,
                description: `add /${pathname.replace(/^\/+|\/+$/g, '')} to endpoint ID ${id}`
            });
        } else {
            // Fallback: just mark as a generic add action
            actions.push({
                action: 'add',
                target: '',
                description: 'add action'
            });
        }
    }

    // First check for explicit action-target pairs
    const words = promptLower.split(' ');
    for (let i = 0; i < words.length - 1; i++) {
        const word = words[i];
        const nextWord = words[i + 1];
        for (const [action, triggers] of Object.entries(ACTION_KEYWORDS)) {
            if (triggers.includes(word)) {
                actions.push({
                    action,
                    target: nextWord,
                    description: `${action} ${nextWord}`
                });
            }
        }
    }

    // Then check keywords for additional context
    keywords.forEach(keyword => {
        const keywordWords = keyword.toLowerCase().split(' ');
        for (const [action, triggers] of Object.entries(ACTION_KEYWORDS)) {
            if (triggers.some(trigger => keywordWords.includes(trigger))) {
                const target = keywordWords.find(word => !triggers.includes(word));
                if (target) {
                    actions.push({
                        action,
                        target,
                        description: `${action} ${target}`
                    });
                }
            }
        }
    });

    return actions;
}; 