import { Injectable } from '@nestjs/common';
import { AiGatewayService } from '../gateway/ai-gateway.service';
import { FeynmanEvaluationResultSchema } from '../prompts/schemas/feynman-evaluation.schema';
import type { FeynmanEvaluationResult } from '../prompts/schemas/feynman-evaluation.schema';

export interface FeynmanEvaluationInput {
  userId: string;
  knowledgeItemTitle: string;
  knowledgeItemContent: string;
  userExplanation: string;
}

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

  async execute(input: FeynmanEvaluationInput): Promise<FeynmanEvaluationResult> {
    const userMessage = [
      `【知识点标题】`,
      input.knowledgeItemTitle,
      '',
      `【知识点原文】`,
      input.knowledgeItemContent,
      '',
      `【用户的费曼解释】`,
      input.userExplanation,
      '',
      `请评估以上费曼解释的质量，严格按照 JSON Schema 输出。`,
    ].join('\n');

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

    return response.parsed as unknown as FeynmanEvaluationResult;
  }
}
