File size: 2,968 Bytes
1906404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { generateInterviewQuestions, evaluateInterview, chatInterview } from '../services/aiService.js';
import { saveInterview, getUserInterviews } from '../services/firebaseService.js';

/**
 * POST /api/interview/start
 * Body: { type, resumeText, jobRole, difficulty }
 * Generates interview questions using AI.
 */
export async function startInterview(req, res) {
  try {
    const { type, resumeText, jobRole, difficulty } = req.body;

    if (!type) {
      return res.status(400).json({ error: 'Interview type is required.' });
    }

    const validTypes = ['resume', 'dsa', 'hr', 'combined'];
    if (!validTypes.includes(type)) {
      return res.status(400).json({ error: `Invalid type. Must be one of: ${validTypes.join(', ')}` });
    }

    const result = await generateInterviewQuestions(type, { resumeText, jobRole, difficulty });
    res.json({ type, ...result });
  } catch (error) {
    console.error('Start interview error:', error);
    res.status(500).json({ error: 'Failed to generate interview questions.' });
  }
}

/**
 * POST /api/interview/submit
 * Body: { type, questions, answers, jobRole }
 * Evaluates answers and stores the interview record.
 */
export async function submitInterview(req, res) {
  try {
    const { type, questions, answers, jobRole } = req.body;

    if (!type || !questions || !answers) {
      return res.status(400).json({ error: 'type, questions, and answers are required.' });
    }

    const evaluation = await evaluateInterview(type, questions, answers, { jobRole });

    // Save to Firestore
    const interviewId = await saveInterview(req.user.uid, {
      type,
      questions,
      answers,
      score: evaluation.score,
      feedback: evaluation.feedback,
      strengths: evaluation.strengths,
      weaknesses: evaluation.weaknesses,
      advice: evaluation.advice,
    });

    res.json({
      interviewId,
      ...evaluation,
    });
  } catch (error) {
    console.error('Submit interview error:', error);
    res.status(500).json({ error: 'Failed to evaluate interview.' });
  }
}

/**
 * GET /api/interview/history
 * Returns all interviews for the authenticated user.
 */
export async function getHistory(req, res) {
  try {
    const interviews = await getUserInterviews(req.user.uid);
    res.json({ interviews });
  } catch (error) {
    console.error('Get history error:', error);
    res.status(500).json({ error: 'Failed to fetch interview history.' });
  }
}

/**
 * POST /api/interview/chat
 * Body: { history }
 * Continues an interview conversation (for voice interview mode).
 */
export async function chat(req, res) {
  try {
    const { history } = req.body;

    if (!history) {
      return res.status(400).json({ error: 'Chat history is required.' });
    }

    const message = await chatInterview(history);
    res.json({ message });
  } catch (error) {
    console.error('Chat error:', error);
    res.status(500).json({ error: 'Failed to get AI response.' });
  }
}