'use client'; import { useState } from 'react'; import { Box, Typography, Button, Paper, TextField, IconButton, Alert, Chip, FormControl, FormLabel, RadioGroup, FormControlLabel, Radio, } from '@mui/material'; import { ArrowBack, Bedtime, WbSunny, Save, Mic, } from '@mui/icons-material'; import { useRouter } from 'next/navigation'; import { AppShell } from '@/components/layouts/AppShell/AppShell'; import { ProtectedRoute } from '@/components/common/ProtectedRoute'; import { motion } from 'framer-motion'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import * as z from 'zod'; import { format } from 'date-fns'; const sleepSchema = z.object({ startTime: z.string(), endTime: z.string(), quality: z.enum(['excellent', 'good', 'fair', 'poor']), notes: z.string().optional(), }).refine((data) => new Date(data.endTime) > new Date(data.startTime), { message: 'End time must be after start time', path: ['endTime'], }); type SleepFormData = z.infer; export default function SleepTrackPage() { const router = useRouter(); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); const { register, handleSubmit, setValue, watch, formState: { errors }, } = useForm({ resolver: zodResolver(sleepSchema), defaultValues: { quality: 'good', }, }); const startTime = watch('startTime'); const endTime = watch('endTime'); const calculateDuration = () => { if (!startTime || !endTime) return null; const start = new Date(startTime); const end = new Date(endTime); const diff = end.getTime() - start.getTime(); if (diff < 0) return null; const hours = Math.floor(diff / (1000 * 60 * 60)); const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); return `${hours}h ${minutes}m`; }; const setStartNow = () => { const now = format(new Date(), "yyyy-MM-dd'T'HH:mm"); setValue('startTime', now); }; const setEndNow = () => { const now = format(new Date(), "yyyy-MM-dd'T'HH:mm"); setValue('endTime', now); }; const onSubmit = async (data: SleepFormData) => { setError(null); try { // TODO: Call API to save sleep data console.log('Sleep data:', data); setSuccess(true); setTimeout(() => router.push('/'), 2000); } catch (err: any) { setError(err.message || 'Failed to log sleep'); } }; return ( router.back()} sx={{ mr: 2 }}> Track Sleep {success && ( Sleep logged successfully! )} {error && ( {error} )} {/* Start Time */} Sleep Start {/* End Time */} Wake Up {/* Duration Display */} {calculateDuration() && ( )} {/* Sleep Quality */} Sleep Quality } label="Excellent" /> } label="Good" /> } label="Fair" /> } label="Poor" /> {/* Notes */} {/* Voice Input Button */} } label="Use Voice Input" onClick={() => {/* TODO: Implement voice input */}} sx={{ cursor: 'pointer' }} /> {/* Submit Button */} ); }