import React, { useMemo, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Building2, Check, KeyRound, LogOut, Pencil, Settings as SettingsIcon, Shield, ShieldCheck, Smartphone, CheckCircle2, Lock, Eye, Sparkles, Clock, Laptop, Activity, Zap, AlertCircle, Key } from 'lucide-react'; import { Button, Field, Input, Modal, Toggle, toast, } from '@/components/ds'; import { base44 } from '@/api/base44Client'; import { useCurrentUser, useUpdateProfile, useUserActivity } from '@/lib/krowHooks'; import { AdminPage } from '@/components/admin/PageShell'; import { endAdminSession } from '@/lib/admin/session'; import { PERMISSIONS } from '@/lib/admin/permissions'; import { SkillSurface } from '@/components/skills/SkillSurface'; import { useTranslation } from 'react-i18next'; /** * Admin Profile — Premium enterprise account & security control center. */ export default function AdminProfile() { const { t } = useTranslation(); const navigate = useNavigate(); const { data: user } = useCurrentUser(); const updateProfile = useUpdateProfile(); const [editOpen, setEditOpen] = useState(false); const [passwordOpen, setPasswordOpen] = useState(false); const [twoFactor, setTwoFactor] = useState(true); const [form, setForm] = useState({ full_name: '', email: '' }); const [nameError, setNameError] = useState(''); const saving = updateProfile.isPending; /* Stamped once on mount */ const [sessionStarted] = useState(() => new Date().toLocaleString(undefined, { weekday: 'short', month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit', }) ); /** Recognise device without fingerprinting */ const deviceLabel = useMemo(() => { const ua = navigator.userAgent; const os = /Mac/i.test(ua) ? 'macOS' : /Windows/i.test(ua) ? 'Windows' : /Android/i.test(ua) ? 'Android' : /iPhone|iPad/i.test(ua) ? 'iOS' : 'Unknown OS'; const browser = /Edg\//i.test(ua) ? 'Edge' : /Chrome\//i.test(ua) ? 'Chrome' : /Safari\//i.test(ua) ? 'Safari' : /Firefox\//i.test(ua) ? 'Firefox' : 'Unknown browser'; return `${browser} on ${os}`; }, []); const { data: activity = [] } = useUserActivity(); React.useEffect(() => { if (!editOpen || !user) return; setForm({ full_name: user.full_name || '', email: user.email || '' }); setNameError(''); }, [editOpen, user]); const myActivity = useMemo( () => activity.filter((e) => e.user_email === user?.email).slice(0, 6), [activity, user] ); const save = async () => { const fullName = form.full_name.trim(); if (!fullName) { setNameError('Name cannot be empty.'); return; } if (fullName.length > 60) { setNameError('Name must be 60 characters or fewer.'); return; } setNameError(''); try { await updateProfile.mutateAsync({ full_name: fullName }); setEditOpen(false); toast.success('Profile updated'); } catch { toast.error('Could not save your profile'); } }; const fullName = user?.full_name || 'Admin User'; const email = user?.email || 'admin@krow.app'; const memberSince = user ? new Date(user.created_date).toLocaleDateString(undefined, { month: 'long', year: 'numeric' }) : 'June 2026'; return ( } >
{/* ── 1. Hero Identity Banner ────────────────────────────────────────────── */}
{/* Subtle background ambient mesh */}
{/* Avatar with status ring */}
{user?.avatar_url ? ( {fullName} ) : ( fullName.split(' ').map(n => n[0]).join('').slice(0, 2).toUpperCase() )}
{/* User details */}

{fullName}

{t('profile.verifiedAdmin')}

{email}

{t('profile.platformAdministrator')} {t('profile.activeVerified')} {twoFactor && ( {t('profile.2faProtected')} )}
{/* Quick stats in Hero */}

{t('profile.scopes')}

{PERMISSIONS.length}

{t('profile.fullControl')}

{t('profile.securityLevel')}

{t('profile.tier1')}

{t('profile.maxPrivileges')}
{/* ── 2. Information Cards Grid ────────────────────────────────────────── */}
{t('profile.systemRole')}

{t('profile.platformAdministrator')}

{t('profile.fullSystemGovernance')}

{t('profile.department')}

{t('profile.workforceOperations')}

{t('profile.enterpriseManagement')}

{t('profile.accountStatus')}

{t('profile.activeVerified')}

{t('profile.allServicesActive')}

{t('profile.memberSince')}

{memberSince}

{t('profile.authenticatedAccount')}

{/* ── 3. Main Split View: Left (Permissions & Activity) | Right (Security & Session) */}
{/* Left Column (2 Cols) */}
{/* Permissions Matrix. `id` is the anchor Settings → Users & permissions links to, so that row lands on this card. */}

{t('profile.accessScopePermissions')}

{t('profile.operationalScopesGrantedToYourAdministrative')}

{PERMISSIONS.length} {t('profile.activeScopes')}
{PERMISSIONS.map((p) => { const isFull = p.level === 'Full access'; return (

{p.scope}

{p.detail}

{/* Status badge */} {isFull ? ( {t('profile.fullAccess')} ) : ( {t('profile.readOnly')} )}
); })}
{/* Recent Activity */}

{t('profile.recentAccountActivity')}

{t('profile.auditLogOfAdministrativeActionsPerformed')}

{myActivity.length} {t('profile.eventsLogged')}
{myActivity.length ? (
{myActivity.map((e) => (

{e.event_type.replace(/_/g, ' ')}

{e.details &&

{e.details}

}
{new Date(e.created_date).toLocaleDateString(undefined, { month: 'short', day: 'numeric' })}
))}
) : (

{t('profile.noRecentActivityRecordedForThis')}

)}
{/* Right Column (1 Col) - Security & Session */}
{/* Security Control Card. `id` is the anchor the account menu's Security item and Settings → Security both point at. */}

{t('profile.security2fa')}

{/* Last Sign-In */}
{t('profile.lastSignIn')} {t('profile.activeDevice')}

{new Date().toLocaleString(undefined, { month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit' })}

{t('profile.ip10241186')} {deviceLabel}

{/* 2FA Toggle */}
{ setTwoFactor(v); toast[v ? 'success' : 'error'](v ? '2FA enabled' : '2FA disabled'); }} />
{/* Action Buttons */}
{/* Password expiry note */}

{t('profile.passwordLastChanged')} {t('profile.42DaysAgo')}{t('profile.administratorCredentialsExpireEvery90Days')}

{/* Current Session Card */}

{t('profile.currentActiveSession')}

{t('profile.sessionStarted')}
{sessionStarted}
{t('profile.sessionType')}
{t('profile.browserTab')}
{t('profile.platformOs')}
{deviceLabel}
{t('profile.tokenSecurity')}
{t('profile.encrypted')}

{t('profile.administratorSessionsExpireAutomaticallyOnBrowser')}

{/* Edit Profile Modal */} } >
{ setForm({ ...form, full_name: e.target.value }); if (nameError) setNameError(''); }} />
{/* Change Password Modal */} } >
); }