'use client'; import { useState } from 'react'; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { User, Mail, Calendar, Shield, Activity, Crown, Edit, Trash2, Ban, CheckCircle, XCircle, Clock, X } from 'lucide-react'; import { cn } from '@/lib/utils'; import type { User as UserType } from '@/types/user'; interface UserDetailModalProps { user: UserType | null; isOpen: boolean; onClose: () => void; onUpdateRole?: (userId: string, role: 'ADMIN' | 'USER') => Promise; onDeleteUser?: (userId: string) => Promise; onToggleBan?: (userId: string, shouldBan: boolean) => Promise; isFirstUser?: boolean; } export function UserDetailModal({ user, isOpen, onClose, onUpdateRole, onDeleteUser, onToggleBan, isFirstUser = false }: UserDetailModalProps) { const [isUpdating, setIsUpdating] = useState(false); if (!user) return null; const getInitials = (name: string) => { return name .split(' ') .map(part => part[0]) .join('') .toUpperCase() .substring(0, 2); }; const getRoleColor = (role: string) => { switch (role?.toLowerCase()) { case 'admin': return 'from-blue-500 to-blue-600'; case 'editor': return 'from-emerald-500 to-emerald-600'; case 'user': return 'from-slate-500 to-slate-600'; default: return 'from-gray-500 to-gray-600'; } }; const handleRoleUpdate = async (newRole: 'ADMIN' | 'USER') => { if (!onUpdateRole) return; // Proteger al primer usuario de perder privilegios de admin if (isFirstUser && newRole === 'USER') { alert('No se puede quitar el rol de admin al primer usuario del sistema por seguridad.'); return; } setIsUpdating(true); try { await onUpdateRole(user.id, newRole); onClose(); } catch (error) { console.error('Error updating role:', error); } finally { setIsUpdating(false); } }; const handleDelete = async () => { if (!onDeleteUser) return; // Proteger al primer usuario (admin principal) if (isFirstUser) { alert('No se puede eliminar el primer usuario del sistema por seguridad.'); return; } if (window.confirm('¿Estás seguro de que quieres eliminar este usuario? Esta acción no se puede deshacer.')) { setIsUpdating(true); try { await onDeleteUser(user.id); onClose(); } catch (error) { console.error('Error deleting user:', error); } finally { setIsUpdating(false); } } }; const handleToggleBan = async () => { if (!onToggleBan) return; const shouldBan = !user.banned; const action = shouldBan ? 'banear' : 'desbanear'; // Proteger al primer usuario de ser baneado if (isFirstUser && shouldBan) { alert('No se puede banear al primer usuario del sistema por seguridad.'); return; } if (window.confirm(`¿Estás seguro de que quieres ${action} a este usuario?`)) { setIsUpdating(true); try { await onToggleBan(user.id, shouldBan); onClose(); } catch (error) { console.error('Error toggling ban:', error); } finally { setIsUpdating(false); } } }; if (!isOpen) return null; return (
{/* Transparent overlay */}
{/* Modal content */}
{/* Header */}

Detalles del Usuario

{/* User Profile Card */}
{getInitials(user.name || 'Usuario')}

{user.name || 'Sin nombre'}

{user.role === 'ADMIN' && (
Admin
)} {user.banned && (
Baneado
)}
{user.email} {user.emailVerified ? ( ) : ( )}
Registrado: {user.createdAt ? new Date(user.createdAt).toLocaleDateString() : 'Fecha no disponible'}
{/* Stats Grid */}
{user.emailVerified ? 'Verificado' : 'Sin verificar'}
Estado del email
{user.role?.toLowerCase() || 'Usuario'}
Rol del usuario
{/* Ban Information */} {user.banned && (
Usuario Baneado
{user.banReason && (

Razón: {user.banReason}

)} {user.banExpires && (
Expira: {new Date(user.banExpires).toLocaleDateString()}
)}
)} {/* Actions */}
); }