'use client'; import { useState } from 'react'; import { Header } from '@/components/layouts/header'; import { Card, CardContent, CardHeader, CardTitle, Button, Input, Label, Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@horux/shared-ui'; import { useAuthStore } from '@/stores/auth-store'; import { isGlobalAdminRfc } from '@horux/shared'; import { useAuditLog } from '@/lib/hooks/use-audit-log'; import { ChevronLeft, ChevronRight, Search, X, FileWarning, ShieldAlert } from 'lucide-react'; const ACTION_GROUPS = [ { value: '', label: 'Todas las acciones' }, { value: 'user.', label: 'Usuarios (login, logout, password)' }, { value: 'subscription.', label: 'Suscripciones (crear, cancelar, cambiar, reactivar)' }, { value: 'trial.', label: 'Trials' }, { value: 'price.', label: 'Precios' }, { value: 'invoice.', label: 'Facturación' }, { value: 'payment.', label: 'Pagos' }, { value: 'tenant.', label: 'Tenants' }, { value: 'fiel.', label: 'FIEL' }, ]; const ACTION_LABELS: Record = { 'user.login': { label: 'Login', color: 'bg-blue-50 text-blue-700' }, 'user.logout': { label: 'Logout', color: 'bg-slate-50 text-slate-700' }, 'user.password_changed': { label: 'Cambio password', color: 'bg-amber-50 text-amber-700' }, 'trial.started': { label: 'Trial iniciado', color: 'bg-sky-50 text-sky-700' }, 'subscription.created': { label: 'Suscripción creada',color: 'bg-green-50 text-green-700' }, 'subscription.cancelled': { label: 'Suscripción cancelada', color: 'bg-orange-50 text-orange-700' }, 'subscription.reactivated': { label: 'Reactivada', color: 'bg-teal-50 text-teal-700' }, 'subscription.plan_changed': { label: 'Cambio de plan', color: 'bg-indigo-50 text-indigo-700' }, 'price.updated': { label: 'Precio editado', color: 'bg-purple-50 text-purple-700' }, 'invoice.emitted_auto': { label: 'Factura auto', color: 'bg-emerald-50 text-emerald-700' }, 'invoice.emitted_manual': { label: 'Factura manual', color: 'bg-emerald-50 text-emerald-700' }, 'payment.marked_paid_manually': { label: 'Pago marcado manual', color: 'bg-lime-50 text-lime-700' }, }; function formatDateTime(iso: string): string { const d = new Date(iso); return d.toLocaleString('es-MX', { dateStyle: 'short', timeStyle: 'medium' }); } function ActionBadge({ action }: { action: string }) { const cfg = ACTION_LABELS[action] || { label: action, color: 'bg-muted text-muted-foreground' }; return {cfg.label}; } export default function AuditLogPage() { const { user } = useAuthStore(); const isGlobalAdmin = isGlobalAdminRfc(user?.tenantRfc, user?.role, user?.platformRoles); const [filters, setFilters] = useState({ action: '', tenantId: '', userId: '', from: '', to: '', page: 1, limit: 50, }); const [expandedId, setExpandedId] = useState(null); const { data, isLoading } = useAuditLog(filters); if (!isGlobalAdmin) { return ( <>

Acceso restringido

Solo el administrador global puede consultar el audit log.

); } const clearFilters = () => setFilters({ action: '', tenantId: '', userId: '', from: '', to: '', page: 1, limit: 50 }); return ( <>
Filtros
setFilters(f => ({ ...f, tenantId: e.target.value, page: 1 }))} placeholder="UUID del tenant" />
setFilters(f => ({ ...f, userId: e.target.value, page: 1 }))} placeholder="UUID del usuario" />
setFilters(f => ({ ...f, from: e.target.value, page: 1 }))} />
setFilters(f => ({ ...f, to: e.target.value, page: 1 }))} />
Eventos {data?.total !== undefined && ({data.total.toLocaleString('es-MX')} totales)} {isLoading ? (

Cargando...

) : !data || data.data.length === 0 ? (

No hay eventos con estos filtros.

) : ( <>
{data.data.map(row => ( ))}
Fecha Acción Usuario Tenant Entidad
{formatDateTime(row.createdAt)} {row.user ? (
{row.user.nombre}
{row.user.email}
) : Sistema}
{row.tenant ? (
{row.tenant.nombre}
{row.tenant.rfc}
) : }
{row.entityType ? `${row.entityType}${row.entityId ? ` ${row.entityId.slice(0, 8)}` : ''}` : '—'}
{expandedId && (() => { const row = data.data.find(r => r.id === expandedId); if (!row) return null; return (

Metadata del evento {row.id}

{JSON.stringify(row.metadata, null, 2)}
); })()} {data.totalPages > 1 && (

Página {data.page} de {data.totalPages}

)} )}
); }