import { Injectable } from '@nestjs/common';
import { AiGatewayService } from '../gateway/ai-gateway.service';
import { ReviewCardGenerationResultSchema } from '../prompts/schemas/review-card-generation.schema';
import type { ReviewCardGenerationResult } from '../prompts/schemas/review-card-generation.schema';

export interface ReviewCardGenerationInput {
  userId: string;
  knowledgeItemTitle: string;
  knowledgeItemContent: string;
  cardCount?: number;
}

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

  async execute(input: ReviewCardGenerationInput): Promise<ReviewCardGenerationResult> {
    const userMessage = [
      `【知识点标题】`,
      input.knowledgeItemTitle,
      '',
      `【知识点内容】`,
      input.knowledgeItemContent,
      '',
      input.cardCount
        ? `请为以上知识点生成 ${input.cardCount} 张复习卡片。`
        : '请为以上知识点生成合适的复习卡片。',
    ].join('\n');

    const response = await this.gateway.generate({
      feature: 'review-card-generation',
      userId: input.userId,
      tier: 'cheap',
      promptKey: 'review-card-generation',
      promptVersion: '1.0.0',
      messages: [
        { role: 'user', content: userMessage },
      ],
      outputSchema: ReviewCardGenerationResultSchema,
    });

    return response.parsed as unknown as ReviewCardGenerationResult;
  }
}
