import { Injectable } from '@nestjs/common';
import { ACTIVE_RECALL_ANALYSIS_SYSTEM_PROMPT } from './active-recall-analysis.prompt';
import { ACTIVE_RECALL_OUTPUT_SCHEMA_DESC } from './schemas/active-recall-analysis.schema';
import { FEYNMAN_EVALUATION_SYSTEM_PROMPT } from './feynman-evaluation.prompt';
import { FEYNMAN_OUTPUT_SCHEMA_DESC } from './schemas/feynman-evaluation.schema';
import { KNOWLEDGE_IMPORT_SYSTEM_PROMPT } from './knowledge-import.prompt';
import { KNOWLEDGE_IMPORT_OUTPUT_SCHEMA_DESC } from './schemas/knowledge-import.schema';
import { REVIEW_CARD_GENERATION_SYSTEM_PROMPT } from './review-card-generation.prompt';
import { REVIEW_CARD_OUTPUT_SCHEMA_DESC } from './schemas/review-card-generation.schema';
import { LEARNING_TREND_SYSTEM_PROMPT } from './learning-trend.prompt';
import { LEARNING_TREND_OUTPUT_SCHEMA_DESC } from './schemas/learning-trend.schema';
import { RAG_CHAT_SYSTEM_PROMPT } from './rag-chat.prompt';
import { RAG_CHAT_OUTPUT_SCHEMA_DESC } from './schemas/rag-chat.schema';
import { QUIZ_GENERATION_SYSTEM_PROMPT } from './quiz-generation.prompt';
import { QUIZ_OUTPUT_SCHEMA_DESC } from './schemas/quiz-generation.schema';
import { LEARNING_ANALYSIS_SYSTEM_PROMPT, LEARNING_ANALYSIS_OUTPUT_DESC } from './learning-analysis.prompt';

export interface PromptTemplate {
  key: string;
  version: string;
  systemPrompt: string;
  outputSchemaDesc: string;
}

@Injectable()
export class PromptTemplateService {
  private readonly templates: Map<string, PromptTemplate> = new Map();

  constructor() {
    this.register({
      key: 'active-recall-analysis',
      version: '1.0.0',
      systemPrompt: ACTIVE_RECALL_ANALYSIS_SYSTEM_PROMPT,
      outputSchemaDesc: ACTIVE_RECALL_OUTPUT_SCHEMA_DESC,
    });
    this.register({
      key: 'feynman-evaluation',
      version: '1.0.0',
      systemPrompt: FEYNMAN_EVALUATION_SYSTEM_PROMPT,
      outputSchemaDesc: FEYNMAN_OUTPUT_SCHEMA_DESC,
    });
    this.register({
      key: 'knowledge-import',
      version: '1.0.0',
      systemPrompt: KNOWLEDGE_IMPORT_SYSTEM_PROMPT,
      outputSchemaDesc: KNOWLEDGE_IMPORT_OUTPUT_SCHEMA_DESC,
    });
    this.register({
      key: 'review-card-generation',
      version: '1.0.0',
      systemPrompt: REVIEW_CARD_GENERATION_SYSTEM_PROMPT,
      outputSchemaDesc: REVIEW_CARD_OUTPUT_SCHEMA_DESC,
    });
    this.register({
      key: 'learning-trend',
      version: '1.0.0',
      systemPrompt: LEARNING_TREND_SYSTEM_PROMPT,
      outputSchemaDesc: LEARNING_TREND_OUTPUT_SCHEMA_DESC,
    });
    this.register({
      key: 'rag-chat',
      version: 'v1',
      systemPrompt: RAG_CHAT_SYSTEM_PROMPT,
      outputSchemaDesc: RAG_CHAT_OUTPUT_SCHEMA_DESC,
    });
    this.register({
      key: 'quiz-generation',
      version: '1.0.0',
      systemPrompt: QUIZ_GENERATION_SYSTEM_PROMPT,
      outputSchemaDesc: QUIZ_OUTPUT_SCHEMA_DESC,
    });
    this.register({
      key: 'learning-analysis',
      version: '1.0.0',
      systemPrompt: LEARNING_ANALYSIS_SYSTEM_PROMPT,
      outputSchemaDesc: LEARNING_ANALYSIS_OUTPUT_DESC,
    });
  }

  get(key: string, version?: string): PromptTemplate {
    const template = this.templates.get(key);
    if (!template) {
      throw new Error(`Prompt template not found: ${key}`);
    }
    if (version && template.version !== version) {
      throw new Error(`Prompt version mismatch for ${key}: requested ${version}, have ${template.version}`);
    }
    return template;
  }

  private register(template: PromptTemplate): void {
    this.templates.set(template.key, template);
  }
}
