import { useState } from 'react' import { useTranslation } from 'react-i18next' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { useAuthStore } from '../store/auth' import { authApi } from '../api/auth' import { notificationsApi, NotificationSettings, NotificationSettingsUpdate } from '../api/notifications' export default function Profile() { const { t } = useTranslation() const { user, setUser } = useAuthStore() const queryClient = useQueryClient() const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [confirmPassword, setConfirmPassword] = useState('') const [error, setError] = useState(null) const [success, setSuccess] = useState(null) const registerEmailMutation = useMutation({ mutationFn: ({ email, password }: { email: string; password: string }) => authApi.registerEmail(email, password), onSuccess: async () => { setSuccess(t('profile.emailSent')) setError(null) setEmail('') setPassword('') setConfirmPassword('') const updatedUser = await authApi.getMe() setUser(updatedUser) queryClient.invalidateQueries({ queryKey: ['user'] }) }, onError: (err: { response?: { data?: { detail?: string } } }) => { setError(err.response?.data?.detail || t('common.error')) setSuccess(null) }, }) const resendVerificationMutation = useMutation({ mutationFn: authApi.resendVerification, onSuccess: () => { setSuccess(t('profile.verificationResent')) setError(null) }, onError: (err: { response?: { data?: { detail?: string } } }) => { setError(err.response?.data?.detail || t('common.error')) setSuccess(null) }, }) const { data: notificationSettings, isLoading: notificationsLoading } = useQuery({ queryKey: ['notification-settings'], queryFn: notificationsApi.getSettings, }) const updateNotificationsMutation = useMutation({ mutationFn: notificationsApi.updateSettings, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['notification-settings'] }) }, }) const handleNotificationToggle = (key: keyof NotificationSettings, value: boolean) => { const update: NotificationSettingsUpdate = { [key]: value } updateNotificationsMutation.mutate(update) } const handleNotificationValue = (key: keyof NotificationSettings, value: number) => { const update: NotificationSettingsUpdate = { [key]: value } updateNotificationsMutation.mutate(update) } const handleSubmit = (e: React.FormEvent) => { e.preventDefault() setError(null) setSuccess(null) if (!email.trim()) { setError(t('profile.emailRequired')) return } if (!password || password.length < 8) { setError(t('profile.passwordMinLength')) return } if (password !== confirmPassword) { setError(t('profile.passwordsMismatch')) return } registerEmailMutation.mutate({ email, password }) } return (

{t('profile.title')}

{/* User Info Card */}

{t('profile.accountInfo')}

{t('profile.telegramId')} {user?.telegram_id}
{user?.username && (
{t('profile.username')} @{user.username}
)}
{t('profile.name')} {user?.first_name} {user?.last_name}
{t('profile.registeredAt')} {user?.created_at ? new Date(user.created_at).toLocaleDateString() : '-'}
{/* Email Section */}

{t('profile.emailAuth')}

{user?.email ? (
Email
{user.email} {user.email_verified ? ( {t('profile.verified')} ) : ( {t('profile.notVerified')} )}
{!user.email_verified && (

{t('profile.verificationRequired')}

)} {user.email_verified && (

{t('profile.canLoginWithEmail')}

)}
) : (

{t('profile.linkEmailDescription')}

setEmail(e.target.value)} placeholder="your@email.com" className="input" />
setPassword(e.target.value)} placeholder={t('profile.passwordPlaceholder')} className="input" />

{t('profile.passwordHint')}

setConfirmPassword(e.target.value)} placeholder={t('profile.confirmPasswordPlaceholder')} className="input" />
{error && (
{error}
)} {success && (
{success}
)}
)} {(error || success) && user?.email && (
{error && (
{error}
)} {success && (
{success}
)}
)}
{/* Notification Settings */}

{t('profile.notifications.title')}

{notificationsLoading ? (
) : notificationSettings ? (
{/* Subscription Expiry */}

{t('profile.notifications.subscriptionExpiry')}

{t('profile.notifications.subscriptionExpiryDesc')}

{notificationSettings.subscription_expiry_enabled && (
{t('profile.notifications.daysBeforeExpiry')}
)}
{/* Traffic Warning */}

{t('profile.notifications.trafficWarning')}

{t('profile.notifications.trafficWarningDesc')}

{notificationSettings.traffic_warning_enabled && (
{t('profile.notifications.atPercent')}
)}
{/* Balance Low */}

{t('profile.notifications.balanceLow')}

{t('profile.notifications.balanceLowDesc')}

{notificationSettings.balance_low_enabled && (
{t('profile.notifications.threshold')} handleNotificationValue('balance_low_threshold', Number(e.target.value))} min={0} className="input w-24 py-1" />
)}
{/* News */}

{t('profile.notifications.news')}

{t('profile.notifications.newsDesc')}

{/* Promo Offers */}

{t('profile.notifications.promoOffers')}

{t('profile.notifications.promoOffersDesc')}

) : (

{t('profile.notifications.unavailable')}

)}
) }