import{j as e}from"./ui-vendor-DUfjpZ4p.js";import{r as b}from"./react-vendor-DlVMRrtE.js";import{h as te,af as B,ag as Rt,ah as Ft,ai as Ee,aj as Ie,ak as ue,al as Mt,i as ie,C as L,k as $,m as U,d as W,a3 as X,a4 as V,a5 as C,a6 as y,a7 as G,a8 as g,e as z,B as M,am as He,an as Tt,s as F,ao as ge,z as Ze,ap as Ht,I as Q,P as At,aq as q,f as et,u as Lt,ar as tt,as as fe,at as Y,a2 as Wt,au as zt,av as Bt,aw as Et,ax as It,ay as Ot,az as $t,aA as Ut,aB as qt,aC as Yt,aD as Kt,aE as Xt,E as st,a9 as Ae,aa as Le,ab as We,ac as ze,aF as at,F as _e,T as nt,a as rt,b as E,c as I,aG as Vt,aH as it,R as Gt,aI as Jt,y as ke,aJ as ot,aK as Qt,aL as Ce,aM as De,aN as Pe,aO as Re,aP as O,D as lt,H as Oe,G as Zt,L as $e,X as Ue,S as es,aQ as ts}from"./index-BKwmLAPt.js";import{C as ss,a as as,b as ns}from"./collapsible-C1tJ-4XR.js";import"./charts-vendor-wgDUSDZK.js";import"./supabase-vendor-Bd7R0Yn5.js";/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const ct=te("Ban",[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["path",{d:"m4.9 4.9 14.2 14.2",key:"1m5liu"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const rs=te("CalendarClock",[["path",{d:"M21 7.5V6a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h3.5",key:"1osxxc"}],["path",{d:"M16 2v4",key:"4m81vk"}],["path",{d:"M8 2v4",key:"1cmpym"}],["path",{d:"M3 10h5",key:"r794hk"}],["path",{d:"M17.5 17.5 16 16.3V14",key:"akvzfd"}],["circle",{cx:"16",cy:"16",r:"6",key:"qoo3c4"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const is=te("Cloud",[["path",{d:"M17.5 19H9a7 7 0 1 1 6.71-9h1.79a4.5 4.5 0 1 1 0 9Z",key:"p7xjir"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const os=te("DatabaseBackup",[["ellipse",{cx:"12",cy:"5",rx:"9",ry:"3",key:"msslwz"}],["path",{d:"M3 12a9 3 0 0 0 5 2.69",key:"1ui2ym"}],["path",{d:"M21 9.3V5",key:"6k6cib"}],["path",{d:"M3 5v14a9 3 0 0 0 6.47 2.88",key:"i62tjy"}],["path",{d:"M12 12v4h4",key:"1bxaet"}],["path",{d:"M13 20a5 5 0 0 0 9-3 4.5 4.5 0 0 0-4.5-4.5c-1.33 0-2.54.54-3.41 1.41L12 16",key:"1f4ei9"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const ls=te("FileArchive",[["path",{d:"M10 12v-1",key:"v7bkov"}],["path",{d:"M10 18v-2",key:"1cjy8d"}],["path",{d:"M10 7V6",key:"dljcrl"}],["path",{d:"M14 2v4a2 2 0 0 0 2 2h4",key:"tnqrlb"}],["path",{d:"M15.5 22H18a2 2 0 0 0 2-2V7l-5-5H6a2 2 0 0 0-2 2v16a2 2 0 0 0 .274 1.01",key:"gkbcor"}],["circle",{cx:"10",cy:"20",r:"2",key:"1xzdoj"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const dt=te("KeyRound",[["path",{d:"M2.586 17.414A2 2 0 0 0 2 18.828V21a1 1 0 0 0 1 1h3a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h1a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h.172a2 2 0 0 0 1.414-.586l.814-.814a6.5 6.5 0 1 0-4-4z",key:"1s6t7t"}],["circle",{cx:"16.5",cy:"7.5",r:".5",fill:"currentColor",key:"w0ekpg"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const cs=te("MonitorSmartphone",[["path",{d:"M18 8V6a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2v7a2 2 0 0 0 2 2h8",key:"10dyio"}],["path",{d:"M10 19v-3.96 3.15",key:"1irgej"}],["path",{d:"M7 19h5",key:"qswx4l"}],["rect",{width:"6",height:"10",x:"16",y:"12",rx:"2",key:"1egngj"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const ds=te("Play",[["polygon",{points:"6 3 20 12 6 21 6 3",key:"1oa8hb"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const hs=te("Server",[["rect",{width:"20",height:"8",x:"2",y:"2",rx:"2",ry:"2",key:"ngkwjq"}],["rect",{width:"20",height:"8",x:"2",y:"14",rx:"2",ry:"2",key:"iecqi9"}],["line",{x1:"6",x2:"6.01",y1:"6",y2:"6",key:"16zg32"}],["line",{x1:"6",x2:"6.01",y1:"18",y2:"18",key:"nzw8ys"}]]);function xe(s,t){const n=B(s),a=B(t),r=n.getTime()-a.getTime();return r<0?-1:r>0?1:r}function us(s){return Rt(s,Date.now())}function fs(s,t){const n=B(s),a=B(t),r=n.getFullYear()-a.getFullYear(),i=n.getMonth()-a.getMonth();return r*12+i}function xs(s){return t=>{const a=(s?Math[s]:Math.trunc)(t);return a===0?0:a}}function gs(s,t){return+B(s)-+B(t)}function ms(s){const t=B(s);return t.setHours(23,59,59,999),t}function ps(s){const t=B(s),n=t.getMonth();return t.setFullYear(t.getFullYear(),n+1,0),t.setHours(23,59,59,999),t}function vs(s){const t=B(s);return+ms(t)==+ps(t)}function ys(s,t){const n=B(s),a=B(t),r=xe(n,a),i=Math.abs(fs(n,a));let c;if(i<1)c=0;else{n.getMonth()===1&&n.getDate()>27&&n.setDate(30),n.setMonth(n.getMonth()-r*i);let d=xe(n,a)===-r;vs(B(s))&&i===1&&xe(s,a)===1&&(d=!1),c=r*(i-Number(d))}return c===0?0:c}function js(s,t,n){const a=gs(s,t)/1e3;return xs(n==null?void 0:n.roundingMethod)(a)}function ws(s,t,n){const a=Mt(),r=(n==null?void 0:n.locale)??a.locale??Ft,i=2520,c=xe(s,t);if(isNaN(c))throw new RangeError("Invalid time value");const d=Object.assign({},n,{addSuffix:n==null?void 0:n.addSuffix,comparison:c});let o,l;c>0?(o=B(t),l=B(s)):(o=B(s),l=B(t));const h=js(l,o),f=(Ee(l)-Ee(o))/1e3,x=Math.round((h-f)/60);let p;if(x<2)return n!=null&&n.includeSeconds?h<5?r.formatDistance("lessThanXSeconds",5,d):h<10?r.formatDistance("lessThanXSeconds",10,d):h<20?r.formatDistance("lessThanXSeconds",20,d):h<40?r.formatDistance("halfAMinute",0,d):h<60?r.formatDistance("lessThanXMinutes",1,d):r.formatDistance("xMinutes",1,d):x===0?r.formatDistance("lessThanXMinutes",1,d):r.formatDistance("xMinutes",x,d);if(x<45)return r.formatDistance("xMinutes",x,d);if(x<90)return r.formatDistance("aboutXHours",1,d);if(x<Ie){const j=Math.round(x/60);return r.formatDistance("aboutXHours",j,d)}else{if(x<i)return r.formatDistance("xDays",1,d);if(x<ue){const j=Math.round(x/Ie);return r.formatDistance("xDays",j,d)}else if(x<ue*2)return p=Math.round(x/ue),r.formatDistance("aboutXMonths",p,d)}if(p=ys(l,o),p<12){const j=Math.round(x/ue);return r.formatDistance("xMonths",j,d)}else{const j=p%12,R=Math.trunc(p/12);return j<3?r.formatDistance("aboutXYears",R,d):j<9?r.formatDistance("overXYears",R,d):r.formatDistance("almostXYears",R+1,d)}}function ne(s,t){return ws(s,us(s),t)}const Ns=()=>{const{toast:s}=ie(),[t,n]=b.useState([]),[a,r]=b.useState(!0),[i,c]=b.useState(null),d=async()=>{r(!0);const l=new Date(Date.now()-24*60*60*1e3).toISOString(),{data:h}=await F.from("active_sessions").select("*").gte("last_active_at",l).eq("revoked",!1).order("last_active_at",{ascending:!1});n(h||[]),r(!1)};b.useEffect(()=>{d();const l=setInterval(d,3e4);return()=>clearInterval(l)},[]);const o=async(l,h)=>{c(l);const{data:{user:f}}=await F.auth.getUser(),{error:x}=await F.from("active_sessions").update({revoked:!0,revoked_at:new Date().toISOString(),revoked_by:f==null?void 0:f.id}).eq("id",l);c(null),x?s({title:"Failed",description:x.message,variant:"destructive"}):(s({title:"Session revoked",description:`${h||"User"} will be signed out within 60 seconds.`}),d())};return e.jsxs(L,{children:[e.jsxs($,{children:[e.jsxs(U,{className:"font-heading",children:["Active Sessions (",t.length,")"]}),e.jsx("p",{className:"text-xs text-muted-foreground font-body",children:"Last 24 hours · Force sign-out propagates within 60 seconds"})]}),e.jsx(W,{className:"p-0",children:e.jsx("div",{className:"max-h-[600px] overflow-auto",children:e.jsxs(X,{children:[e.jsx(V,{children:e.jsxs(C,{children:[e.jsx(y,{children:"User"}),e.jsx(y,{children:"Role"}),e.jsx(y,{children:"Device"}),e.jsx(y,{children:"Location"}),e.jsx(y,{children:"IP"}),e.jsx(y,{children:"Last Active"}),e.jsx(y,{className:"text-right",children:"Action"})]})}),e.jsx(G,{children:a?e.jsx(C,{children:e.jsx(g,{colSpan:7,className:"text-center py-8 text-muted-foreground",children:"Loading…"})}):t.length===0?e.jsx(C,{children:e.jsx(g,{colSpan:7,className:"text-center py-8 text-muted-foreground",children:"No active sessions."})}):t.map(l=>e.jsxs(C,{children:[e.jsx(g,{className:"font-body text-sm",children:l.user_email||"—"}),e.jsx(g,{children:e.jsx(z,{variant:"secondary",children:l.user_role})}),e.jsx(g,{className:"font-body text-xs",children:l.device_info||"—"}),e.jsx(g,{className:"font-body text-xs",children:[l.city,l.country].filter(Boolean).join(", ")||"—"}),e.jsx(g,{className:"font-mono text-xs",children:l.ip_address||"—"}),e.jsx(g,{className:"font-body text-xs whitespace-nowrap",children:ne(new Date(l.last_active_at),{addSuffix:!0})}),e.jsx(g,{className:"text-right",children:e.jsxs(M,{size:"sm",variant:"destructive",disabled:i===l.id,onClick:()=>o(l.id,l.user_email),className:"gap-1.5 font-body",children:[i===l.id?e.jsx(He,{className:"h-3.5 w-3.5 animate-spin"}):e.jsx(Tt,{className:"h-3.5 w-3.5"}),"Force sign-out"]})})]},l.id))})]})})})]})},bs=()=>{const{toast:s}=ie(),[t,n]=b.useState([]),[a,r]=b.useState(!0),[i,c]=b.useState(!1),d=async()=>{r(!0);let h=F.from("security_alerts").select("*").order("created_at",{ascending:!1}).limit(200);i||(h=h.eq("acknowledged",!1));const{data:f}=await h;n(f||[]),r(!1)};b.useEffect(()=>{d();const h=setInterval(d,3e4);return()=>clearInterval(h)},[i]);const o=async h=>{const{data:{user:f}}=await F.auth.getUser();await F.from("security_alerts").update({acknowledged:!0,acknowledged_by:f==null?void 0:f.id,acknowledged_at:new Date().toISOString()}).eq("id",h),s({title:"Acknowledged"}),d()},l=h=>h==="high"?"destructive":h==="medium"?"default":"secondary";return e.jsxs(L,{children:[e.jsxs($,{className:"flex flex-row items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs(U,{className:"font-heading flex items-center gap-2",children:[e.jsx(ge,{className:"h-5 w-5 text-destructive"})," Security Alerts (",t.length,")"]}),e.jsx("p",{className:"text-xs text-muted-foreground font-body mt-1",children:"Brute-force, off-hours admin access, suspicious activity"})]}),e.jsx(M,{variant:"outline",size:"sm",onClick:()=>c(!i),className:"font-body",children:i?"Show open only":"Show all"})]}),e.jsx(W,{className:"p-0",children:e.jsx("div",{className:"max-h-[600px] overflow-auto",children:e.jsxs(X,{children:[e.jsx(V,{children:e.jsxs(C,{children:[e.jsx(y,{children:"When"}),e.jsx(y,{children:"Severity"}),e.jsx(y,{children:"Type"}),e.jsx(y,{children:"Description"}),e.jsx(y,{children:"Email / IP"}),e.jsx(y,{className:"text-right",children:"Action"})]})}),e.jsx(G,{children:a?e.jsx(C,{children:e.jsx(g,{colSpan:6,className:"text-center py-8 text-muted-foreground",children:"Loading…"})}):t.length===0?e.jsx(C,{children:e.jsx(g,{colSpan:6,className:"text-center py-8 text-muted-foreground",children:"No security alerts. System is healthy."})}):t.map(h=>e.jsxs(C,{className:h.acknowledged?"opacity-60":"",children:[e.jsx(g,{className:"font-body text-xs whitespace-nowrap",children:ne(new Date(h.created_at),{addSuffix:!0})}),e.jsx(g,{children:e.jsxs(z,{variant:l(h.severity),className:"gap-1",children:[e.jsx(Ze,{className:"h-3 w-3"}),h.severity]})}),e.jsx(g,{className:"font-body text-sm",children:h.alert_type}),e.jsx(g,{className:"font-body text-sm max-w-md",children:h.description}),e.jsxs(g,{className:"font-mono text-xs",children:[h.user_email||"—",e.jsx("br",{}),h.ip_address||""]}),e.jsx(g,{className:"text-right",children:h.acknowledged?e.jsxs(z,{variant:"secondary",className:"gap-1",children:[e.jsx(Ht,{className:"h-3 w-3"})," Ack"]}):e.jsx(M,{size:"sm",variant:"outline",onClick:()=>o(h.id),className:"font-body",children:"Acknowledge"})})]},h.id))})]})})})]})},Ss=()=>{const{toast:s}=ie(),[t,n]=b.useState([]),[a,r]=b.useState(""),[i,c]=b.useState(""),d=async()=>{const{data:h}=await F.from("ip_blocklist").select("*").order("blocked_at",{ascending:!1});n(h||[])};b.useEffect(()=>{d()},[]);const o=async()=>{if(!a.trim())return;const{data:{user:h}}=await F.auth.getUser(),{error:f}=await F.from("ip_blocklist").insert({ip_address:a.trim(),reason:i.trim()||"Manually blocked by super admin",blocked_by:h==null?void 0:h.id});f?s({title:"Failed",description:f.message,variant:"destructive"}):(s({title:"IP blocked"}),r(""),c(""),d())},l=async h=>{await F.from("ip_blocklist").update({active:!1}).eq("id",h),s({title:"Unblocked"}),d()};return e.jsxs(L,{children:[e.jsxs($,{children:[e.jsxs(U,{className:"font-heading flex items-center gap-2",children:[e.jsx(ct,{className:"h-5 w-5 text-destructive"})," IP Blocklist"]}),e.jsx("p",{className:"text-xs text-muted-foreground font-body mt-1",children:"Blocked IPs are denied login at the application layer."})]}),e.jsxs(W,{className:"space-y-4",children:[e.jsxs("div",{className:"flex gap-2 flex-wrap",children:[e.jsx(Q,{placeholder:"IP address (e.g. 192.0.2.1)",value:a,onChange:h=>r(h.target.value),className:"max-w-[220px] font-mono text-sm"}),e.jsx(Q,{placeholder:"Reason (optional)",value:i,onChange:h=>c(h.target.value),className:"flex-1 min-w-[200px] font-body"}),e.jsxs(M,{onClick:o,className:"gap-1.5 font-heading",children:[e.jsx(At,{className:"h-4 w-4"})," Block IP"]})]}),e.jsxs(X,{children:[e.jsx(V,{children:e.jsxs(C,{children:[e.jsx(y,{children:"IP Address"}),e.jsx(y,{children:"Reason"}),e.jsx(y,{children:"Blocked"}),e.jsx(y,{children:"Status"}),e.jsx(y,{className:"text-right",children:"Action"})]})}),e.jsx(G,{children:t.length===0?e.jsx(C,{children:e.jsx(g,{colSpan:5,className:"text-center py-6 text-muted-foreground",children:"No IPs blocked."})}):t.map(h=>e.jsxs(C,{children:[e.jsx(g,{className:"font-mono text-sm",children:h.ip_address}),e.jsx(g,{className:"font-body text-sm",children:h.reason||"—"}),e.jsx(g,{className:"font-body text-xs",children:q(new Date(h.blocked_at),"MMM d, HH:mm")}),e.jsx(g,{children:h.active?e.jsx(z,{variant:"destructive",children:"Active"}):e.jsx(z,{variant:"secondary",children:"Lifted"})}),e.jsx(g,{className:"text-right",children:h.active&&e.jsxs(M,{size:"sm",variant:"outline",onClick:()=>l(h.id),className:"gap-1 font-body",children:[e.jsx(et,{className:"h-3.5 w-3.5"})," Unblock"]})})]},h.id))})]})]})]})},_s=s=>{const t=(Date.now()-new Date(s).getTime())/1e3;return t<60?"just now":t<3600?`${Math.floor(t/60)}m ago`:t<86400?`${Math.floor(t/3600)}h ago`:`${Math.floor(t/86400)}d ago`},ks=()=>{const{isSuperAdmin:s}=Lt(),{toast:t}=ie(),[n,a]=b.useState([]),[r,i]=b.useState(!0),[c,d]=b.useState(null),[o,l]=b.useState(!1),[h,f]=b.useState(new Set),[x,p]=b.useState(null),j=async()=>{i(!0);const{data:v}=await F.from("active_sessions").select("user_id, user_email, user_role, ip_address, country, city, started_at").order("started_at",{ascending:!1}).limit(500),u=(v||[]).map(_=>({user_id:_.user_id,email:_.user_email,role:_.user_role,ip:_.ip_address,country:_.country,city:_.city,success:!0,created_at:_.started_at,suspicious:!1})),k=new Map;u.forEach(_=>{if(!_.email)return;const w=k.get(_.email)||[];w.push(_),k.set(_.email,w)}),k.forEach(_=>{_.sort((w,H)=>+new Date(w.created_at)-+new Date(H.created_at));for(let w=1;w<_.length;w++)+new Date(_[w].created_at)-+new Date(_[w-1].created_at)<60*60*1e3&&_[w].country&&_[w-1].country&&_[w].country!==_[w-1].country&&(_[w].suspicious=!0,_[w-1].suspicious=!0)}),a(u);const T=Array.from(new Set(u.map(_=>_.user_id).filter(Boolean)));if(T.length>0){const{data:_}=await F.from("profiles").select("id, approval_status").in("id",T),w=new Set;(_||[]).forEach(H=>{H.approval_status==="blocked"&&w.add(H.id)}),f(w)}i(!1)};b.useEffect(()=>{j()},[]);const R=async(v,u)=>{p(v);try{const{error:k}=await F.from("profiles").update({approval_status:"blocked"}).eq("id",v);if(k)throw k;await F.from("active_sessions").update({revoked:!0,revoked_at:new Date().toISOString()}).eq("user_id",v),f(T=>new Set(T).add(v)),t({title:"Account blocked",description:`${u} has been blocked and sessions revoked.`})}catch(k){t({title:"Error",description:k.message||"Failed to block account.",variant:"destructive"})}finally{p(null)}},S=b.useMemo(()=>{const v=new Map,u=new Set;return n.forEach(k=>{const T=k.country||"Unknown",_=v.get(T)||{country:T,count:0,suspicious:0,emails:new Set,users:[]};if(_.count++,k.suspicious&&_.suspicious++,k.email){_.emails.add(k.email);const w=`${T}::${k.email}`;if(!u.has(w))u.add(w),_.users.push({user_id:k.user_id,email:k.email,role:k.role,city:k.city,ip:k.ip,lastSeen:k.created_at,suspicious:k.suspicious});else if(k.suspicious){const H=_.users.find(se=>se.email===k.email);H&&(H.suspicious=!0)}}v.set(T,_)}),Array.from(v.values()).sort((k,T)=>T.count-k.count)},[n]),N=b.useMemo(()=>o?S.filter(v=>v.suspicious>0):S,[S,o]),P=b.useMemo(()=>{const v=new Set;return S.forEach(u=>u.emails.forEach(k=>v.add(k))),v.size},[S]);return e.jsxs(L,{children:[e.jsx($,{children:e.jsxs("div",{className:"flex items-start justify-between gap-4 flex-wrap",children:[e.jsxs("div",{children:[e.jsxs(U,{className:"font-heading flex items-center gap-2",children:[e.jsx(tt,{className:"h-5 w-5 text-accent"})," Geographic Login Origins"]}),e.jsxs("p",{className:"text-xs text-muted-foreground font-body mt-1",children:[S.length," countries · ",P," unique user",P!==1?"s":""," · suspicious = same user from 2 countries within 1 hour."]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(fe,{id:"susp-only",checked:o,onCheckedChange:l}),e.jsx(Y,{htmlFor:"susp-only",className:"text-xs cursor-pointer",children:"Suspicious only"})]})]})}),e.jsx(W,{children:r?e.jsx("p",{className:"text-center py-8 text-muted-foreground",children:"Loading…"}):N.length===0?e.jsxs("p",{className:"text-center py-8 text-muted-foreground",children:["No geo data",o?" matching filter":"","."]}):e.jsx("div",{className:"grid gap-2",children:N.map(v=>{const u=S[0].count,k=v.count/u*100,T=c===v.country,_=o?v.users.filter(w=>w.suspicious):v.users;return e.jsxs(ss,{open:T,onOpenChange:w=>d(w?v.country:null),className:"border border-border rounded-md",children:[e.jsx(as,{className:"w-full text-left px-3 py-2 hover:bg-muted/40 transition-colors",children:e.jsxs("div",{className:"space-y-1",children:[e.jsxs("div",{className:"flex justify-between items-center text-sm font-body gap-2",children:[e.jsxs("span",{className:"font-medium text-foreground flex items-center gap-2",children:[e.jsx(Wt,{className:`h-4 w-4 transition-transform ${T?"rotate-0":"-rotate-90"}`}),v.country,v.suspicious>0&&e.jsxs("span",{className:"text-xs px-2 py-0.5 rounded-full bg-destructive text-destructive-foreground flex items-center gap-1",children:[e.jsx(Ze,{className:"h-3 w-3"})," ",v.suspicious," suspicious"]})]}),e.jsxs("span",{className:"text-xs text-muted-foreground whitespace-nowrap",children:[v.count," session",v.count!==1?"s":""," · ",v.emails.size," user",v.emails.size!==1?"s":""]})]}),e.jsx("div",{className:"h-2 bg-muted rounded-full overflow-hidden",children:e.jsx("div",{className:`h-full ${v.suspicious>0?"bg-destructive":"bg-accent"}`,style:{width:`${k}%`}})})]})}),e.jsx(ns,{children:e.jsx("div",{className:"border-t border-border divide-y divide-border",children:_.length===0?e.jsx("div",{className:"px-3 py-3 text-xs text-muted-foreground",children:"No users to show."}):_.map((w,H)=>{const se=w.user_id?h.has(w.user_id):!1;return e.jsxs("div",{className:`px-3 py-2 text-xs grid grid-cols-1 md:grid-cols-[1.5fr_0.7fr_0.9fr_0.9fr_0.8fr_auto] gap-2 items-center ${w.suspicious?"bg-destructive/10":""}`,children:[e.jsx("span",{className:"font-medium text-foreground truncate",title:w.email,children:w.email}),e.jsx("span",{className:"text-muted-foreground capitalize",children:w.role||"—"}),e.jsx("span",{className:"text-muted-foreground",children:w.city||"—"}),e.jsx("span",{className:"text-muted-foreground font-mono",children:w.ip||"—"}),e.jsxs("span",{className:"text-muted-foreground flex items-center gap-1",children:[_s(w.lastSeen),w.suspicious&&e.jsx("span",{className:"text-[10px] px-1.5 py-0.5 rounded bg-destructive text-destructive-foreground",children:"impossible travel"})]}),e.jsx("span",{className:"flex justify-end",children:s&&w.user_id&&(se?e.jsx("span",{className:"text-[10px] px-2 py-1 rounded bg-muted text-muted-foreground font-medium",children:"Blocked"}):e.jsxs(zt,{children:[e.jsx(Bt,{asChild:!0,children:e.jsxs(M,{size:"sm",variant:w.suspicious?"destructive":"outline",className:"h-7 text-[11px] gap-1",disabled:x===w.user_id,children:[x===w.user_id?e.jsx(He,{className:"h-3 w-3 animate-spin"}):e.jsx(Et,{className:"h-3 w-3"}),"Block"]})}),e.jsxs(It,{children:[e.jsxs(Ot,{children:[e.jsx($t,{children:"Block this account?"}),e.jsxs(Ut,{children:[e.jsx("strong",{children:w.email})," will lose access immediately and all active sessions will be revoked. You can reactivate later from Client Management."]})]}),e.jsxs(qt,{children:[e.jsx(Yt,{children:"Cancel"}),e.jsx(Kt,{onClick:()=>R(w.user_id,w.email),className:"bg-destructive text-destructive-foreground hover:bg-destructive/90",children:"Block account"})]})]})]}))})]},`${w.email}-${H}`)})})})]},v.country)})})})]})};function ht(s,t,n,a,r){a=a||{};var i=1.15,c=r.internal.scaleFactor,d=r.internal.getFontSize()/c,o=r.getLineHeightFactor?r.getLineHeightFactor():i,l=d*o,h=/\r\n|\r|\n/g,f="",x=1;if((a.valign==="middle"||a.valign==="bottom"||a.halign==="center"||a.halign==="right")&&(f=typeof s=="string"?s.split(h):s,x=f.length||1),n+=d*(2-i),a.valign==="middle"?n-=x/2*l:a.valign==="bottom"&&(n-=x*l),a.halign==="center"||a.halign==="right"){var p=d;if(a.halign==="center"&&(p*=.5),f&&x>=1){for(var j=0;j<f.length;j++)r.text(f[j],t-r.getStringUnitWidth(f[j])*p,n),n+=l;return r}t-=r.getStringUnitWidth(s)*p}return a.halign==="justify"?r.text(s,t,n,{maxWidth:a.maxWidth||100,align:"justify"}):r.text(s,t,n),r}var qe={},re=function(){function s(t){this.jsPDFDocument=t,this.userStyles={textColor:t.getTextColor?this.jsPDFDocument.getTextColor():0,fontSize:t.internal.getFontSize(),fontStyle:t.internal.getFont().fontStyle,font:t.internal.getFont().fontName,lineWidth:t.getLineWidth?this.jsPDFDocument.getLineWidth():0,lineColor:t.getDrawColor?this.jsPDFDocument.getDrawColor():0}}return s.setDefaults=function(t,n){n===void 0&&(n=null),n?n.__autoTableDocumentDefaults=t:qe=t},s.unifyColor=function(t){return Array.isArray(t)?t:typeof t=="number"?[t,t,t]:typeof t=="string"?[t]:null},s.prototype.applyStyles=function(t,n){var a,r,i;n===void 0&&(n=!1),t.fontStyle&&this.jsPDFDocument.setFontStyle&&this.jsPDFDocument.setFontStyle(t.fontStyle);var c=this.jsPDFDocument.internal.getFont(),d=c.fontStyle,o=c.fontName;if(t.font&&(o=t.font),t.fontStyle){d=t.fontStyle;var l=this.getFontList()[o];l&&l.indexOf(d)===-1&&this.jsPDFDocument.setFontStyle&&(this.jsPDFDocument.setFontStyle(l[0]),d=l[0])}if(this.jsPDFDocument.setFont(o,d),t.fontSize&&this.jsPDFDocument.setFontSize(t.fontSize),!n){var h=s.unifyColor(t.fillColor);h&&(a=this.jsPDFDocument).setFillColor.apply(a,h),h=s.unifyColor(t.textColor),h&&(r=this.jsPDFDocument).setTextColor.apply(r,h),h=s.unifyColor(t.lineColor),h&&(i=this.jsPDFDocument).setDrawColor.apply(i,h),typeof t.lineWidth=="number"&&this.jsPDFDocument.setLineWidth(t.lineWidth)}},s.prototype.splitTextToSize=function(t,n,a){return this.jsPDFDocument.splitTextToSize(t,n,a)},s.prototype.rect=function(t,n,a,r,i){return this.jsPDFDocument.rect(t,n,a,r,i)},s.prototype.getLastAutoTable=function(){return this.jsPDFDocument.lastAutoTable||null},s.prototype.getTextWidth=function(t){return this.jsPDFDocument.getTextWidth(t)},s.prototype.getDocument=function(){return this.jsPDFDocument},s.prototype.setPage=function(t){this.jsPDFDocument.setPage(t)},s.prototype.addPage=function(){return this.jsPDFDocument.addPage()},s.prototype.getFontList=function(){return this.jsPDFDocument.getFontList()},s.prototype.getGlobalOptions=function(){return qe||{}},s.prototype.getDocumentOptions=function(){return this.jsPDFDocument.__autoTableDocumentDefaults||{}},s.prototype.pageSize=function(){var t=this.jsPDFDocument.internal.pageSize;return t.width==null&&(t={width:t.getWidth(),height:t.getHeight()}),t},s.prototype.scaleFactor=function(){return this.jsPDFDocument.internal.scaleFactor},s.prototype.getLineHeightFactor=function(){var t=this.jsPDFDocument;return t.getLineHeightFactor?t.getLineHeightFactor():1.15},s.prototype.getLineHeight=function(t){return t/this.scaleFactor()*this.getLineHeightFactor()},s.prototype.pageNumber=function(){var t=this.jsPDFDocument.internal.getCurrentPageInfo();return t?t.pageNumber:this.jsPDFDocument.internal.getNumberOfPages()},s}(),Fe=function(s,t){return Fe=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(n,a){n.__proto__=a}||function(n,a){for(var r in a)Object.prototype.hasOwnProperty.call(a,r)&&(n[r]=a[r])},Fe(s,t)};function ut(s,t){if(typeof t!="function"&&t!==null)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");Fe(s,t);function n(){this.constructor=s}s.prototype=t===null?Object.create(t):(n.prototype=t.prototype,new n)}var ft=function(s){ut(t,s);function t(n){var a=s.call(this)||this;return a._element=n,a}return t}(Array);function Cs(s){return{font:"helvetica",fontStyle:"normal",overflow:"linebreak",fillColor:!1,textColor:20,halign:"left",valign:"top",fontSize:10,cellPadding:5/s,lineColor:200,lineWidth:0,cellWidth:"auto",minCellHeight:0,minCellWidth:0}}function Ds(s){var t={striped:{table:{fillColor:255,textColor:80,fontStyle:"normal"},head:{textColor:255,fillColor:[41,128,185],fontStyle:"bold"},body:{},foot:{textColor:255,fillColor:[41,128,185],fontStyle:"bold"},alternateRow:{fillColor:245}},grid:{table:{fillColor:255,textColor:80,fontStyle:"normal",lineWidth:.1},head:{textColor:255,fillColor:[26,188,156],fontStyle:"bold",lineWidth:0},body:{},foot:{textColor:255,fillColor:[26,188,156],fontStyle:"bold",lineWidth:0},alternateRow:{}},plain:{head:{fontStyle:"bold"},foot:{fontStyle:"bold"}}};return t[s]}function me(s,t,n){n.applyStyles(t,!0);var a=Array.isArray(s)?s:[s],r=a.map(function(i){return n.getTextWidth(i)}).reduce(function(i,c){return Math.max(i,c)},0);return r}function xt(s,t,n,a){var r=t.settings.tableLineWidth,i=t.settings.tableLineColor;s.applyStyles({lineWidth:r,lineColor:i});var c=gt(r,!1);c&&s.rect(n.x,n.y,t.getWidth(s.pageSize().width),a.y-n.y,c)}function gt(s,t){var n=s>0,a=t||t===0;return n&&a?"DF":n?"S":a?"F":null}function ve(s,t){var n,a,r,i;if(s=s||t,Array.isArray(s)){if(s.length>=4)return{top:s[0],right:s[1],bottom:s[2],left:s[3]};if(s.length===3)return{top:s[0],right:s[1],bottom:s[2],left:s[1]};if(s.length===2)return{top:s[0],right:s[1],bottom:s[0],left:s[1]};s.length===1?s=s[0]:s=t}return typeof s=="object"?(typeof s.vertical=="number"&&(s.top=s.vertical,s.bottom=s.vertical),typeof s.horizontal=="number"&&(s.right=s.horizontal,s.left=s.horizontal),{left:(n=s.left)!==null&&n!==void 0?n:t,top:(a=s.top)!==null&&a!==void 0?a:t,right:(r=s.right)!==null&&r!==void 0?r:t,bottom:(i=s.bottom)!==null&&i!==void 0?i:t}):(typeof s!="number"&&(s=t),{top:s,right:s,bottom:s,left:s})}function mt(s,t){var n=ve(t.settings.margin,0);return s.pageSize().width-(n.left+n.right)}function Ps(s,t,n,a,r){var i={},c=1.3333333333333333,d=we(t,function(v){return r.getComputedStyle(v).backgroundColor});d!=null&&(i.fillColor=d);var o=we(t,function(v){return r.getComputedStyle(v).color});o!=null&&(i.textColor=o);var l=Fs(a,n);l&&(i.cellPadding=l);var h="borderTopColor",f=c*n,x=a.borderTopWidth;if(a.borderBottomWidth===x&&a.borderRightWidth===x&&a.borderLeftWidth===x){var p=(parseFloat(x)||0)/f;p&&(i.lineWidth=p)}else i.lineWidth={top:(parseFloat(a.borderTopWidth)||0)/f,right:(parseFloat(a.borderRightWidth)||0)/f,bottom:(parseFloat(a.borderBottomWidth)||0)/f,left:(parseFloat(a.borderLeftWidth)||0)/f},i.lineWidth.top||(i.lineWidth.right?h="borderRightColor":i.lineWidth.bottom?h="borderBottomColor":i.lineWidth.left&&(h="borderLeftColor"));var j=we(t,function(v){return r.getComputedStyle(v)[h]});j!=null&&(i.lineColor=j);var R=["left","right","center","justify"];R.indexOf(a.textAlign)!==-1&&(i.halign=a.textAlign),R=["middle","bottom","top"],R.indexOf(a.verticalAlign)!==-1&&(i.valign=a.verticalAlign);var S=parseInt(a.fontSize||"");isNaN(S)||(i.fontSize=S/c);var N=Rs(a);N&&(i.fontStyle=N);var P=(a.fontFamily||"").toLowerCase();return s.indexOf(P)!==-1&&(i.font=P),i}function Rs(s){var t="";return(s.fontWeight==="bold"||s.fontWeight==="bolder"||parseInt(s.fontWeight)>=700)&&(t="bold"),(s.fontStyle==="italic"||s.fontStyle==="oblique")&&(t+="italic"),t}function we(s,t){var n=pt(s,t);if(!n)return null;var a=n.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d*\.?\d*))?\)$/);if(!a||!Array.isArray(a))return null;var r=[parseInt(a[1]),parseInt(a[2]),parseInt(a[3])],i=parseInt(a[4]);return i===0||isNaN(r[0])||isNaN(r[1])||isNaN(r[2])?null:r}function pt(s,t){var n=t(s);return n==="rgba(0, 0, 0, 0)"||n==="transparent"||n==="initial"||n==="inherit"?s.parentElement==null?null:pt(s.parentElement,t):n}function Fs(s,t){var n=[s.paddingTop,s.paddingRight,s.paddingBottom,s.paddingLeft],a=96/(72/t),r=(parseInt(s.lineHeight)-parseInt(s.fontSize))/t/2,i=n.map(function(d){return parseInt(d||"0")/a}),c=ve(i,0);return r>c.top&&(c.top=r),r>c.bottom&&(c.bottom=r),c}function vt(s,t,n,a,r){var i,c;a===void 0&&(a=!1),r===void 0&&(r=!1);var d;typeof t=="string"?d=n.document.querySelector(t):d=t;var o=Object.keys(s.getFontList()),l=s.scaleFactor(),h=[],f=[],x=[];if(!d)return console.error("Html table could not be found with input: ",t),{head:h,body:f,foot:x};for(var p=0;p<d.rows.length;p++){var j=d.rows[p],R=(c=(i=j==null?void 0:j.parentElement)===null||i===void 0?void 0:i.tagName)===null||c===void 0?void 0:c.toLowerCase(),S=Ms(o,l,n,j,a,r);S&&(R==="thead"?h.push(S):R==="tfoot"?x.push(S):f.push(S))}return{head:h,body:f,foot:x}}function Ms(s,t,n,a,r,i){for(var c=new ft(a),d=0;d<a.cells.length;d++){var o=a.cells[d],l=n.getComputedStyle(o);if(r||l.display!=="none"){var h=void 0;i&&(h=Ps(s,o,t,l,n)),c.push({rowSpan:o.rowSpan,colSpan:o.colSpan,styles:h,_element:o,content:Ts(o)})}}var f=n.getComputedStyle(a);if(c.length>0&&(r||f.display!=="none"))return c}function Ts(s){var t=s.cloneNode(!0);return t.innerHTML=t.innerHTML.replace(/\n/g,"").replace(/ +/g," "),t.innerHTML=t.innerHTML.split(/<br.*?>/).map(function(n){return n.trim()}).join(`
`),t.innerText||t.textContent||""}function Hs(s,t,n){for(var a=0,r=[s,t,n];a<r.length;a++){var i=r[a];i&&typeof i!="object"&&console.error("The options parameter should be of type object, is: "+typeof i),i.startY&&typeof i.startY!="number"&&(console.error("Invalid value for startY option",i.startY),delete i.startY)}}function Z(s,t,n,a,r){if(s==null)throw new TypeError("Cannot convert undefined or null to object");for(var i=Object(s),c=1;c<arguments.length;c++){var d=arguments[c];if(d!=null)for(var o in d)Object.prototype.hasOwnProperty.call(d,o)&&(i[o]=d[o])}return i}function yt(s,t){var n=new re(s),a=n.getDocumentOptions(),r=n.getGlobalOptions();Hs(r,a,t);var i=Z({},r,a,t),c;typeof window<"u"&&(c=window);var d=As(r,a,t),o=Ls(r,a,t),l=Ws(n,i),h=Bs(n,i,c);return{id:t.tableId,content:h,hooks:o,styles:d,settings:l}}function As(s,t,n){for(var a={styles:{},headStyles:{},bodyStyles:{},footStyles:{},alternateRowStyles:{},columnStyles:{}},r=function(o){if(o==="columnStyles"){var l=s[o],h=t[o],f=n[o];a.columnStyles=Z({},l,h,f)}else{var x=[s,t,n],p=x.map(function(j){return j[o]||{}});a[o]=Z({},p[0],p[1],p[2])}},i=0,c=Object.keys(a);i<c.length;i++){var d=c[i];r(d)}return a}function Ls(s,t,n){for(var a=[s,t,n],r={didParseCell:[],willDrawCell:[],didDrawCell:[],willDrawPage:[],didDrawPage:[]},i=0,c=a;i<c.length;i++){var d=c[i];d.didParseCell&&r.didParseCell.push(d.didParseCell),d.willDrawCell&&r.willDrawCell.push(d.willDrawCell),d.didDrawCell&&r.didDrawCell.push(d.didDrawCell),d.willDrawPage&&r.willDrawPage.push(d.willDrawPage),d.didDrawPage&&r.didDrawPage.push(d.didDrawPage)}return r}function Ws(s,t){var n,a,r,i,c,d,o,l,h,f,x,p,j=ve(t.margin,40/s.scaleFactor()),R=(n=zs(s,t.startY))!==null&&n!==void 0?n:j.top,S;t.showFoot===!0?S="everyPage":t.showFoot===!1?S="never":S=(a=t.showFoot)!==null&&a!==void 0?a:"everyPage";var N;t.showHead===!0?N="everyPage":t.showHead===!1?N="never":N=(r=t.showHead)!==null&&r!==void 0?r:"everyPage";var P=(i=t.useCss)!==null&&i!==void 0?i:!1,v=t.theme||(P?"plain":"striped"),u=!!t.horizontalPageBreak,k=(c=t.horizontalPageBreakRepeat)!==null&&c!==void 0?c:null;return{includeHiddenHtml:(d=t.includeHiddenHtml)!==null&&d!==void 0?d:!1,useCss:P,theme:v,startY:R,margin:j,pageBreak:(o=t.pageBreak)!==null&&o!==void 0?o:"auto",rowPageBreak:(l=t.rowPageBreak)!==null&&l!==void 0?l:"auto",tableWidth:(h=t.tableWidth)!==null&&h!==void 0?h:"auto",showHead:N,showFoot:S,tableLineWidth:(f=t.tableLineWidth)!==null&&f!==void 0?f:0,tableLineColor:(x=t.tableLineColor)!==null&&x!==void 0?x:200,horizontalPageBreak:u,horizontalPageBreakRepeat:k,horizontalPageBreakBehaviour:(p=t.horizontalPageBreakBehaviour)!==null&&p!==void 0?p:"afterAllRows"}}function zs(s,t){var n=s.getLastAutoTable(),a=s.scaleFactor(),r=s.pageNumber(),i=!1;if(n&&n.startPageNumber){var c=n.startPageNumber+n.pageNumber-1;i=c===r}return typeof t=="number"?t:(t==null||t===!1)&&i&&(n==null?void 0:n.finalY)!=null?n.finalY+20/a:null}function Bs(s,t,n){var a=t.head||[],r=t.body||[],i=t.foot||[];if(t.html){var c=t.includeHiddenHtml;if(n){var d=vt(s,t.html,n,c,t.useCss)||{};a=d.head||a,r=d.body||a,i=d.foot||a}else console.error("Cannot parse html in non browser environment")}var o=t.columns||Es(a,r,i);return{columns:o,head:a,body:r,foot:i}}function Es(s,t,n){var a=s[0]||t[0]||n[0]||[],r=[];return Object.keys(a).filter(function(i){return i!=="_element"}).forEach(function(i){var c=1,d;Array.isArray(a)?d=a[parseInt(i)]:d=a[i],typeof d=="object"&&!Array.isArray(d)&&(c=(d==null?void 0:d.colSpan)||1);for(var o=0;o<c;o++){var l=void 0;Array.isArray(a)?l=r.length:l=i+(o>0?"_".concat(o):"");var h={dataKey:l};r.push(h)}}),r}var Me=function(){function s(t,n,a){this.table=n,this.pageNumber=n.pageNumber,this.settings=n.settings,this.cursor=a,this.doc=t.getDocument()}return s}(),Is=function(s){ut(t,s);function t(n,a,r,i,c,d){var o=s.call(this,n,a,d)||this;return o.cell=r,o.row=i,o.column=c,o.section=i.section,o}return t}(Me),Os=function(){function s(t,n){this.pageNumber=1,this.id=t.id,this.settings=t.settings,this.styles=t.styles,this.hooks=t.hooks,this.columns=n.columns,this.head=n.head,this.body=n.body,this.foot=n.foot}return s.prototype.getHeadHeight=function(t){return this.head.reduce(function(n,a){return n+a.getMaxCellHeight(t)},0)},s.prototype.getFootHeight=function(t){return this.foot.reduce(function(n,a){return n+a.getMaxCellHeight(t)},0)},s.prototype.allRows=function(){return this.head.concat(this.body).concat(this.foot)},s.prototype.callCellHooks=function(t,n,a,r,i,c){for(var d=0,o=n;d<o.length;d++){var l=o[d],h=new Is(t,this,a,r,i,c),f=l(h)===!1;if(a.text=Array.isArray(a.text)?a.text:[a.text],f)return!1}return!0},s.prototype.callEndPageHooks=function(t,n){t.applyStyles(t.userStyles);for(var a=0,r=this.hooks.didDrawPage;a<r.length;a++){var i=r[a];i(new Me(t,this,n))}},s.prototype.callWillDrawPageHooks=function(t,n){for(var a=0,r=this.hooks.willDrawPage;a<r.length;a++){var i=r[a];i(new Me(t,this,n))}},s.prototype.getWidth=function(t){if(typeof this.settings.tableWidth=="number")return this.settings.tableWidth;if(this.settings.tableWidth==="wrap"){var n=this.columns.reduce(function(r,i){return r+i.wrappedWidth},0);return n}else{var a=this.settings.margin;return t-a.left-a.right}},s}(),jt=function(){function s(t,n,a,r,i){i===void 0&&(i=!1),this.height=0,this.raw=t,t instanceof ft&&(this.raw=t._element,this.element=t._element),this.index=n,this.section=a,this.cells=r,this.spansMultiplePages=i}return s.prototype.getMaxCellHeight=function(t){var n=this;return t.reduce(function(a,r){var i;return Math.max(a,((i=n.cells[r.index])===null||i===void 0?void 0:i.height)||0)},0)},s.prototype.hasRowSpan=function(t){var n=this;return t.filter(function(a){var r=n.cells[a.index];return r?r.rowSpan>1:!1}).length>0},s.prototype.canEntireRowFit=function(t,n){return this.getMaxCellHeight(n)<=t},s.prototype.getMinimumRowHeight=function(t,n){var a=this;return t.reduce(function(r,i){var c=a.cells[i.index];if(!c)return 0;var d=n.getLineHeight(c.styles.fontSize),o=c.padding("vertical"),l=o+d;return l>r?l:r},0)},s}(),wt=function(){function s(t,n,a){var r;this.contentHeight=0,this.contentWidth=0,this.wrappedWidth=0,this.minReadableWidth=0,this.minWidth=0,this.width=0,this.height=0,this.x=0,this.y=0,this.styles=n,this.section=a,this.raw=t;var i=t;t!=null&&typeof t=="object"&&!Array.isArray(t)?(this.rowSpan=t.rowSpan||1,this.colSpan=t.colSpan||1,i=(r=t.content)!==null&&r!==void 0?r:t,t._element&&(this.raw=t._element)):(this.rowSpan=1,this.colSpan=1);var c=i!=null?""+i:"",d=/\r\n|\r|\n/g;this.text=c.split(d)}return s.prototype.getTextPos=function(){var t;if(this.styles.valign==="top")t=this.y+this.padding("top");else if(this.styles.valign==="bottom")t=this.y+this.height-this.padding("bottom");else{var n=this.height-this.padding("vertical");t=this.y+n/2+this.padding("top")}var a;if(this.styles.halign==="right")a=this.x+this.width-this.padding("right");else if(this.styles.halign==="center"){var r=this.width-this.padding("horizontal");a=this.x+r/2+this.padding("left")}else a=this.x+this.padding("left");return{x:a,y:t}},s.prototype.getContentHeight=function(t,n){n===void 0&&(n=1.15);var a=Array.isArray(this.text)?this.text.length:1,r=this.styles.fontSize/t*n,i=a*r+this.padding("vertical");return Math.max(i,this.styles.minCellHeight)},s.prototype.padding=function(t){var n=ve(this.styles.cellPadding,0);return t==="vertical"?n.top+n.bottom:t==="horizontal"?n.left+n.right:n[t]},s}(),$s=function(){function s(t,n,a){this.wrappedWidth=0,this.minReadableWidth=0,this.minWidth=0,this.width=0,this.dataKey=t,this.raw=n,this.index=a}return s.prototype.getMaxCustomCellWidth=function(t){for(var n=0,a=0,r=t.allRows();a<r.length;a++){var i=r[a],c=i.cells[this.index];c&&typeof c.styles.cellWidth=="number"&&(n=Math.max(n,c.styles.cellWidth))}return n},s}();function Us(s,t){qs(s,t);var n=[],a=0;t.columns.forEach(function(i){var c=i.getMaxCustomCellWidth(t);c?i.width=c:(i.width=i.wrappedWidth,n.push(i)),a+=i.width});var r=t.getWidth(s.pageSize().width)-a;r&&(r=Te(n,r,function(i){return Math.max(i.minReadableWidth,i.minWidth)})),r&&(r=Te(n,r,function(i){return i.minWidth})),r=Math.abs(r),!t.settings.horizontalPageBreak&&r>.1/s.scaleFactor()&&(r=r<1?r:Math.round(r),console.log("Of the table content, ".concat(r," units width could not fit page"))),Ks(t),Xs(t,s),Ys(t)}function qs(s,t){var n=s.scaleFactor(),a=t.settings.horizontalPageBreak,r=mt(s,t);t.allRows().forEach(function(i){for(var c=0,d=t.columns;c<d.length;c++){var o=d[c],l=i.cells[o.index];if(l){var h=t.hooks.didParseCell;t.callCellHooks(s,h,l,i,o,null);var f=l.padding("horizontal");l.contentWidth=me(l.text,l.styles,s)+f;var x=me(l.text.join(" ").split(/[^\S\u00A0]+/),l.styles,s);if(l.minReadableWidth=x+l.padding("horizontal"),typeof l.styles.cellWidth=="number")l.minWidth=l.styles.cellWidth,l.wrappedWidth=l.styles.cellWidth;else if(l.styles.cellWidth==="wrap"||a===!0)l.contentWidth>r?(l.minWidth=r,l.wrappedWidth=r):(l.minWidth=l.contentWidth,l.wrappedWidth=l.contentWidth);else{var p=10/n;l.minWidth=l.styles.minCellWidth||p,l.wrappedWidth=l.contentWidth,l.minWidth>l.wrappedWidth&&(l.wrappedWidth=l.minWidth)}}}}),t.allRows().forEach(function(i){for(var c=0,d=t.columns;c<d.length;c++){var o=d[c],l=i.cells[o.index];if(l&&l.colSpan===1)o.wrappedWidth=Math.max(o.wrappedWidth,l.wrappedWidth),o.minWidth=Math.max(o.minWidth,l.minWidth),o.minReadableWidth=Math.max(o.minReadableWidth,l.minReadableWidth);else{var h=t.styles.columnStyles[o.dataKey]||t.styles.columnStyles[o.index]||{},f=h.cellWidth||h.minCellWidth;f&&typeof f=="number"&&(o.minWidth=f,o.wrappedWidth=f)}l&&(l.colSpan>1&&!o.minWidth&&(o.minWidth=l.minWidth),l.colSpan>1&&!o.wrappedWidth&&(o.wrappedWidth=l.minWidth))}})}function Te(s,t,n){for(var a=t,r=s.reduce(function(p,j){return p+j.wrappedWidth},0),i=0;i<s.length;i++){var c=s[i],d=c.wrappedWidth/r,o=a*d,l=c.width+o,h=n(c),f=l<h?h:l;t-=f-c.width,c.width=f}if(t=Math.round(t*1e10)/1e10,t){var x=s.filter(function(p){return t<0?p.width>n(p):!0});x.length&&(t=Te(x,t,n))}return t}function Ys(s){for(var t={},n=1,a=s.allRows(),r=0;r<a.length;r++)for(var i=a[r],c=0,d=s.columns;c<d.length;c++){var o=d[c],l=t[o.index];if(n>1)n--,delete i.cells[o.index];else if(l)l.cell.height+=i.height,n=l.cell.colSpan,delete i.cells[o.index],l.left--,l.left<=1&&delete t[o.index];else{var h=i.cells[o.index];if(!h)continue;if(h.height=i.height,h.rowSpan>1){var f=a.length-r,x=h.rowSpan>f?f:h.rowSpan;t[o.index]={cell:h,left:x,row:i}}}}}function Ks(s){for(var t=s.allRows(),n=0;n<t.length;n++)for(var a=t[n],r=null,i=0,c=0,d=0;d<s.columns.length;d++){var o=s.columns[d];if(c-=1,c>1&&s.columns[d+1])i+=o.width,delete a.cells[o.index];else if(r){var l=r;delete a.cells[o.index],r=null,l.width=o.width+i}else{var l=a.cells[o.index];if(!l)continue;if(c=l.colSpan,i=0,l.colSpan>1){r=l,i+=o.width;continue}l.width=o.width+i}}}function Xs(s,t){for(var n={count:0,height:0},a=0,r=s.allRows();a<r.length;a++){for(var i=r[a],c=0,d=s.columns;c<d.length;c++){var o=d[c],l=i.cells[o.index];if(l){t.applyStyles(l.styles,!0);var h=l.width-l.padding("horizontal");if(l.styles.overflow==="linebreak")l.text=t.splitTextToSize(l.text,h+1/t.scaleFactor(),{fontSize:l.styles.fontSize});else if(l.styles.overflow==="ellipsize")l.text=Ye(l.text,h,l.styles,t,"...");else if(l.styles.overflow==="hidden")l.text=Ye(l.text,h,l.styles,t,"");else if(typeof l.styles.overflow=="function"){var f=l.styles.overflow(l.text,h);typeof f=="string"?l.text=[f]:l.text=f}l.contentHeight=l.getContentHeight(t.scaleFactor(),t.getLineHeightFactor());var x=l.contentHeight/l.rowSpan;l.rowSpan>1&&n.count*n.height<x*l.rowSpan?n={height:x,count:l.rowSpan}:n&&n.count>0&&n.height>x&&(x=n.height),x>i.height&&(i.height=x)}}n.count--}}function Ye(s,t,n,a,r){return s.map(function(i){return Vs(i,t,n,a,r)})}function Vs(s,t,n,a,r){var i=1e4*a.scaleFactor();if(t=Math.ceil(t*i)/i,t>=me(s,n,a))return s;for(;t<me(s+r,n,a)&&!(s.length<=1);)s=s.substring(0,s.length-1);return s.trim()+r}function Nt(s,t){var n=new re(s),a=Gs(t,n.scaleFactor()),r=new Os(t,a);return Us(n,r),n.applyStyles(n.userStyles),r}function Gs(s,t){var n=s.content,a=Qs(n.columns);if(n.head.length===0){var r=Ke(a,"head");r&&n.head.push(r)}if(n.foot.length===0){var r=Ke(a,"foot");r&&n.foot.push(r)}var i=s.settings.theme,c=s.styles;return{columns:a,head:Ne("head",n.head,a,c,i,t),body:Ne("body",n.body,a,c,i,t),foot:Ne("foot",n.foot,a,c,i,t)}}function Ne(s,t,n,a,r,i){var c={},d=t.map(function(o,l){for(var h=0,f={},x=0,p=0,j=0,R=n;j<R.length;j++){var S=R[j];if(c[S.index]==null||c[S.index].left===0)if(p===0){var N=void 0;Array.isArray(o)?N=o[S.index-x-h]:N=o[S.dataKey];var P={};typeof N=="object"&&!Array.isArray(N)&&(P=(N==null?void 0:N.styles)||{});var v=Zs(s,S,l,r,a,i,P),u=new wt(N,v,s);f[S.dataKey]=u,f[S.index]=u,p=u.colSpan-1,c[S.index]={left:u.rowSpan-1,times:p}}else p--,x++;else c[S.index].left--,p=c[S.index].times,h++}return new jt(o,l,s,f)});return d}function Ke(s,t){var n={};return s.forEach(function(a){if(a.raw!=null){var r=Js(t,a.raw);r!=null&&(n[a.dataKey]=r)}}),Object.keys(n).length>0?n:null}function Js(s,t){if(s==="head"){if(typeof t=="object")return t.header||null;if(typeof t=="string"||typeof t=="number")return t}else if(s==="foot"&&typeof t=="object")return t.footer;return null}function Qs(s){return s.map(function(t,n){var a,r;return typeof t=="object"?r=(a=t.dataKey)!==null&&a!==void 0?a:n:r=n,new $s(r,t,n)})}function Zs(s,t,n,a,r,i,c){var d=Ds(a),o;s==="head"?o=r.headStyles:s==="body"?o=r.bodyStyles:s==="foot"&&(o=r.footStyles);var l=Z({},d.table,d[s],r.styles,o),h=r.columnStyles[t.dataKey]||r.columnStyles[t.index]||{},f=s==="body"?h:{},x=s==="body"&&n%2===0?Z({},d.alternateRow,r.alternateRowStyles):{},p=Cs(i),j=Z({},p,l,x,f);return Z(j,c)}function ea(s,t,n){var a;n===void 0&&(n={});var r=mt(s,t),i=new Map,c=[],d=[],o=[];Array.isArray(t.settings.horizontalPageBreakRepeat)?o=t.settings.horizontalPageBreakRepeat:(typeof t.settings.horizontalPageBreakRepeat=="string"||typeof t.settings.horizontalPageBreakRepeat=="number")&&(o=[t.settings.horizontalPageBreakRepeat]),o.forEach(function(x){var p=t.columns.find(function(j){return j.dataKey===x||j.index===x});p&&!i.has(p.index)&&(i.set(p.index,!0),c.push(p.index),d.push(t.columns[p.index]),r-=p.wrappedWidth)});for(var l=!0,h=(a=n==null?void 0:n.start)!==null&&a!==void 0?a:0;h<t.columns.length;){if(i.has(h)){h++;continue}var f=t.columns[h].wrappedWidth;if(l||r>=f)l=!1,c.push(h),d.push(t.columns[h]),r-=f;else break;h++}return{colIndexes:c,columns:d,lastIndex:h-1}}function ta(s,t){for(var n=[],a=0;a<t.columns.length;a++){var r=ea(s,t,{start:a});r.columns.length&&(n.push(r),a=r.lastIndex)}return n}function bt(s,t){var n=t.settings,a=n.startY,r=n.margin,i={x:r.left,y:a},c=t.getHeadHeight(t.columns)+t.getFootHeight(t.columns),d=a+r.bottom+c;if(n.pageBreak==="avoid"){var o=t.body,l=o.reduce(function(x,p){return x+p.height},0);d+=l}var h=new re(s);(n.pageBreak==="always"||n.startY!=null&&d>h.pageSize().height)&&(_t(h),i.y=r.top),t.callWillDrawPageHooks(h,i);var f=Z({},i);t.startPageNumber=h.pageNumber(),n.horizontalPageBreak?sa(h,t,f,i):(h.applyStyles(h.userStyles),(n.showHead==="firstPage"||n.showHead==="everyPage")&&t.head.forEach(function(x){return ee(h,t,x,i,t.columns)}),h.applyStyles(h.userStyles),t.body.forEach(function(x,p){var j=p===t.body.length-1;pe(h,t,x,j,f,i,t.columns)}),h.applyStyles(h.userStyles),(n.showFoot==="lastPage"||n.showFoot==="everyPage")&&t.foot.forEach(function(x){return ee(h,t,x,i,t.columns)})),xt(h,t,f,i),t.callEndPageHooks(h,i),t.finalY=i.y,s.lastAutoTable=t,h.applyStyles(h.userStyles)}function sa(s,t,n,a){var r=ta(s,t),i=t.settings;if(i.horizontalPageBreakBehaviour==="afterAllRows")r.forEach(function(l,h){s.applyStyles(s.userStyles),h>0?ce(s,t,n,a,l.columns,!0):Xe(s,t,a,l.columns),aa(s,t,n,a,l.columns),be(s,t,a,l.columns)});else for(var c=-1,d=r[0],o=function(){var l=c;if(d){s.applyStyles(s.userStyles);var h=d.columns;c>=0?ce(s,t,n,a,h,!0):Xe(s,t,a,h),l=Ve(s,t,c+1,a,h),be(s,t,a,h)}var f=l-c;r.slice(1).forEach(function(x){s.applyStyles(s.userStyles),ce(s,t,n,a,x.columns,!0),Ve(s,t,c+1,a,x.columns,f),be(s,t,a,x.columns)}),c=l};c<t.body.length-1;)o()}function Xe(s,t,n,a){var r=t.settings;s.applyStyles(s.userStyles),(r.showHead==="firstPage"||r.showHead==="everyPage")&&t.head.forEach(function(i){return ee(s,t,i,n,a)})}function aa(s,t,n,a,r){s.applyStyles(s.userStyles),t.body.forEach(function(i,c){var d=c===t.body.length-1;pe(s,t,i,d,n,a,r)})}function Ve(s,t,n,a,r,i){s.applyStyles(s.userStyles),i=i??t.body.length;for(var c=Math.min(n+i,t.body.length),d=-1,o=t.body.slice(n,c),l=0;l<o.length;l++){var h=o[l],f=n+l===t.body.length-1,x=St(s,t,f,a);if(h.canEntireRowFit(x,r))ee(s,t,h,a,r),d=n+l;else break}return d}function be(s,t,n,a){var r=t.settings;s.applyStyles(s.userStyles),(r.showFoot==="lastPage"||r.showFoot==="everyPage")&&t.foot.forEach(function(i){return ee(s,t,i,n,a)})}function na(s,t,n){var a=n.getLineHeight(s.styles.fontSize),r=s.padding("vertical"),i=Math.floor((t-r)/a);return Math.max(0,i)}function ra(s,t,n,a){var r={};s.spansMultiplePages=!0,s.height=0;for(var i=0,c=0,d=n.columns;c<d.length;c++){var o=d[c],l=s.cells[o.index];if(l){Array.isArray(l.text)||(l.text=[l.text]);var h=new wt(l.raw,l.styles,l.section);h=Z(h,l),h.text=[];var f=na(l,t,a);l.text.length>f&&(h.text=l.text.splice(f,l.text.length));var x=a.scaleFactor(),p=a.getLineHeightFactor();l.contentHeight=l.getContentHeight(x,p),l.contentHeight>=t&&(l.contentHeight=t,h.styles.minCellHeight-=t),l.contentHeight>s.height&&(s.height=l.contentHeight),h.contentHeight=h.getContentHeight(x,p),h.contentHeight>i&&(i=h.contentHeight),r[o.index]=h}}var j=new jt(s.raw,-1,s.section,r,!0);j.height=i;for(var R=0,S=n.columns;R<S.length;R++){var o=S[R],h=j.cells[o.index];h&&(h.height=j.height);var l=s.cells[o.index];l&&(l.height=s.height)}return j}function ia(s,t,n,a){var r=s.pageSize().height,i=a.settings.margin,c=i.top+i.bottom,d=r-c;t.section==="body"&&(d-=a.getHeadHeight(a.columns)+a.getFootHeight(a.columns));var o=t.getMinimumRowHeight(a.columns,s),l=o<n;if(o>d)return console.log("Will not be able to print row ".concat(t.index," correctly since it's minimum height is larger than page height")),!0;if(!l)return!1;var h=t.hasRowSpan(a.columns),f=t.getMaxCellHeight(a.columns)>d;return f?(h&&console.log("The content of row ".concat(t.index," will not be drawn correctly since drawing rows with a height larger than the page height and has cells with rowspans is not supported.")),!0):!(h||a.settings.rowPageBreak==="avoid")}function pe(s,t,n,a,r,i,c){var d=St(s,t,a,i);if(n.canEntireRowFit(d,c))ee(s,t,n,i,c);else if(ia(s,n,d,t)){var o=ra(n,d,t,s);ee(s,t,n,i,c),ce(s,t,r,i,c),pe(s,t,o,a,r,i,c)}else ce(s,t,r,i,c),pe(s,t,n,a,r,i,c)}function ee(s,t,n,a,r){a.x=t.settings.margin.left;for(var i=0,c=r;i<c.length;i++){var d=c[i],o=n.cells[d.index];if(!o){a.x+=d.width;continue}s.applyStyles(o.styles),o.x=a.x,o.y=a.y;var l=t.callCellHooks(s,t.hooks.willDrawCell,o,n,d,a);if(l===!1){a.x+=d.width;continue}oa(s,o,a);var h=o.getTextPos();ht(o.text,h.x,h.y,{halign:o.styles.halign,valign:o.styles.valign,maxWidth:Math.ceil(o.width-o.padding("left")-o.padding("right"))},s.getDocument()),t.callCellHooks(s,t.hooks.didDrawCell,o,n,d,a),a.x+=d.width}a.y+=n.height}function oa(s,t,n){var a=t.styles;if(s.getDocument().setFillColor(s.getDocument().getFillColor()),typeof a.lineWidth=="number"){var r=gt(a.lineWidth,a.fillColor);r&&s.rect(t.x,n.y,t.width,t.height,r)}else typeof a.lineWidth=="object"&&(a.fillColor&&s.rect(t.x,n.y,t.width,t.height,"F"),la(s,t,n,a.lineWidth))}function la(s,t,n,a){var r,i,c,d;a.top&&(r=n.x,i=n.y,c=n.x+t.width,d=n.y,a.right&&(c+=.5*a.right),a.left&&(r-=.5*a.left),o(a.top,r,i,c,d)),a.bottom&&(r=n.x,i=n.y+t.height,c=n.x+t.width,d=n.y+t.height,a.right&&(c+=.5*a.right),a.left&&(r-=.5*a.left),o(a.bottom,r,i,c,d)),a.left&&(r=n.x,i=n.y,c=n.x,d=n.y+t.height,a.top&&(i-=.5*a.top),a.bottom&&(d+=.5*a.bottom),o(a.left,r,i,c,d)),a.right&&(r=n.x+t.width,i=n.y,c=n.x+t.width,d=n.y+t.height,a.top&&(i-=.5*a.top),a.bottom&&(d+=.5*a.bottom),o(a.right,r,i,c,d));function o(l,h,f,x,p){s.getDocument().setLineWidth(l),s.getDocument().line(h,f,x,p,"S")}}function St(s,t,n,a){var r=t.settings.margin.bottom,i=t.settings.showFoot;return(i==="everyPage"||i==="lastPage"&&n)&&(r+=t.getFootHeight(t.columns)),s.pageSize().height-a.y-r}function ce(s,t,n,a,r,i){r===void 0&&(r=[]),i===void 0&&(i=!1),s.applyStyles(s.userStyles),t.settings.showFoot==="everyPage"&&!i&&t.foot.forEach(function(d){return ee(s,t,d,a,r)}),t.callEndPageHooks(s,a);var c=t.settings.margin;xt(s,t,n,a),_t(s),t.pageNumber++,a.x=c.left,a.y=c.top,n.y=c.top,t.callWillDrawPageHooks(s,a),t.settings.showHead==="everyPage"&&(t.head.forEach(function(d){return ee(s,t,d,a,r)}),s.applyStyles(s.userStyles))}function _t(s){var t=s.pageNumber();s.setPage(t+1);var n=s.pageNumber();return n===t?(s.addPage(),!0):!1}function ca(s){s.API.autoTable=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var a=t[0],r=yt(this,a),i=Nt(this,r);return bt(this,i),this},s.API.lastAutoTable=!1,s.API.autoTableText=function(t,n,a,r){ht(t,n,a,r,this)},s.API.autoTableSetDefaults=function(t){return re.setDefaults(t,this),this},s.autoTableSetDefaults=function(t,n){re.setDefaults(t,n)},s.API.autoTableHtmlToJson=function(t,n){var a;if(n===void 0&&(n=!1),typeof window>"u")return console.error("Cannot run autoTableHtmlToJson in non browser environment"),null;var r=new re(this),i=vt(r,t,window,n,!1),c=i.head,d=i.body,o=((a=c[0])===null||a===void 0?void 0:a.map(function(l){return l.content}))||[];return{columns:o,rows:d,data:d}}}var Se;function da(s,t){var n=yt(s,t),a=Nt(s,n);bt(s,a)}try{if(typeof window<"u"&&window){var Ge=window,Je=Ge.jsPDF||((Se=Ge.jspdf)===null||Se===void 0?void 0:Se.jsPDF);Je&&ca(Je)}}catch(s){console.error("Could not apply autoTable plugin",s)}const ha=s=>{if(s==null)return"";const t=String(s);return/[",\n\r]/.test(t)?`"${t.replace(/"/g,'""')}"`:t},kt=(s,t,n)=>{const a=[t.join(","),...n.map(d=>d.map(ha).join(","))].join(`
`),r=new Blob([a],{type:"text/csv;charset=utf-8"}),i=URL.createObjectURL(r),c=document.createElement("a");c.href=i,c.download=s,c.click(),URL.revokeObjectURL(i)},Ct=(s,t,n,a)=>{const r=new Xt({orientation:"landscape",unit:"pt",format:"a4"});r.setFontSize(16),r.setTextColor(20,33,61),r.text("ABTC — Audit Evidence Export",40,40),r.setFontSize(11),r.setTextColor(80),r.text(s,40,58),r.setFontSize(9),r.text(`Generated: ${q(new Date,"PPpp")} · ${a.length} records`,40,72),da(r,{startY:86,head:[n],body:a.map(c=>c.map(d=>d==null?"":String(d))),headStyles:{fillColor:[20,33,61],textColor:255,fontSize:9},bodyStyles:{fontSize:8,textColor:50},alternateRowStyles:{fillColor:[248,248,250]},margin:{top:86,left:40,right:40}});const i=r.internal.getNumberOfPages();for(let c=1;c<=i;c++)r.setPage(c),r.setFontSize(8),r.setTextColor(120),r.text(`Confidential — ABTC audit log · Page ${c} of ${i}`,40,r.internal.pageSize.getHeight()-20);r.save(t)},ua=({logs:s})=>{const[t,n]=b.useState(null),a=new Map;s.forEach(o=>{if(!o.user_email)return;const l=a.get(o.user_email)||{email:o.user_email,role:o.user_role||"client",count:0,last:o.created_at};l.count++,new Date(o.created_at)>new Date(l.last)&&(l.last=o.created_at),a.set(o.user_email,l)});const r=Array.from(a.values()).sort((o,l)=>l.count-o.count),i=t?s.filter(o=>o.user_email===t):[],c=()=>{t&&kt(`audit-${t.replace("@","_at_")}-${q(new Date,"yyyy-MM-dd")}.csv`,["When","Category","Action","Entity Type","Entity Name","IP"],i.map(o=>[o.created_at,o.action_category,o.action_type,o.entity_type||"",o.entity_name||"",o.ip_address||""]))},d=()=>{t&&Ct(`Activity audit trail — ${t}`,`audit-${t.replace("@","_at_")}-${q(new Date,"yyyy-MM-dd")}.pdf`,["When","Category","Action","Entity","IP"],i.map(o=>[q(new Date(o.created_at),"MMM d HH:mm"),o.action_category,o.action_type,o.entity_name||o.entity_id||"",o.ip_address||""]))};return e.jsxs(e.Fragment,{children:[e.jsxs(X,{children:[e.jsx(V,{children:e.jsxs(C,{children:[e.jsx(y,{children:"User"}),e.jsx(y,{children:"Role"}),e.jsx(y,{children:"Activities (recent)"}),e.jsx(y,{children:"Last seen action"}),e.jsx(y,{className:"text-right",children:"Drill-down"})]})}),e.jsx(G,{children:r.length===0?e.jsx(C,{children:e.jsx(g,{colSpan:5,className:"text-center py-8 text-muted-foreground",children:"No user activity yet."})}):r.map(o=>e.jsxs(C,{children:[e.jsx(g,{className:"font-body text-sm",children:o.email}),e.jsx(g,{children:e.jsx(z,{variant:"secondary",children:o.role})}),e.jsx(g,{className:"font-body text-sm",children:o.count}),e.jsx(g,{className:"font-body text-xs",children:ne(new Date(o.last),{addSuffix:!0})}),e.jsx(g,{className:"text-right",children:e.jsxs(M,{size:"sm",variant:"outline",onClick:()=>n(o.email),className:"gap-1.5 font-body",children:[e.jsx(st,{className:"h-3.5 w-3.5"})," View timeline"]})})]},o.email))})]}),e.jsx(Ae,{open:!!t,onOpenChange:o=>!o&&n(null),children:e.jsxs(Le,{className:"max-w-4xl max-h-[80vh] overflow-hidden flex flex-col",children:[e.jsx(We,{children:e.jsxs(ze,{className:"font-heading flex items-center justify-between",children:[e.jsxs("span",{children:["Activity timeline — ",t]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(M,{size:"sm",variant:"outline",onClick:c,className:"gap-1.5 font-body",children:[e.jsx(at,{className:"h-3.5 w-3.5"})," CSV"]}),e.jsxs(M,{size:"sm",variant:"outline",onClick:d,className:"gap-1.5 font-body",children:[e.jsx(_e,{className:"h-3.5 w-3.5"})," PDF"]})]})]})}),e.jsx("div",{className:"overflow-auto flex-1",children:e.jsxs(X,{children:[e.jsx(V,{children:e.jsxs(C,{children:[e.jsx(y,{children:"When"}),e.jsx(y,{children:"Category"}),e.jsx(y,{children:"Action"}),e.jsx(y,{children:"Entity"}),e.jsx(y,{children:"IP"})]})}),e.jsx(G,{children:i.map(o=>e.jsxs(C,{children:[e.jsx(g,{className:"font-body text-xs whitespace-nowrap",children:q(new Date(o.created_at),"MMM d, HH:mm:ss")}),e.jsx(g,{children:e.jsx(z,{variant:"outline",className:"font-body",children:o.action_category})}),e.jsx(g,{className:"font-body text-sm",children:o.action_type}),e.jsxs(g,{className:"font-body text-sm",children:[o.entity_name||o.entity_id||"—"," ",o.entity_type&&e.jsxs("span",{className:"text-xs text-muted-foreground",children:["(",o.entity_type,")"]})]}),e.jsx(g,{className:"font-mono text-xs",children:o.ip_address||"—"})]},o.id))})]})})]})})]})},fa=()=>{const{toast:s}=ie(),[t,n]=b.useState([]),[a,r]=b.useState(!0),[i,c]=b.useState(null),[d,o]=b.useState(null),[l,h]=b.useState(""),f=async()=>{r(!0);const{data:N,error:P}=await F.from("password_reset_requests").select("*").order("requested_at",{ascending:!1}).limit(200);P?s({title:"Failed to load",description:P.message,variant:"destructive"}):n(N||[]),r(!1)};b.useEffect(()=>{f();const N=setInterval(f,3e4);return()=>clearInterval(N)},[]);const x=async N=>{c(N.id);const{error:P}=await F.functions.invoke("approve-password-reset",{body:{request_id:N.id,decision:"approve"}});if(c(null),P){s({title:"Approve failed",description:P.message,variant:"destructive"});return}s({title:"Password reset approved",description:`${N.email} can now sign in with the new password.`}),f()},p=async()=>{if(!d)return;c(d.id);const{error:N}=await F.functions.invoke("approve-password-reset",{body:{request_id:d.id,decision:"reject",notes:l}});if(c(null),N){s({title:"Reject failed",description:N.message,variant:"destructive"});return}s({title:"Reset request rejected"}),o(null),h(""),f()},j=t.filter(N=>N.status==="pending"),R=t.filter(N=>N.status!=="pending"),S=(N,P)=>e.jsxs(X,{children:[e.jsx(V,{children:e.jsxs(C,{children:[e.jsx(y,{children:"Email"}),e.jsx(y,{children:"Requested"}),e.jsx(y,{children:"Status"}),e.jsx(y,{children:"IP"}),!P&&e.jsx(y,{children:"Reviewed"}),!P&&e.jsx(y,{children:"Notes"}),P&&e.jsx(y,{className:"text-right",children:"Actions"})]})}),e.jsx(G,{children:a?e.jsx(C,{children:e.jsx(g,{colSpan:P?5:6,className:"text-center py-8 text-muted-foreground",children:"Loading…"})}):N.length===0?e.jsx(C,{children:e.jsx(g,{colSpan:P?5:6,className:"text-center py-8 text-muted-foreground",children:"No requests."})}):N.map(v=>e.jsxs(C,{children:[e.jsx(g,{className:"font-body text-sm",children:v.email}),e.jsxs(g,{className:"font-body text-xs whitespace-nowrap",children:[q(new Date(v.requested_at),"MMM d, HH:mm"),e.jsx("span",{className:"block text-muted-foreground",children:ne(new Date(v.requested_at),{addSuffix:!0})})]}),e.jsx(g,{children:e.jsx(z,{variant:v.status==="approved"?"secondary":v.status==="rejected"?"destructive":"outline",children:v.status})}),e.jsx(g,{className:"font-mono text-xs",children:v.request_ip||"—"}),!P&&e.jsx(g,{className:"font-body text-xs",children:v.reviewed_at?q(new Date(v.reviewed_at),"MMM d, HH:mm"):"—"}),!P&&e.jsx(g,{className:"font-body text-xs text-muted-foreground max-w-[260px] truncate",children:v.admin_notes||"—"}),P&&e.jsx(g,{className:"text-right",children:e.jsxs("div",{className:"flex gap-2 justify-end",children:[e.jsxs(M,{size:"sm",onClick:()=>x(v),disabled:i===v.id,className:"font-heading gap-1.5 bg-accent text-accent-foreground hover:bg-accent/90",children:[i===v.id?e.jsx(He,{className:"h-3.5 w-3.5 animate-spin"}):e.jsx(Gt,{className:"h-3.5 w-3.5"}),"Approve"]}),e.jsxs(M,{size:"sm",variant:"outline",onClick:()=>{o(v),h("")},disabled:i===v.id,className:"font-heading gap-1.5",children:[e.jsx(Jt,{className:"h-3.5 w-3.5"})," Reject"]})]})})]},v.id))})]});return e.jsxs(L,{children:[e.jsx($,{children:e.jsxs(U,{className:"font-heading flex items-center gap-2",children:[e.jsx(dt,{className:"h-5 w-5 text-accent"}),"Password Reset Requests",j.length>0&&e.jsxs(z,{variant:"destructive",className:"ml-2",children:[j.length," pending"]})]})}),e.jsx(W,{children:e.jsxs(nt,{defaultValue:"pending",children:[e.jsxs(rt,{children:[e.jsxs(E,{value:"pending",className:"font-heading",children:["Pending (",j.length,")"]}),e.jsxs(E,{value:"history",className:"font-heading",children:["History (",R.length,")"]})]}),e.jsx(I,{value:"pending",className:"mt-4",children:S(j,!0)}),e.jsx(I,{value:"history",className:"mt-4",children:S(R,!1)})]})}),e.jsx(Ae,{open:!!d,onOpenChange:N=>!N&&o(null),children:e.jsxs(Le,{children:[e.jsx(We,{children:e.jsx(ze,{className:"font-heading",children:"Reject password reset"})}),e.jsxs("div",{className:"space-y-3",children:[e.jsx("p",{className:"text-sm font-body text-muted-foreground",children:"Rejecting will leave the account's current password unchanged. Optional note for the audit trail:"}),e.jsx(Vt,{value:l,onChange:N=>h(N.target.value),placeholder:"Reason for rejecting (optional)",rows:3})]}),e.jsxs(it,{children:[e.jsx(M,{variant:"outline",onClick:()=>o(null),className:"font-heading",children:"Cancel"}),e.jsx(M,{variant:"destructive",onClick:p,disabled:!!i,className:"font-heading",children:"Confirm rejection"})]})]})})]})},Qe=s=>{if(!s)return"—";const t=["B","KB","MB","GB"];let n=0,a=s;for(;a>=1024&&n<t.length-1;)a/=1024,n++;return`${a.toFixed(1)} ${t[n]}`},xa=()=>{const{toast:s}=ie(),[t,n]=b.useState([]),[a,r]=b.useState(null),[i,c]=b.useState(!1),[d,o]=b.useState(!1),[l,h]=b.useState(!1),[f,x]=b.useState(0),[p,j]=b.useState(""),[R,S]=b.useState(!1),[N,P]=b.useState(null),[v,u]=b.useState(null),[k,T]=b.useState(""),[_,w]=b.useState(!1),H=async()=>{c(!0);const[m,D]=await Promise.all([F.from("backup_jobs").select("*").order("started_at",{ascending:!1}).limit(50),F.from("backup_settings").select("*").limit(1).single()]);n(m.data||[]),r(D.data||null),c(!1)};b.useEffect(()=>{H()},[]);const se=async()=>{o(!0),x(0),j("Starting…"),s({title:"Backup started",description:l?"Including uploaded files — this may take several minutes.":"Database export running…"});const{data:m,error:D}=await F.functions.invoke("backup-create",{body:{job_type:"manual",include_storage:l}});if(D||!(m!=null&&m.job_id)){o(!1),s({title:"Backup failed to start",description:(D==null?void 0:D.message)||"No job id returned",variant:"destructive"}),H();return}const J=m.job_id,de=Date.now(),oe=async()=>{const{data:he}=await F.from("backup_jobs").select("status,error_message,file_size,ftp_uploaded,progress,progress_message").eq("id",J).single(),A=he,le=A==null?void 0:A.status;if(typeof(A==null?void 0:A.progress)=="number"&&x(A.progress),A!=null&&A.progress_message&&j(A.progress_message),le==="completed"){x(100),s({title:"Backup complete",description:`Size: ${Qe(A.file_size)} ${A.ftp_uploaded?"· uploaded to FTP":""}`}),o(!1),H();return}if(le==="failed"){s({title:"Backup failed",description:A.error_message||"Unknown error",variant:"destructive"}),o(!1),H();return}if(Date.now()-de>15*60*1e3){s({title:"Backup taking too long",description:"Check the history table later.",variant:"destructive"}),o(!1),H();return}H(),setTimeout(oe,2e3)};setTimeout(oe,1500)},ye=async m=>{if(!m.file_path)return;const{data:D,error:J}=await F.storage.from("backups").createSignedUrl(m.file_path,600);if(J||!D){s({title:"Could not generate link",description:J==null?void 0:J.message,variant:"destructive"});return}window.open(D.signedUrl,"_blank")},K=async m=>{confirm("Delete this backup permanently?")&&(m.file_path&&await F.storage.from("backups").remove([m.file_path]),await F.from("backup_jobs").delete().eq("id",m.id),H())},ae=async m=>{if(!a)return;const{error:D}=await F.from("backup_settings").update(m).eq("id",a.id);D?s({title:"Save failed",description:D.message,variant:"destructive"}):(s({title:"Saved"}),H())},Dt=async()=>{s({title:"Testing FTP…"});const{data:m,error:D}=await F.functions.invoke("backup-ftp-test",{body:{remote_path:a==null?void 0:a.ftp_remote_path}});D||!(m!=null&&m.success)?s({title:"FTP test failed",description:(D==null?void 0:D.message)||(m==null?void 0:m.error),variant:"destructive"}):s({title:"FTP connected",description:`${m.host}:${m.port} → ${m.remote_path} (${m.files_in_dir} files)`})},Pt=async()=>{var de,oe,he;if(k!=="RESTORE"){s({title:"Type RESTORE to confirm",variant:"destructive"});return}w(!0);let m={};if(N)m.backup_id=N;else if(v){const A=await v.arrayBuffer();let le="";const Be=new Uint8Array(A);for(let je=0;je<Be.byteLength;je++)le+=String.fromCharCode(Be[je]);m.zip_base64=btoa(le)}else{w(!1);return}const{data:D,error:J}=await F.functions.invoke("backup-restore",{body:m});if(w(!1),J)s({title:"Restore failed",description:J.message,variant:"destructive"});else{const A=((oe=(de=D==null?void 0:D.summary)==null?void 0:de.errors)==null?void 0:oe.length)||0;s({title:"Restore complete",description:`${Object.keys(((he=D==null?void 0:D.summary)==null?void 0:he.tables)||{}).length} tables · ${A} errors`}),S(!1),T(""),u(null),P(null)}};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-3",children:[e.jsxs(M,{onClick:se,disabled:d,className:"gap-2 font-heading",children:[e.jsx(ds,{className:"h-4 w-4"})," ",d?"Backing up…":"Backup Now"]}),e.jsxs("div",{className:"flex items-center gap-2 px-3 py-2 rounded-md border bg-muted/30",children:[e.jsx(fe,{id:"include-storage",checked:l,onCheckedChange:h,disabled:d}),e.jsxs(Y,{htmlFor:"include-storage",className:"font-body text-sm cursor-pointer",children:["Include uploaded files ",e.jsx("span",{className:"text-muted-foreground",children:"(slower, larger)"})]})]}),e.jsxs(M,{variant:"outline",onClick:()=>{P(null),u(null),S(!0)},className:"gap-2 font-heading",children:[e.jsx(ke,{className:"h-4 w-4"})," Restore from File"]}),e.jsxs(M,{variant:"outline",onClick:H,className:"gap-2 font-heading",children:[e.jsx(ot,{className:"h-4 w-4"})," Refresh"]})]}),d&&e.jsxs("div",{className:"rounded-md border bg-card p-4 space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between text-sm font-body",children:[e.jsx("span",{className:"text-foreground",children:p||"Working…"}),e.jsxs("span",{className:"font-mono text-accent font-heading",children:[f,"%"]})]}),e.jsx(Qt,{value:f,className:"h-2"})]})]}),e.jsxs("div",{className:"grid lg:grid-cols-2 gap-4",children:[e.jsxs(L,{children:[e.jsx($,{children:e.jsxs(U,{className:"font-heading flex items-center gap-2",children:[e.jsx(hs,{className:"h-5 w-5 text-accent"})," Shared Hosting (FTP)"]})}),e.jsxs(W,{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(Y,{className:"font-body",children:"Auto-upload backups to FTP"}),e.jsx(fe,{checked:!!(a!=null&&a.ftp_enabled),onCheckedChange:m=>ae({ftp_enabled:m})})]}),e.jsxs("div",{children:[e.jsx(Y,{className:"font-body text-xs",children:"Remote folder (cPanel path)"}),e.jsx(Q,{value:(a==null?void 0:a.ftp_remote_path)||"",onChange:m=>r(D=>D&&{...D,ftp_remote_path:m.target.value}),placeholder:"/public_html/backups"})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(M,{size:"sm",variant:"outline",onClick:()=>a&&ae({ftp_remote_path:a.ftp_remote_path}),children:"Save Path"}),e.jsxs(M,{size:"sm",variant:"outline",onClick:Dt,className:"gap-1",children:[e.jsx(is,{className:"h-3 w-3"})," Test Connection"]})]}),e.jsx("p",{className:"text-xs text-muted-foreground font-body",children:"FTP credentials are stored as encrypted secrets. Update them in project secrets if needed."})]})]}),e.jsxs(L,{children:[e.jsx($,{children:e.jsxs(U,{className:"font-heading flex items-center gap-2",children:[e.jsx(rs,{className:"h-5 w-5 text-accent"})," Schedule"]})}),e.jsxs(W,{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(Y,{className:"font-body",children:"Enable scheduled backups"}),e.jsx(fe,{checked:!!(a!=null&&a.schedule_enabled),onCheckedChange:m=>ae({schedule_enabled:m})})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx(Y,{className:"font-body text-xs",children:"Frequency"}),e.jsxs(Ce,{value:(a==null?void 0:a.schedule_frequency)||"daily",onValueChange:m=>ae({schedule_frequency:m}),children:[e.jsx(De,{children:e.jsx(Pe,{})}),e.jsxs(Re,{children:[e.jsx(O,{value:"daily",children:"Daily"}),e.jsx(O,{value:"weekly",children:"Weekly"}),e.jsx(O,{value:"monthly",children:"Monthly"})]})]})]}),e.jsxs("div",{children:[e.jsx(Y,{className:"font-body text-xs",children:"Hour (UTC)"}),e.jsx(Q,{type:"number",min:0,max:23,value:(a==null?void 0:a.schedule_hour_utc)??2,onChange:m=>ae({schedule_hour_utc:parseInt(m.target.value||"0")})})]})]}),(a==null?void 0:a.schedule_frequency)==="weekly"&&e.jsxs("div",{children:[e.jsx(Y,{className:"font-body text-xs",children:"Day of week"}),e.jsxs(Ce,{value:String(a.schedule_day_of_week),onValueChange:m=>ae({schedule_day_of_week:parseInt(m)}),children:[e.jsx(De,{children:e.jsx(Pe,{})}),e.jsx(Re,{children:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"].map((m,D)=>e.jsx(O,{value:String(D),children:m},D))})]})]}),(a==null?void 0:a.schedule_frequency)==="monthly"&&e.jsxs("div",{children:[e.jsx(Y,{className:"font-body text-xs",children:"Day of month (1–28)"}),e.jsx(Q,{type:"number",min:1,max:28,value:a.schedule_day_of_month,onChange:m=>ae({schedule_day_of_month:parseInt(m.target.value||"1")})})]}),e.jsxs("div",{children:[e.jsx(Y,{className:"font-body text-xs",children:"Retention (keep last N scheduled backups)"}),e.jsx(Q,{type:"number",min:1,max:365,value:(a==null?void 0:a.retention_count)??7,onChange:m=>ae({retention_count:parseInt(m.target.value||"7")})})]}),(a==null?void 0:a.last_run_at)&&e.jsxs("p",{className:"text-xs text-muted-foreground font-body",children:["Last scheduled run: ",ne(new Date(a.last_run_at),{addSuffix:!0})]})]})]})]}),e.jsxs(L,{children:[e.jsx($,{children:e.jsxs(U,{className:"font-heading flex items-center gap-2",children:[e.jsx(ls,{className:"h-5 w-5 text-accent"})," Backup History (",t.length,")"]})}),e.jsx(W,{className:"p-0",children:e.jsx("div",{className:"max-h-[500px] overflow-auto",children:e.jsxs(X,{children:[e.jsx(V,{children:e.jsxs(C,{children:[e.jsx(y,{children:"When"}),e.jsx(y,{children:"Type"}),e.jsx(y,{children:"Status"}),e.jsx(y,{children:"Size"}),e.jsx(y,{children:"Tables"}),e.jsx(y,{children:"Files"}),e.jsx(y,{children:"FTP"}),e.jsx(y,{children:"Actions"})]})}),e.jsx(G,{children:i?e.jsx(C,{children:e.jsx(g,{colSpan:8,className:"text-center py-8 text-muted-foreground",children:"Loading…"})}):t.length===0?e.jsx(C,{children:e.jsx(g,{colSpan:8,className:"text-center py-8 text-muted-foreground",children:'No backups yet. Click "Backup Now" to create one.'})}):t.map(m=>e.jsxs(C,{children:[e.jsx(g,{className:"font-body text-xs whitespace-nowrap",children:q(new Date(m.started_at),"MMM d, HH:mm")}),e.jsx(g,{children:e.jsx(z,{variant:"outline",className:"font-body",children:m.job_type})}),e.jsx(g,{children:e.jsx(z,{variant:m.status==="completed"?"secondary":m.status==="failed"?"destructive":"outline",children:m.status})}),e.jsx(g,{className:"font-mono text-xs",children:Qe(m.file_size)}),e.jsx(g,{className:"font-body text-xs",children:m.table_count}),e.jsx(g,{className:"font-body text-xs",children:m.file_count}),e.jsx(g,{children:m.ftp_uploaded?e.jsx(z,{variant:"secondary",className:"text-xs",children:"✓ uploaded"}):e.jsx("span",{className:"text-xs text-muted-foreground",children:"—"})}),e.jsxs(g,{children:[e.jsxs("div",{className:"flex gap-1",children:[m.status==="completed"&&e.jsxs(e.Fragment,{children:[e.jsx(M,{size:"sm",variant:"ghost",onClick:()=>ye(m),title:"Download",children:e.jsx(lt,{className:"h-3.5 w-3.5"})}),e.jsx(M,{size:"sm",variant:"ghost",onClick:()=>{P(m.id),u(null),S(!0)},title:"Restore",children:e.jsx(ke,{className:"h-3.5 w-3.5"})})]}),e.jsx(M,{size:"sm",variant:"ghost",onClick:()=>K(m),title:"Delete",children:e.jsx(et,{className:"h-3.5 w-3.5 text-destructive"})})]}),m.error_message&&e.jsx("p",{className:"text-xs text-destructive mt-1",children:m.error_message})]})]},m.id))})]})})})]}),e.jsx(Ae,{open:R,onOpenChange:S,children:e.jsxs(Le,{children:[e.jsx(We,{children:e.jsxs(ze,{className:"font-heading flex items-center gap-2",children:[e.jsx(ge,{className:"h-5 w-5 text-destructive"})," Confirm Restore"]})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("p",{className:"text-sm font-body",children:["This will ",e.jsx("strong",{children:"merge / upsert"})," all rows and storage files from the backup into the live database. Existing rows with matching IDs will be overwritten. No data will be deleted."]}),!N&&e.jsxs("div",{children:[e.jsx(Y,{className:"font-body text-xs",children:"Select backup .zip file"}),e.jsx(Q,{type:"file",accept:".zip",onChange:m=>{var D;return u(((D=m.target.files)==null?void 0:D[0])||null)}})]}),e.jsxs("div",{children:[e.jsx(Y,{className:"font-body text-xs",children:"Type RESTORE to confirm"}),e.jsx(Q,{value:k,onChange:m=>T(m.target.value),placeholder:"RESTORE"})]})]}),e.jsxs(it,{children:[e.jsx(M,{variant:"outline",onClick:()=>S(!1),children:"Cancel"}),e.jsx(M,{variant:"destructive",disabled:_||k!=="RESTORE"||!N&&!v,onClick:Pt,children:_?"Restoring…":"Restore Now"})]})]})})]})},wa=()=>{const[s,t]=b.useState(!0),[n,a]=b.useState([]),[r,i]=b.useState([]),[c,d]=b.useState([]),[o,l]=b.useState(""),[h,f]=b.useState("all"),[x,p]=b.useState(0),j=async()=>{t(!0);const u=new Date(Date.now()-15*60*1e3).toISOString(),[k,T,_,w,H]=await Promise.all([F.from("login_logs").select("*").order("created_at",{ascending:!1}).limit(500),F.from("activity_logs").select("*").order("created_at",{ascending:!1}).limit(500),F.from("profiles").select("id, full_name, email, last_seen_at").gte("last_seen_at",u).order("last_seen_at",{ascending:!1}),F.from("user_roles").select("user_id, role"),F.from("security_alerts").select("id",{count:"exact",head:!0}).eq("acknowledged",!1)]);a(k.data||[]),i(T.data||[]),p(H.count||0);const se=new Map;(w.data||[]).forEach(K=>se.set(K.user_id,K.role));const ye=(_.data||[]).map(K=>({id:K.id,full_name:K.full_name,email:K.email,last_seen_at:K.last_seen_at,role:se.get(K.id)||"client"}));d(ye),t(!1)};b.useEffect(()=>{j();const u=setInterval(j,3e4);return()=>clearInterval(u)},[]);const R=r.filter(u=>{var _,w;const k=!o||((_=u.user_email)==null?void 0:_.toLowerCase().includes(o.toLowerCase()))||u.action_type.toLowerCase().includes(o.toLowerCase())||((w=u.entity_name)==null?void 0:w.toLowerCase().includes(o.toLowerCase())),T=h==="all"||u.action_category===h;return k&&T}),S=n.filter(u=>{var k,T;return!o||((k=u.email)==null?void 0:k.toLowerCase().includes(o.toLowerCase()))||((T=u.ip_address)==null?void 0:T.includes(o))}),N={activeNow:c.length,loginsToday:n.filter(u=>new Date(u.created_at).toDateString()===new Date().toDateString()).length,failedLogins24h:n.filter(u=>!u.success&&Date.now()-new Date(u.created_at).getTime()<864e5).length,activitiesToday:r.filter(u=>new Date(u.created_at).toDateString()===new Date().toDateString()).length},P=u=>u==="super_admin"?"bg-accent text-accent-foreground":u==="admin"?"bg-primary text-primary-foreground":u==="case_manager"?"bg-secondary text-secondary-foreground":"bg-muted text-muted-foreground",v=u=>{switch(u){case"auth":return e.jsx($e,{className:"h-3.5 w-3.5"});case"report":return e.jsx(_e,{className:"h-3.5 w-3.5"});case"download":return e.jsx(lt,{className:"h-3.5 w-3.5"});case"upload":return e.jsx(ke,{className:"h-3.5 w-3.5"});case"edit":return e.jsx(ts,{className:"h-3.5 w-3.5"});case"record_access":return e.jsx(st,{className:"h-3.5 w-3.5"});default:return e.jsx(Ue,{className:"h-3.5 w-3.5"})}};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4 flex-wrap",children:[e.jsxs("div",{children:[e.jsxs("h1",{className:"font-heading text-3xl font-bold text-foreground flex items-center gap-2",children:[e.jsx(Oe,{className:"h-7 w-7 text-accent"})," System Control Center"]}),e.jsx("p",{className:"text-muted-foreground font-body mt-1 max-w-2xl",children:"Real-time visibility into logins, user activity, record access, and system audit trail across the ABTC platform."})]}),e.jsxs("div",{className:"flex gap-2 flex-wrap",children:[e.jsxs(M,{onClick:j,variant:"outline",className:"font-heading gap-2",children:[e.jsx(ot,{className:"h-4 w-4"})," Refresh"]}),e.jsxs(M,{onClick:()=>kt(`activity-log-${q(new Date,"yyyy-MM-dd")}.csv`,["When","User","Role","Category","Action","Entity Type","Entity Name","IP"],R.map(u=>[u.created_at,u.user_email,u.user_role,u.action_category,u.action_type,u.entity_type,u.entity_name,u.ip_address])),variant:"outline",className:"font-heading gap-2",children:[e.jsx(at,{className:"h-4 w-4"})," CSV"]}),e.jsxs(M,{onClick:()=>Ct("Full Activity Audit Log",`activity-log-${q(new Date,"yyyy-MM-dd")}.pdf`,["When","User","Role","Category","Action","Entity","IP"],R.map(u=>[q(new Date(u.created_at),"MMM d HH:mm"),u.user_email||"",u.user_role||"",u.action_category,u.action_type,u.entity_name||u.entity_id||"",u.ip_address||""])),variant:"outline",className:"font-heading gap-2",children:[e.jsx(_e,{className:"h-4 w-4"})," PDF"]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 lg:grid-cols-5 gap-4",children:[e.jsx(L,{className:"border-l-4 border-l-accent shadow-sm",children:e.jsxs(W,{className:"p-5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-xs uppercase tracking-wide text-muted-foreground font-heading",children:"Active Now"}),e.jsx(Zt,{className:"h-4 w-4 text-accent"})]}),e.jsx("p",{className:"text-3xl font-heading font-bold mt-2 text-foreground",children:N.activeNow}),e.jsx("p",{className:"text-xs text-muted-foreground font-body mt-1",children:"last 15 minutes"})]})}),e.jsx(L,{className:"border-l-4 border-l-primary shadow-sm",children:e.jsxs(W,{className:"p-5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-xs uppercase tracking-wide text-muted-foreground font-heading",children:"Logins Today"}),e.jsx($e,{className:"h-4 w-4 text-primary"})]}),e.jsx("p",{className:"text-3xl font-heading font-bold mt-2 text-foreground",children:N.loginsToday}),e.jsx("p",{className:"text-xs text-muted-foreground font-body mt-1",children:"all users"})]})}),e.jsx(L,{className:"border-l-4 border-l-destructive shadow-sm",children:e.jsxs(W,{className:"p-5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-xs uppercase tracking-wide text-muted-foreground font-heading",children:"Failed Logins"}),e.jsx(Oe,{className:"h-4 w-4 text-destructive"})]}),e.jsx("p",{className:"text-3xl font-heading font-bold mt-2 text-destructive",children:N.failedLogins24h}),e.jsx("p",{className:"text-xs text-muted-foreground font-body mt-1",children:"last 24 hours"})]})}),e.jsx(L,{className:"border-l-4 border-l-accent shadow-sm",children:e.jsxs(W,{className:"p-5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-xs uppercase tracking-wide text-muted-foreground font-heading",children:"Activities Today"}),e.jsx(Ue,{className:"h-4 w-4 text-accent"})]}),e.jsx("p",{className:"text-3xl font-heading font-bold mt-2 text-foreground",children:N.activitiesToday}),e.jsx("p",{className:"text-xs text-muted-foreground font-body mt-1",children:"all categories"})]})}),e.jsx(L,{className:`border-l-4 shadow-sm ${x>0?"border-l-destructive bg-destructive/5":"border-l-accent"}`,children:e.jsxs(W,{className:"p-5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-xs uppercase tracking-wide text-muted-foreground font-heading",children:"Open Alerts"}),e.jsx(ge,{className:`h-4 w-4 ${x>0?"text-destructive":"text-accent"}`})]}),e.jsx("p",{className:`text-3xl font-heading font-bold mt-2 ${x>0?"text-destructive":"text-foreground"}`,children:x}),e.jsx("p",{className:"text-xs text-muted-foreground font-body mt-1",children:"unacknowledged"})]})})]}),e.jsxs("div",{className:"flex gap-3 flex-wrap",children:[e.jsxs("div",{className:"flex-1 min-w-[260px] relative",children:[e.jsx(es,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(Q,{placeholder:"Search by email, IP, action, entity…",value:o,onChange:u=>l(u.target.value),className:"pl-9 font-body"})]}),e.jsxs(Ce,{value:h,onValueChange:f,children:[e.jsx(De,{className:"w-52 font-body",children:e.jsx(Pe,{})}),e.jsxs(Re,{children:[e.jsx(O,{value:"all",children:"All categories"}),e.jsx(O,{value:"auth",children:"Authentication"}),e.jsx(O,{value:"report",children:"Reports"}),e.jsx(O,{value:"document",children:"Documents"}),e.jsx(O,{value:"record_access",children:"Record / Search Access"}),e.jsx(O,{value:"edit",children:"Edits"}),e.jsx(O,{value:"upload",children:"Uploads"}),e.jsx(O,{value:"download",children:"Downloads"}),e.jsx(O,{value:"system",children:"System"})]})]})]}),e.jsxs(nt,{defaultValue:"activity",children:[e.jsx("div",{className:"w-full overflow-x-auto -mx-1 px-1 pb-1",children:e.jsxs(rt,{className:"inline-flex h-auto w-max min-w-full flex-nowrap gap-1",children:[e.jsx(E,{value:"activity",className:"font-heading",children:"Activity Log"}),e.jsx(E,{value:"logins",className:"font-heading",children:"Login Logs"}),e.jsx(E,{value:"active",className:"font-heading",children:"Active Users"}),e.jsxs(E,{value:"sessions",className:"font-heading gap-1.5",children:[e.jsx(cs,{className:"h-3.5 w-3.5"}),"Sessions"]}),e.jsxs(E,{value:"alerts",className:"font-heading gap-1.5",children:[e.jsx(ge,{className:"h-3.5 w-3.5"}),"Alerts ",x>0&&e.jsx("span",{className:"ml-1 px-1.5 rounded-full bg-destructive text-destructive-foreground text-[10px]",children:x})]}),e.jsxs(E,{value:"geo",className:"font-heading gap-1.5",children:[e.jsx(tt,{className:"h-3.5 w-3.5"}),"Geo Map"]}),e.jsx(E,{value:"users",className:"font-heading",children:"Users Drill-down"}),e.jsxs(E,{value:"blocklist",className:"font-heading gap-1.5",children:[e.jsx(ct,{className:"h-3.5 w-3.5"}),"IP Blocklist"]}),e.jsxs(E,{value:"resets",className:"font-heading gap-1.5",children:[e.jsx(dt,{className:"h-3.5 w-3.5"}),"Password Resets"]}),e.jsxs(E,{value:"backup",className:"font-heading gap-1.5",children:[e.jsx(os,{className:"h-3.5 w-3.5"}),"Backup & Restore"]}),e.jsx(E,{value:"rbac",className:"font-heading",children:"RBAC Audit"})]})}),e.jsx(I,{value:"activity",children:e.jsxs(L,{children:[e.jsx($,{children:e.jsxs(U,{className:"font-heading",children:["User Activity (",R.length,")"]})}),e.jsx(W,{className:"p-0",children:e.jsx("div",{className:"max-h-[600px] overflow-auto",children:e.jsxs(X,{children:[e.jsx(V,{children:e.jsxs(C,{children:[e.jsx(y,{children:"When"}),e.jsx(y,{children:"User"}),e.jsx(y,{children:"Role"}),e.jsx(y,{children:"Category"}),e.jsx(y,{children:"Action"}),e.jsx(y,{children:"Entity"}),e.jsx(y,{children:"IP"})]})}),e.jsx(G,{children:s?e.jsx(C,{children:e.jsx(g,{colSpan:7,className:"text-center py-8 text-muted-foreground",children:"Loading…"})}):R.length===0?e.jsx(C,{children:e.jsx(g,{colSpan:7,className:"text-center py-8 text-muted-foreground",children:"No activity recorded yet."})}):R.map(u=>e.jsxs(C,{children:[e.jsx(g,{className:"font-body text-xs whitespace-nowrap",children:ne(new Date(u.created_at),{addSuffix:!0})}),e.jsx(g,{className:"font-body text-sm",children:u.user_email||"—"}),e.jsx(g,{children:e.jsx(z,{className:P(u.user_role||"client"),variant:"secondary",children:u.user_role})}),e.jsx(g,{children:e.jsxs(z,{variant:"outline",className:"gap-1 font-body",children:[v(u.action_category)," ",u.action_category]})}),e.jsx(g,{className:"font-body text-sm",children:u.action_type}),e.jsxs(g,{className:"font-body text-sm",children:[u.entity_name||u.entity_id||"—",u.entity_type&&e.jsxs("span",{className:"text-xs text-muted-foreground ml-1",children:["(",u.entity_type,")"]})]}),e.jsx(g,{className:"font-mono text-xs",children:u.ip_address||"—"})]},u.id))})]})})})]})}),e.jsx(I,{value:"logins",children:e.jsxs(L,{children:[e.jsx($,{children:e.jsxs(U,{className:"font-heading",children:["Login History (",S.length,")"]})}),e.jsx(W,{className:"p-0",children:e.jsx("div",{className:"max-h-[600px] overflow-auto",children:e.jsxs(X,{children:[e.jsx(V,{children:e.jsxs(C,{children:[e.jsx(y,{children:"Date / Time"}),e.jsx(y,{children:"Email"}),e.jsx(y,{children:"Status"}),e.jsx(y,{children:"IP Address"}),e.jsx(y,{children:"Device"}),e.jsx(y,{children:"Failure Reason"})]})}),e.jsx(G,{children:s?e.jsx(C,{children:e.jsx(g,{colSpan:6,className:"text-center py-8 text-muted-foreground",children:"Loading…"})}):S.length===0?e.jsx(C,{children:e.jsx(g,{colSpan:6,className:"text-center py-8 text-muted-foreground",children:"No login attempts recorded yet."})}):S.map(u=>e.jsxs(C,{children:[e.jsx(g,{className:"font-body text-xs whitespace-nowrap",children:q(new Date(u.created_at),"MMM d, HH:mm:ss")}),e.jsx(g,{className:"font-body text-sm",children:u.email||"—"}),e.jsx(g,{children:u.success?e.jsx(z,{variant:"secondary",className:"bg-secondary text-secondary-foreground",children:"Success"}):e.jsx(z,{variant:"destructive",children:"Failed"})}),e.jsx(g,{className:"font-mono text-xs",children:u.ip_address||"—"}),e.jsx(g,{className:"font-body text-xs",children:u.device_info||"—"}),e.jsx(g,{className:"font-body text-xs text-muted-foreground",children:u.failure_reason||"—"})]},u.id))})]})})})]})}),e.jsx(I,{value:"active",children:e.jsxs(L,{children:[e.jsx($,{children:e.jsxs(U,{className:"font-heading",children:["Active Users — last 15 minutes (",c.length,")"]})}),e.jsx(W,{className:"p-0",children:e.jsxs(X,{children:[e.jsx(V,{children:e.jsxs(C,{children:[e.jsx(y,{children:"Name"}),e.jsx(y,{children:"Email"}),e.jsx(y,{children:"Role"}),e.jsx(y,{children:"Last Seen"})]})}),e.jsx(G,{children:c.length===0?e.jsx(C,{children:e.jsx(g,{colSpan:4,className:"text-center py-8 text-muted-foreground",children:"No active users right now."})}):c.map(u=>e.jsxs(C,{children:[e.jsx(g,{className:"font-body",children:u.full_name||"—"}),e.jsx(g,{className:"font-body text-sm",children:u.email}),e.jsx(g,{children:e.jsx(z,{className:P(u.role),variant:"secondary",children:u.role})}),e.jsx(g,{className:"font-body text-xs",children:u.last_seen_at?ne(new Date(u.last_seen_at),{addSuffix:!0}):"—"})]},u.id))})]})})]})}),e.jsx(I,{value:"sessions",children:e.jsx(Ns,{})}),e.jsx(I,{value:"alerts",children:e.jsx(bs,{})}),e.jsx(I,{value:"geo",children:e.jsx(ks,{})}),e.jsx(I,{value:"users",children:e.jsxs(L,{children:[e.jsx($,{children:e.jsx(U,{className:"font-heading",children:"User Activity Drill-down"})}),e.jsx(W,{className:"p-0",children:e.jsx(ua,{logs:r})})]})}),e.jsx(I,{value:"blocklist",children:e.jsx(Ss,{})}),e.jsx(I,{value:"resets",children:e.jsx(fa,{})}),e.jsx(I,{value:"backup",children:e.jsx(xa,{})}),e.jsx(I,{value:"rbac",children:e.jsxs(L,{children:[e.jsx($,{children:e.jsx(U,{className:"font-heading",children:"Role-Based Access Audit"})}),e.jsxs(W,{className:"space-y-3 font-body text-sm",children:[e.jsx("p",{className:"text-muted-foreground",children:"Summary of access boundaries currently enforced via RLS policies:"}),e.jsxs("ul",{className:"space-y-2 list-disc list-inside",children:[e.jsxs("li",{children:[e.jsx("strong",{children:"Super Admin:"})," Full access including system control center, analytics, all logs."]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Admin:"})," Full data access (cases, debtors, companies, reports). Cannot view audit logs."]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Case Manager:"})," Manage debt cases, CSR reports, recovery records. Cannot view all clients."]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Field Admin:"})," Insert/update debtors and companies for KYC fieldwork only."]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Client:"})," Sees only their own cases, reports, watchlists, and assigned debtors."]})]}),e.jsx("p",{className:"text-muted-foreground pt-2",children:"All sensitive tables have Row-Level Security enabled. Logs are write-only for normal users and read-only for super admins — they cannot be modified or deleted."})]})]})})]})]})};export{wa as default};
