import React, { useState } from 'react'; import { useAppContext } from '../store/AppContext'; import { Sun, Moon, Lock, Trash2, Film, ChevronRight, AlertTriangle } from 'lucide-react'; const TMDB_GENRES = [ { id: 28, name: 'Action' }, { id: 12, name: 'Adventure' }, { id: 16, name: 'Animation' }, { id: 35, name: 'Comedy' }, { id: 80, name: 'Crime' }, { id: 99, name: 'Documentary' }, { id: 18, name: 'Drama' }, { id: 10751, name: 'Family' }, { id: 14, name: 'Fantasy' }, { id: 36, name: 'History' }, { id: 27, name: 'Horror' }, { id: 10402, name: 'Music' }, { id: 9648, name: 'Mystery' }, { id: 10749, name: 'Romance' }, { id: 878, name: 'Science Fiction' }, { id: 53, name: 'Thriller' }, { id: 10752, name: 'War' }, { id: 37, name: 'Western' }, ]; type SettingsSection = 'main' | 'genres' | 'password' | 'delete'; export const Settings: React.FC = () => { const { user, token, theme, toggleTheme, updateGenres, logout } = useAppContext(); const [activeSection, setActiveSection] = useState('main'); const [selectedGenres, setSelectedGenres] = useState(user?.genres ?? []); const [genresSaved, setGenresSaved] = useState(false); // Password reset state const [currentPassword, setCurrentPassword] = useState(''); const [newPassword, setNewPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [passwordError, setPasswordError] = useState(''); const [passwordSuccess, setPasswordSuccess] = useState(''); // Delete account state const [deletePassword, setDeletePassword] = useState(''); const [deleteError, setDeleteError] = useState(''); const isGuest = !user?.username; const toggleGenre = (id: number) => { setSelectedGenres((prev) => prev.includes(id) ? prev.filter((g) => g !== id) : [...prev, id] ); setGenresSaved(false); }; const handleSaveGenres = async () => { if (selectedGenres.length === 0) return; await updateGenres(selectedGenres); setGenresSaved(true); setTimeout(() => setGenresSaved(false), 2000); }; const handleChangePassword = async (e: React.FormEvent) => { e.preventDefault(); setPasswordError(''); setPasswordSuccess(''); if (newPassword.length < 4) { setPasswordError('New password must be at least 4 characters.'); return; } if (newPassword !== confirmPassword) { setPasswordError('New passwords do not match.'); return; } try { const res = await fetch('/api/auth/password', { method: 'PUT', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}`, }, body: JSON.stringify({ currentPassword, newPassword }), }); const data = await res.json(); if (!res.ok) throw new Error(data.error); setPasswordSuccess('Password changed successfully!'); setCurrentPassword(''); setNewPassword(''); setConfirmPassword(''); } catch (err: unknown) { setPasswordError(err instanceof Error ? err.message : 'An error occurred'); } }; const handleDeleteAccount = async () => { if (!deletePassword) { setDeleteError('Please enter your password to confirm.'); return; } setDeleteError(''); try { const res = await fetch('/api/auth/account', { method: 'DELETE', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}`, }, body: JSON.stringify({ password: deletePassword }), }); const data = await res.json(); if (!res.ok) throw new Error(data.error); logout(); } catch (err: unknown) { setDeleteError(err instanceof Error ? err.message : 'An error occurred'); } }; if (!user) return null; // ── Sub-sections ────────────────────────────────────────────── if (activeSection === 'genres') { return (

Movie Styles

Select the genres you enjoy. This influences your movie feed.

{TMDB_GENRES.map((genre) => ( ))}
); } if (activeSection === 'password') { return (

Change Password

Update your account password.

{passwordError &&

{passwordError}

} {passwordSuccess &&

{passwordSuccess}

} setCurrentPassword(e.target.value)} placeholder="Current password" className="w-full bg-surface border border-border rounded-xl p-3 text-foreground placeholder:text-muted focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent" required /> setNewPassword(e.target.value)} placeholder="New password" className="w-full bg-surface border border-border rounded-xl p-3 text-foreground placeholder:text-muted focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent" required /> setConfirmPassword(e.target.value)} placeholder="Confirm new password" className="w-full bg-surface border border-border rounded-xl p-3 text-foreground placeholder:text-muted focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent" required />
); } if (activeSection === 'delete') { return (

Delete Account

This action is permanent and irreversible. All your data — swipes, matches, and account info — will be deleted.

{deleteError &&

{deleteError}

} setDeletePassword(e.target.value)} placeholder="Your password" className="w-full bg-surface border border-red-500/40 rounded-xl p-3 text-foreground placeholder:text-muted focus:outline-none focus:ring-2 focus:ring-red-500 focus:border-transparent mb-4" />
); } // ── Main settings menu ──────────────────────────────────────── return (

Settings

{/* Theme toggle */}
{theme === 'dark' ? ( ) : ( )}

Theme

{theme === 'dark' ? 'Dark mode' : 'Light mode'}

{/* Movie Styles */} {/* Password Reset (only for registered users) */} {!isGuest && ( )} {/* Delete Account (hidden for guests) */} {!isGuest && ( )}
); };