'use client'; import { useState, useEffect } from 'react'; import { Card, CardContent, Typography, Button, Box, Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions, TextField, Alert, CircularProgress, Chip, } from '@mui/material'; import { DeleteForever, Cancel, Warning } from '@mui/icons-material'; import { complianceApi, DeletionRequest } from '@/lib/api/compliance'; import { useAuth } from '@/lib/auth/AuthContext'; export function AccountDeletion() { const { logout } = useAuth(); const [deletionRequest, setDeletionRequest] = useState(null); const [isDialogOpen, setIsDialogOpen] = useState(false); const [isCancelDialogOpen, setIsCancelDialogOpen] = useState(false); const [reason, setReason] = useState(''); const [cancellationReason, setCancellationReason] = useState(''); const [isLoading, setIsLoading] = useState(false); const [isCheckingStatus, setIsCheckingStatus] = useState(true); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); useEffect(() => { checkDeletionStatus(); }, []); const checkDeletionStatus = async () => { setIsCheckingStatus(true); try { const status = await complianceApi.getDeletionStatus(); setDeletionRequest(status); } catch (err) { console.error('Failed to check deletion status:', err); } finally { setIsCheckingStatus(false); } }; const handleRequestDeletion = async () => { setIsLoading(true); setError(null); setSuccess(null); try { const request = await complianceApi.requestAccountDeletion(reason); setDeletionRequest(request); setSuccess( `Account deletion scheduled for ${new Date(request.scheduledDeletionAt).toLocaleDateString()}. You can cancel this request within 30 days.` ); setIsDialogOpen(false); setReason(''); } catch (err: any) { console.error('Failed to request deletion:', err); setError(err.response?.data?.message || 'Failed to request account deletion. Please try again.'); } finally { setIsLoading(false); } }; const handleCancelDeletion = async () => { setIsLoading(true); setError(null); setSuccess(null); try { await complianceApi.cancelAccountDeletion(cancellationReason); setDeletionRequest(null); setSuccess('Account deletion request has been cancelled successfully.'); setIsCancelDialogOpen(false); setCancellationReason(''); } catch (err: any) { console.error('Failed to cancel deletion:', err); setError(err.response?.data?.message || 'Failed to cancel account deletion. Please try again.'); } finally { setIsLoading(false); } }; if (isCheckingStatus) { return ( ); } return ( <> Danger Zone {error && ( setError(null)}> {error} )} {success && ( setSuccess(null)}> {success} )} {deletionRequest?.status === 'pending' ? ( } sx={{ mb: 2 }}> Account Deletion Scheduled Your account is scheduled for deletion on{' '} {new Date(deletionRequest.scheduledDeletionAt).toLocaleDateString()}. You can cancel this request at any time before that date. {deletionRequest.reason && ( Reason: {deletionRequest.reason} )} ) : ( Permanently delete your account and all associated data. This action cannot be undone after the 30-day grace period. This complies with GDPR's Right to Erasure. Warning: Deleting your account will remove all your data including children profiles, activities, photos, and AI conversations. This data cannot be recovered. )} {/* Delete Account Dialog */} !isLoading && setIsDialogOpen(false)} maxWidth="sm" fullWidth> Delete Account Are you sure you want to delete your account? This will permanently remove all your data after a 30-day grace period. This action will delete:
  • Your profile and account information
  • All children profiles and their data
  • All activity tracking records
  • All photos and attachments
  • All AI conversation history
  • All family memberships
setReason(e.target.value)} placeholder="Help us improve by telling us why you're leaving..." sx={{ mt: 2 }} />
{/* Cancel Deletion Dialog */} !isLoading && setIsCancelDialogOpen(false)} maxWidth="sm" fullWidth > Cancel Account Deletion Are you sure you want to cancel your account deletion request? Your account will remain active. setCancellationReason(e.target.value)} placeholder="Tell us what made you change your mind..." sx={{ mt: 2 }} /> ); }