'use client';
import { useState } from 'react';
import {
Box,
Stepper,
Step,
StepLabel,
Button,
Typography,
Paper,
TextField,
Avatar,
IconButton,
Alert,
CircularProgress,
MenuItem,
Card,
CardActionArea,
CardContent,
Radio,
RadioGroup,
FormControlLabel,
FormControl,
Grid,
StepConnector,
stepConnectorClasses,
styled,
} from '@mui/material';
import { ArrowBack, ArrowForward, Check, Language, Straighten } from '@mui/icons-material';
import { motion, AnimatePresence } from 'framer-motion';
import { useRouter } from 'next/navigation';
import { useAuth } from '@/lib/auth/AuthContext';
import { childrenApi } from '@/lib/api/children';
import { useLocale, MeasurementSystem } from '@/hooks/useLocale';
import { useTranslation } from '@/hooks/useTranslation';
import { supportedLanguages } from '@/lib/i18n/config';
import { usersApi } from '@/lib/api/users';
import { useTheme } from '@mui/material/styles';
import { StepIconProps } from '@mui/material/StepIcon';
const steps = ['Welcome', 'Language', 'Measurements', 'Add Child', 'Complete'];
// Custom connector for mobile-friendly stepper
const CustomConnector = styled(StepConnector)(({ theme }) => ({
[`&.${stepConnectorClasses.active}`]: {
[`& .${stepConnectorClasses.line}`]: {
borderColor: theme.palette.primary.main,
},
},
[`&.${stepConnectorClasses.completed}`]: {
[`& .${stepConnectorClasses.line}`]: {
borderColor: theme.palette.primary.main,
},
},
[`& .${stepConnectorClasses.line}`]: {
borderColor: theme.palette.divider,
borderTopWidth: 2,
borderRadius: 1,
},
}));
// Custom step icon showing numbers
const CustomStepIconRoot = styled('div')<{ ownerState: { active?: boolean; completed?: boolean } }>(
({ theme, ownerState }) => ({
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
width: 32,
height: 32,
borderRadius: '50%',
backgroundColor: ownerState.completed
? theme.palette.primary.main
: ownerState.active
? theme.palette.primary.main
: theme.palette.grey[300],
color: ownerState.active || ownerState.completed ? '#fff' : theme.palette.text.secondary,
fontWeight: 600,
fontSize: '0.875rem',
})
);
function CustomStepIcon(props: StepIconProps) {
const { active, completed, icon } = props;
return (
{completed ? : icon}
);
}
export default function OnboardingPage() {
const [activeStep, setActiveStep] = useState(0);
const [selectedLanguage, setSelectedLanguage] = useState('en');
const [selectedMeasurement, setSelectedMeasurement] = useState('metric');
const [childName, setChildName] = useState('');
const [childBirthDate, setChildBirthDate] = useState('');
const [childGender, setChildGender] = useState<'male' | 'female' | 'other'>('other');
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const router = useRouter();
const { user, refreshUser } = useAuth();
const { setLanguage, setMeasurementSystem } = useLocale();
const { t } = useTranslation('onboarding');
const theme = useTheme();
const handleNext = async () => {
setError('');
// Step 1: Save language preference
if (activeStep === 1) {
try {
setLoading(true);
await setLanguage(selectedLanguage);
// Save to backend
if (user?.id) {
await usersApi.updatePreferences({
language: selectedLanguage,
});
}
setActiveStep((prevActiveStep) => prevActiveStep + 1);
} catch (err: any) {
console.error('Failed to save language:', err);
// Language was saved locally even if backend failed, so continue
setActiveStep((prevActiveStep) => prevActiveStep + 1);
} finally {
setLoading(false);
}
return;
}
// Step 2: Save measurement preference
if (activeStep === 2) {
try {
setLoading(true);
setMeasurementSystem(selectedMeasurement);
// Save to backend
if (user?.id) {
await usersApi.updatePreferences({
measurementUnit: selectedMeasurement,
});
await refreshUser();
}
setActiveStep((prevActiveStep) => prevActiveStep + 1);
} catch (err: any) {
console.error('Failed to save measurement:', err);
// Measurement was saved locally even if backend failed, so continue
setActiveStep((prevActiveStep) => prevActiveStep + 1);
} finally {
setLoading(false);
}
return;
}
// Step 3: Validate and save child data (Add Child)
if (activeStep === 3) {
if (!childName.trim() || !childBirthDate) {
setError(t('child.name') + ' and ' + t('child.dateOfBirth') + ' are required');
return;
}
const familyId = user?.families?.[0]?.familyId;
if (!familyId) {
console.error('No family found. User object:', JSON.stringify(user, null, 2));
setError('Unable to create child profile. Your account setup is incomplete. Please contact support or try logging out and back in.');
return;
}
try {
setLoading(true);
setError('');
await childrenApi.createChild(familyId, {
name: childName.trim(),
birthDate: childBirthDate,
gender: childGender,
});
setActiveStep((prevActiveStep) => prevActiveStep + 1);
} catch (err: any) {
console.error('Failed to create child:', err);
setError(err.response?.data?.message || 'Failed to save child. Please try again.');
} finally {
setLoading(false);
}
return;
}
if (activeStep === steps.length - 1) {
// Complete onboarding
router.push('/');
} else {
setActiveStep((prevActiveStep) => prevActiveStep + 1);
}
};
const handleBack = () => {
setActiveStep((prevActiveStep) => prevActiveStep - 1);
};
const handleSkip = () => {
router.push('/');
};
return (
}
sx={{ mb: 4 }}
>
{steps.map((label, index) => {
let stepLabel = label;
if (index === 0) stepLabel = t('welcome.title').split('!')[0];
else if (index === 1) stepLabel = t('language.title');
else if (index === 2) stepLabel = t('measurements.title');
else if (index === 3) stepLabel = t('child.title');
else if (index === 4) stepLabel = t('complete.title').split('!')[0];
// Only show label for active step on mobile
const showLabel = activeStep === index;
return (
{stepLabel}
);
})}
{/* Step 0: Welcome */}
{activeStep === 0 && (
{t('welcome.title')} 🎉
{t('welcome.description')}
📊
{t('welcome.getStarted')}
🤖
AI Insights
👨👩👧
Family Sharing
)}
{/* Step 1: Language Selection */}
{activeStep === 1 && (
{t('language.title')}
{t('language.subtitle')}
{error && (
{error}
)}
{supportedLanguages.map((lang) => (
setSelectedLanguage(lang.code)}
sx={{ p: 2 }}
>
{lang.nativeName}
{lang.name}
))}
{t('language.description')}
)}
{/* Step 2: Measurement System */}
{activeStep === 2 && (
{t('measurements.title')}
{t('measurements.subtitle')}
{error && (
{error}
)}
setSelectedMeasurement('metric')}
sx={{ p: 3, height: '100%' }}
>
{t('measurements.metric.title')}
{t('measurements.metric.description')}
setSelectedMeasurement('imperial')}
sx={{ p: 3, height: '100%' }}
>
{t('measurements.imperial.title')}
{t('measurements.imperial.description')}
{t('measurements.description')}
)}
{/* Step 3: Add Child */}
{activeStep === 3 && (
{t('child.title')}
{t('child.subtitle')}
{error && (
{error}
)}
setChildName(e.target.value)}
margin="normal"
required
disabled={loading}
InputProps={{
sx: { borderRadius: 3 },
}}
/>
setChildBirthDate(e.target.value)}
margin="normal"
required
disabled={loading}
InputLabelProps={{
shrink: true,
}}
InputProps={{
sx: { borderRadius: 3 },
}}
/>
setChildGender(e.target.value as 'male' | 'female' | 'other')}
margin="normal"
disabled={loading}
InputProps={{
sx: { borderRadius: 3 },
}}
>
{t('child.skipForNow')}
)}
{/* Step 4: Complete */}
{activeStep === 4 && (
{t('complete.title')} 🎉
{t('complete.description')}
{t('complete.subtitle')}
• Track your first feeding, sleep, or diaper change
• Chat with our AI assistant for parenting tips
• Explore insights and predictions based on your data
)}
}
>
{t('navigation.back')}
{activeStep === 3 && (
)}
: (activeStep === steps.length - 1 ? : )}
>
{activeStep === steps.length - 1 ? t('complete.startTracking') : t('navigation.next')}
);
}