import React, { useState, useEffect } from 'react'; import { useNavigate, useLocation } from 'react-router-dom'; import { useAuth } from '../contexts/AuthContext'; import { useOrg } from '../contexts/OrgContext'; import { Card, Button } from '../components/UiKit'; import { EMPLOYEE_QUESTIONS, EmployeeSubmissionAnswers } from '../employeeQuestions'; import { Question } from '../components/ui/Question'; import { QuestionInput } from '../components/ui/QuestionInput'; import { FigmaQuestion } from '../components/figma/FigmaQuestion'; import { EnhancedFigmaQuestion } from '../components/figma/EnhancedFigmaQuestion'; import { LinearProgress } from '../components/ui/Progress'; import { Alert } from '../components/ui/Alert'; const EmployeeQuestionnaireSteps: React.FC = () => { const navigate = useNavigate(); const location = useLocation(); const { user } = useAuth(); const { submitEmployeeAnswers, generateEmployeeReport, employees } = useOrg(); const [answers, setAnswers] = useState({}); const [currentStep, setCurrentStep] = useState(0); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(''); // Get employee info from multiple sources const invitedEmployee = location.state?.invitedEmployee; // Find current employee info - try multiple strategies let currentEmployee = invitedEmployee || employees.find(emp => emp.email === user?.email); // Additional matching strategies for edge cases if (!currentEmployee && user?.email) { // Try case-insensitive email matching currentEmployee = employees.find(emp => emp.email?.toLowerCase() === user.email?.toLowerCase() ); // Try matching by name if email doesn't work (for invite flow) if (!currentEmployee && invitedEmployee) { currentEmployee = employees.find(emp => emp.name === invitedEmployee.name || emp.id === invitedEmployee.id ); } } // Demo mode fallbacks if (!currentEmployee && user?.email === 'demo@auditly.local' && employees.length > 0) { currentEmployee = employees[employees.length - 1]; } if (!currentEmployee && employees.length === 1) { currentEmployee = employees[0]; } // Filter out followup questions that shouldn't be shown yet const getVisibleQuestions = () => { return EMPLOYEE_QUESTIONS.filter(question => { if (!question.followupTo) return true; const parentAnswer = answers[question.followupTo]; if (question.followupTo === 'has_kpis') { return parentAnswer === 'Yes'; } if (question.followupTo === 'unclear_responsibilities') { return parentAnswer === 'Yes'; } if (question.followupTo === 'role_shift_interest') { return parentAnswer === 'Yes'; } return false; }); }; const visibleQuestions = getVisibleQuestions(); const currentQuestion = visibleQuestions[currentStep]; const handleAnswerChange = (value: string) => { if (currentQuestion) { setAnswers(prev => ({ ...prev, [currentQuestion.id]: value })); } }; const handleNext = () => { if (currentStep < visibleQuestions.length - 1) { setCurrentStep(prev => prev + 1); } else { handleSubmit(); } }; const handleBack = () => { if (currentStep > 0) { setCurrentStep(prev => prev - 1); } }; const handleSubmit = async () => { setIsSubmitting(true); setError(''); try { // Validate required questions const requiredQuestions = visibleQuestions.filter(q => q.required); const missingAnswers = requiredQuestions.filter(q => !answers[q.id]?.trim()); if (missingAnswers.length > 0) { setError(`Please answer all required questions: ${missingAnswers.map(q => q.prompt).join(', ')}`); setIsSubmitting(false); return; } if (!currentEmployee) { // Enhanced fallback logic if (employees.length > 0) { // Try to find employee by matching with the user's email more aggressively let fallbackEmployee = employees.find(emp => emp.email?.toLowerCase().includes(user?.email?.toLowerCase().split('@')[0] || '') ); // If still no match, use the most recent employee or one with matching domain if (!fallbackEmployee) { const userDomain = user?.email?.split('@')[1]; fallbackEmployee = employees.find(emp => emp.email?.split('@')[1] === userDomain ) || employees[employees.length - 1]; } console.log('Using enhanced fallback employee:', fallbackEmployee); const success = await submitEmployeeAnswers(fallbackEmployee.id, answers); if (success) { try { const report = await generateEmployeeReport(fallbackEmployee); if (report) { console.log('Report generated successfully for fallback employee:', report); } } catch (reportError) { console.error('Failed to generate report for fallback employee:', reportError); } navigate('/questionnaire-complete', { replace: true, state: { employeeId: fallbackEmployee.id, employeeName: fallbackEmployee.name, message: 'Questionnaire submitted successfully! Your responses have been recorded.' } }); return; } } setError(`We couldn't match your account (${user?.email}) with an employee record. Please contact your administrator to ensure your invite was set up correctly.`); setIsSubmitting(false); return; } const result = await submitEmployeeAnswers(currentEmployee.id, answers); if (result.success) { const message = result.reportGenerated ? 'Questionnaire submitted successfully! Your AI-powered performance report has been generated.' : 'Questionnaire submitted successfully! Your report will be available shortly.'; navigate('/questionnaire-complete', { state: { employeeId: currentEmployee.id, employeeName: currentEmployee.name, reportGenerated: result.reportGenerated, message: message } }); } else { setError(result.message || 'Failed to submit questionnaire'); } } catch (error) { console.error('Submission error:', error); setError('Failed to submit questionnaire. Please try again.'); } finally { setIsSubmitting(false); } }; const getProgressPercentage = () => { return Math.round(((currentStep + 1) / visibleQuestions.length) * 100); }; const isCurrentQuestionAnswered = () => { if (!currentQuestion) return false; const answer = answers[currentQuestion.id]; return answer && answer.trim().length > 0; }; if (!currentQuestion) { return (

No questions available

); } return (
{/* Header */}
A

Welcome to Auditly!

Please complete this questionnaire to help us understand your role and create personalized insights.

{currentEmployee ? (
👋 Hello {currentEmployee.name}! {currentEmployee.role && `(${currentEmployee.role})`}
) : (
⚠️ Employee info not found. User: {user?.email}, Employees: {employees.length}

Don't worry - your account was created successfully! This is likely a temporary sync issue.

You can still complete the questionnaire, and we'll match it to your profile automatically.

)}
{/* Progress Bar */}
Question {currentStep + 1} of {visibleQuestions.length} {getProgressPercentage()}%
{/* Enhanced Question Card */}
0 ? handleBack : undefined} onNext={ currentStep < visibleQuestions.length - 1 ? handleNext : (isCurrentQuestionAnswered() || !currentQuestion.required) ? handleSubmit : undefined } nextLabel={ currentStep < visibleQuestions.length - 1 ? 'Next' : isSubmitting ? 'Submitting...' : 'Submit & Generate Report' } showNavigation={true} />
{/* Original Figma Question Card for Comparison */}
0 ? handleBack : undefined} onNext={ currentStep < visibleQuestions.length - 1 ? handleNext : (isCurrentQuestionAnswered() || !currentQuestion.required) ? handleSubmit : undefined } nextLabel={ currentStep < visibleQuestions.length - 1 ? 'Next' : isSubmitting ? 'Submitting...' : 'Submit & Generate Report' } showNavigation={true} />
{/* Alternative Input for Different Question Types */}
{/* Error Display */} {error && (
{error}
)} {/* Navigation Buttons */}
{currentStep > 0 && ( )} {currentStep < visibleQuestions.length - 1 ? ( ) : ( )}
{/* Help Text */}

{currentQuestion.required && !isCurrentQuestionAnswered() ? 'Please answer this required question to continue.' : 'You can skip optional questions or come back to them later.' }

); }; export default EmployeeQuestionnaireSteps;