1import{j as e}from"./vendor-motion-BYSGJZRG.js";import{r as t,u as U}from"./vendor-react-BAWrOFBJ.js";import{h as L,B as W,R as J,p as I,C as T,f as A,b as X,d as Y,L as V,l as Z,s as F,u as ee}from"./index-BD6qdh5D.js";import{S as se}from"./SuperAdminNavigation-Ci82jotc.js";import{M as ae}from"./ModuleNavigation-DN-rwBNC.js";import{I as le}from"./input-a2GrFMNb.js";import{S as f,a as b,b as v,c as y,d as l}from"./select-CMrhmtbm.js";import{T as te,a as ne,b as M,c as d,d as re,e as o}from"./table-CPaI6DUo.js";import{M as ie}from"./mail-C42xyr8K.js";import{C as ce}from"./circle-check-Bh6pSYQV.js";import{B as oe}from"./ban-BcNrVZlR.js";import{f as de}from"./format-PM7ZyHu2.js";import"./vendor-pdf-Cq2FS4Cb.js";import"./vendor-radix-Scc5zhsa.js";import"./vendor-charts-DWQnO0ie.js";import"./layout-grid-BfmIWPXU.js";import"./package-s8p4O8za.js";import"./car-kzUQib6h.js";import"./calculator-C4KMSRTO.js";import"./menu-pIja9pUX.js";import"./receipt-1JHC1KMd.js";import"./chevron-up-CL_jrATs.js";const O=[{label:"Last 24 hours",value:"24h",days:1},{label:"Last 7 days",value:"7d",days:7},{label:"Last 30 days",value:"30d",days:30},{label:"Last 3 months",value:"3mo",days:90},{label:"Last 6 months",value:"6mo",days:180},{label:"Last 12 months",value:"12mo",days:365},{label:"All time",value:"all",days:0}],me={sent:{label:"Sent",className:"bg-emerald-100 text-emerald-800 hover:bg-emerald-100"},queued:{label:"Queued",className:"bg-blue-100 text-blue-800 hover:bg-blue-100"},pending:{label:"Pending",className:"bg-slate-100 text-slate-700 hover:bg-slate-100"},failed:{label:"Failed",className:"bg-red-100 text-red-800 hover:bg-red-100"},dlq:{label:"Failed (DLQ)",className:"bg-red-100 text-red-800 hover:bg-red-100"},bounced:{label:"Bounced",className:"bg-orange-100 text-orange-800 hover:bg-orange-100"},complained:{label:"Spam",className:"bg-purple-100 text-purple-800 hover:bg-purple-100"},suppressed:{label:"Suppressed",className:"bg-yellow-100 text-yellow-800 hover:bg-yellow-100"}};function ue({status:n}){const i=me[n]??{label:n,className:"bg-slate-100 text-slate-700"};return e.jsx(Z,{className:i.className,variant:"secondary",children:i.label})}function xe(){const[n,i]=t.useState("30d"),[r,x]=t.useState("all"),[w,k]=t.useState("all"),[m,H]=t.useState("all"),[S,D]=t.useState("all"),[h,Q]=t.useState(""),C=t.useMemo(()=>{const s=O.find(a=>a.value===n);return!s||s.days===0?null:new Date(Date.now()-s.days*86400*1e3).toISOString()},[n]),{data:q=[],isLoading:B,refetch:K,isFetching:E}=L({queryKey:["email-audit-log",C],queryFn:async()=>{let s=F.from("email_audit_log").select("*").order("created_at",{ascending:!1}).limit(5e3);C&&(s=s.gte("created_at",C));const{data:a,error:c}=await s;if(c)throw c;return a??[]}}),{data:_=[]}=L({queryKey:["email-audit-companies"],queryFn:async()=>{const{data:s}=await F.from("companies").select("id, name").order("name");return s??[]}}),{data:p=[]}=L({queryKey:["email-audit-locations"],queryFn:async()=>{const{data:s}=await F.from("locations").select("id, name, company_id").order("name");return s??[]}}),P=t.useMemo(()=>Object.fromEntries(_.map(s=>[s.id,s.name])),[_]),G=t.useMemo(()=>Object.fromEntries(p.map(s=>[s.id,s.name])),[p]),u=t.useMemo(()=>{const s=new Map,a=[];for(const c of q){if(!c.message_id){a.push(c);continue}const R=s.get(c.message_id);(!R||new Date(c.created_at)>new Date(R.created_at))&&s.set(c.message_id,c)}return[...s.values(),...a]},[q]),$=t.useMemo(()=>Array.from(new Set(u.map(s=>s.template_name))).sort(),[u]),j=t.useMemo(()=>u.filter(s=>r==="all"||s.template_name===r).filter(s=>w==="all"||s.status===w).filter(s=>m==="all"||s.company_id===m).filter(s=>S==="all"||s.location_id===S).filter(s=>!h||s.recipient_email.toLowerCase().includes(h.toLowerCase())||(s.subject??"").toLowerCase().includes(h.toLowerCase())).sort((s,a)=>new Date(a.created_at).getTime()-new Date(s.created_at).getTime()),[u,r,w,m,S,h]),g=t.useMemo(()=>{const s={total:u.length,sent:0,failed:0,suppressed:0,bounced:0};for(const a of u)a.status==="sent"?s.sent++:a.status==="failed"||a.status==="dlq"?s.failed++:a.status==="suppressed"?s.suppressed++:(a.status==="bounced"||a.status==="complained")&&s.bounced++;return s},[u]),z=t.useMemo(()=>m==="all"?p:p.filter(s=>s.company_id===m),[p,m]);return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-2xl font-bold",children:"Email Delivery Logs"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Permanent history of every email sent â kept beyond the 30-day live log."})]}),e.jsxs(W,{variant:"outline",size:"sm",onClick:()=>K(),disabled:E,children:[e.jsx(J,{className:`h-4 w-4 mr-2 ${E?"animate-spin":""}`}),"Refresh"]})]}
1),e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-5 gap-3",children:[e.jsx(N,{icon:e.jsx(ie,{className:"h-4 w-4"}),label:"Total",value:g.total}),e.jsx(N,{icon:e.jsx(ce,{className:"h-4 w-4 text-emerald-600"}),label:"Sent",value:g.sent}),e.jsx(N,{icon:e.jsx(I,{className:"h-4 w-4 text-red-600"}),label:"Failed",value:g.failed}),e.jsx(N,{icon:e.jsx(I,{className:"h-4 w-4 text-orange-600"}),label:"Bounced/Spam",value:g.bounced}),e.jsx(N,{icon:e.jsx(oe,{className:"h-4 w-4 text-yellow-600"}),label:"Suppressed",value:g.suppressed})]}),e.jsx(T,{children:e.jsxs(A,{className:"pt-6 space-y-3",children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-3",children:[e.jsxs(f,{value:n,onValueChange:i,children:[e.jsx(b,{children:e.jsx(v,{})}),e.jsx(y,{children:O.map(s=>e.jsx(l,{value:s.value,children:s.label},s.value))})]}),e.jsxs(f,{value:m,onValueChange:s=>{H(s),D("all")},children:[e.jsx(b,{children:e.jsx(v,{placeholder:"All companies"})}),e.jsxs(y,{children:[e.jsx(l,{value:"all",children:"All companies"}),_.map(s=>e.jsx(l,{value:s.id,children:s.name},s.id))]})]}),e.jsxs(f,{value:S,onValueChange:D,children:[e.jsx(b,{children:e.jsx(v,{placeholder:"All locations"})}),e.jsxs(y,{children:[e.jsx(l,{value:"all",children:"All locations"}),z.map(s=>e.jsx(l,{value:s.id,children:s.name},s.id))]})]})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-3",children:[e.jsxs(f,{value:r,onValueChange:x,children:[e.jsx(b,{children:e.jsx(v,{placeholder:"All templates"})}),e.jsxs(y,{children:[e.jsx(l,{value:"all",children:"All email types"}),$.map(s=>e.jsx(l,{value:s,children:s},s))]})]}),e.jsxs(f,{value:w,onValueChange:k,children:[e.jsx(b,{children:e.jsx(v,{})}),e.jsxs(y,{children:[e.jsx(l,{value:"all",children:"All statuses"}),e.jsx(l,{value:"sent",children:"Sent"}),e.jsx(l,{value:"queued",children:"Queued"}),e.jsx(l,{value:"failed",children:"Failed"}),e.jsx(l,{value:"dlq",children:"Failed (DLQ)"}),e.jsx(l,{value:"bounced",children:"Bounced"}),e.jsx(l,{value:"complained",children:"Spam complaint"}),e.jsx(l,{value:"suppressed",children:"Suppressed"})]})]}),e.jsx(le,{placeholder:"Search recipient or subjectâ¦",value:h,onChange:s=>Q(s.target.value)})]})]})}),e.jsxs(T,{children:[e.jsx(X,{children:e.jsx(Y,{className:"text-base",children:B?"Loadingâ¦":`${j.length} emails`})}),e.jsx(A,{children:B?e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(V,{className:"h-6 w-6 animate-spin text-muted-foreground"})}):e.jsxs("div",{className:"overflow-x-auto",children:[e.jsxs(te,{children:[e.jsx(ne,{children:e.jsxs(M,{children:[e.jsx(d,{children:"When"}),e.jsx(d,{children:"Type"}),e.jsx(d,{children:"Company"}),e.jsx(d,{children:"Location"}),e.jsx(d,{children:"Recipient"}),e.jsx(d,{children:"Subject"}),e.jsx(d,{children:"Status"}),e.jsx(d,{children:"Details"})]})}),e.jsxs(re,{children:[j.slice(0,300).map(s=>{var a;return e.jsxs(M,{children:[e.jsx(o,{className:"text-xs whitespace-nowrap",children:de(new Date(s.created_at),"dd.MM.yyyy HH:mm")}),e.jsx(o,{className:"text-xs font-mono",children:s.template_name}),e.jsx(o,{className:"text-xs",children:s.company_id?P[s.company_id]??"â":"â"}),e.jsx(o,{className:"text-xs",children:s.location_id?G[s.location_id]??"â":"â"}),e.jsx(o,{className:"text-xs",children:s.recipient_email}),e.jsx(o,{className:"text-xs max-w-[220px] truncate",children:s.subject??((a=s.metadata)==null?void 0:a.subject)??"â"}),e.jsx(o,{children:e.jsx(ue,{status:s.status})}),e.jsx(o,{className:"text-xs text-muted-foreground max-w-[260px] truncate",children:s.error_message??"â"})]},s.id)}),j.length===0&&e.jsx(M,{children:e.jsx(o,{colSpan:8,className:"text-center text-muted-foreground py-8",children:"No emails match your filters."})})]})]}),j.length>300&&e.jsxs("p",{className:"text-xs text-muted-foreground mt-3 text-center",children:["Showing latest 300 of ",j.length,". Narrow filters to see more."]})]})})]})]})}function N({icon:n,label:i,value:r}){return e.jsx(T,{children:e.jsxs(A,{className:"pt-4 pb-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-xs text-muted-foreground mb-1",children:[n,i]}),e.jsx("div",{className:"text-2xl font-bold",children:r})]})})}function Re(){const n=U(),{user:i,isAdmin:r,loading:x}=ee();return t.useEffect(()=>{!x&&(!i||!r)&&n("/admin",{replace:!0})},[i,r,x,n]),x||!r?e.jsx("div",{className:"min-h-screen flex items-center justify-center bg-background",children:e.jsx(V,{className:"h-8 w-8 animate-spin text-primary"})}):e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx(se,{}),e.jsxs("div",{className:"max-w-7xl mx-auto p-6",children:[e.jsx(ae,{}),e.jsx(xe,{})]})]})}export{Re as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.