import { Injectable } from '@nestjs/common';
import { AiGatewayService } from '../gateway/ai-gateway.service';
import { ActiveRecallAnalysisResultSchema } from '../prompts/schemas/active-recall-analysis.schema';
import type { ActiveRecallAnalysisResult } from '../prompts/schemas/active-recall-analysis.schema';

export interface ActiveRecallAnalysisInput {
  userId: string;
  questionText: string;
  knowledgeItemContent: string;
  userAnswer: string;
}

@Injectable()
export class ActiveRecallAnalysisWorkflow {
  constructor(private readonly gateway: AiGatewayService) {}

  async execute(input: ActiveRecallAnalysisInput): Promise<ActiveRecallAnalysisResult> {
    const userMessage = [
      `【知识点原文】`,
      input.knowledgeItemContent,
      '',
      `【用户的主动回忆回答】`,
      input.userAnswer,
      '',
      `请根据以上内容进行分析。`,
    ].join('\n');

    const response = await this.gateway.generate({
      feature: 'active-recall-analysis',
      userId: input.userId,
      tier: 'primary',
      promptKey: 'active-recall-analysis',
      promptVersion: '1.0.0',
      messages: [
        { role: 'user', content: userMessage },
      ],
      outputSchema: ActiveRecallAnalysisResultSchema,
    });

    return response.parsed as unknown as ActiveRecallAnalysisResult;
  }
}
