/**
 * Context Injector Extension — context hook example
 *
 * Demonstrates: Context hook to inject system messages before LLM
 *
 * Usage: xopc extension install ./examples/extensions/context-injector
 */

import type { ExtensionApi } from 'xopc/extension-sdk';

export default function(api: ExtensionApi) {
  const config = api.extensionConfig;
  const injectedContext = (config.injectedContext as string) || 'Remember to be concise.';

  api.logger.info('Context Injector extension registered!');

  // Context Hook: Inject system messages before sending to LLM
  api.registerHook('context', async (event) => {
    const messages = [...(event as any).messages];

    // Find last system message position
    let lastSystemIndex = -1;
    for (let i = messages.length - 1; i >= 0; i--) {
      if (messages[i].role === 'system') {
        lastSystemIndex = i;
        break;
      }
    }

    // Inject context after last system message
    if (lastSystemIndex >= 0) {
      messages.splice(lastSystemIndex + 1, 0, {
        role: 'system',
        content: `[Extension Context] ${injectedContext}`,
      });
    } else {
      messages.unshift({
        role: 'system',
        content: `[Extension Context] ${injectedContext}`,
      });
    }

    api.logger.debug(`Injected context into ${messages.length} messages`);
    return { messages };
  });

  // Turn lifecycle tracking
  api.registerHook('turn_start', () => {
    api.logger.debug('New conversation turn started');
  });

  api.registerHook('turn_end', () => {
    api.logger.debug('Conversation turn ended');
  });
}
