import{p as L,n as y,k as r,s as c,j as e}from"./index-D0iJORaS.js";import{a as b,S as T,C as S,g as B,h as I,f as P}from"./card-DxZKb33V.js";import{I as A,L as E}from"./input-CcQzS21A.js";import{B as H}from"./badge-Mi6rMrZC.js";import{T as M,d as R,e as f,c as n,a as k,b as o}from"./table-BTL-c2Li.js";import"./index-BDB3jGHw.js";function J(){const d=L(),{user:i,loading:m}=y(),[p,j]=r.useState([]),[g,x]=r.useState(!0),[l,w]=r.useState("");r.useEffect(()=>{!m&&(!i||i.role!=="admin")&&d({to:"/"})},[i,m,d]),r.useEffect(()=>{i?.role==="admin"&&(async()=>{x(!0);const{data:s}=await c.from("logs_acesso").select("*").order("criado_em",{ascending:!1}).limit(500),t=Array.from(new Set((s??[]).map(a=>a.user_id).filter(Boolean))),h=Array.from(new Set((s??[]).map(a=>a.cliente_id).filter(Boolean))),[{data:N},{data:v}]=await Promise.all([t.length?c.from("perfis_usuarios").select("id, email").in("id",t):Promise.resolve({data:[]}),h.length?c.from("clientes").select("id, nome_fantasia").in("id",h):Promise.resolve({data:[]})]),_=new Map((N??[]).map(a=>[a.id,a.email])),C=new Map((v??[]).map(a=>[a.id,a.nome_fantasia]));j((s??[]).map(a=>({...a,user_email:a.user_id?_.get(a.user_id):void 0,cliente_nome:a.cliente_id?C.get(a.cliente_id):void 0}))),x(!1)})()},[i?.role]);const u=p.filter(s=>{if(!l.trim())return!0;const t=l.toLowerCase();return s.acao?.toLowerCase().includes(t)||s.user_email?.toLowerCase().includes(t)||s.cliente_nome?.toLowerCase().includes(t)||s.ip?.toLowerCase().includes(t)});return e.jsx(b,{children:e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"flex h-10 w-10 items-center justify-center rounded-lg bg-primary/10 text-primary",children:e.jsx(T,{className:"h-5 w-5"})}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold tracking-tight",children:"Logs de acesso"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Histórico de logins e ações realizadas no sistema."})]})]}),e.jsxs(S,{children:[e.jsxs(B,{children:[e.jsx(I,{className:"text-base",children:"Eventos recentes"}),e.jsx(A,{placeholder:"Filtrar por ação, e-mail, cliente ou IP...",value:l,onChange:s=>w(s.target.value),className:"max-w-md"})]}),e.jsx(P,{children:g?e.jsx("div",{className:"flex items-center justify-center py-10 text-muted-foreground",children:e.jsx(E,{className:"h-5 w-5 animate-spin"})}):u.length===0?e.jsx("div",{className:"py-10 text-center text-sm text-muted-foreground",children:"Nenhum log encontrado."}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(M,{children:[e.jsx(R,{children:e.jsxs(f,{children:[e.jsx(n,{children:"Data"}),e.jsx(n,{children:"Ação"}),e.jsx(n,{children:"Usuário"}),e.jsx(n,{children:"Cliente"}),e.jsx(n,{children:"IP"})]})}),e.jsx(k,{children:u.map(s=>e.jsxs(f,{children:[e.jsx(o,{className:"whitespace-nowrap text-xs text-muted-foreground",children:new Date(s.criado_em).toLocaleString("pt-BR")}),e.jsx(o,{children:e.jsx(H,{variant:"secondary",className:"capitalize",children:s.acao})}),e.jsx(o,{className:"text-sm",children:s.user_email??"—"}),e.jsx(o,{className:"text-sm",children:s.cliente_nome??"—"}),e.jsx(o,{className:"text-xs text-muted-foreground",children:s.ip??"—"})]},s.id))})]})})})]})]})})}export{J as component};
