1import{r as a,j as e}from"./react-vendor-DHsc3z8R.js";import{a as Z,j as K,X,M as V}from"./index-zHdis8yY.js";import{S as b}from"./StatusPill-ByttfFpw.js";import"./dropdown-menu-D7htail8.js";import{B as U}from"./bot-fCcsK2Z9.js";import{R as A}from"./refresh-cw-Bj2pST-f.js";import{S as Y}from"./send-DqsKC_ri.js";import{S as ee}from"./search-N0Xs0rvp.js";import{Z as te}from"./zap-C_syKUsu.js";import{A as se}from"./activity-C5ez3Iro.js";import{M as ae}from"./message-circle-DrHWXNBX.js";import{T}from"./triangle-alert-DzmV-SIh.js";import{C}from"./circle-check-big-DleXm_l7.js";import"./query-vendor-BpSM1h0G.js";import"./ui-vendor-CSbCjZRx.js";import"./motion-vendor-BtpmimIC.js";import"./check-toxosa8q.js";import"./chevron-right-OP-RSzSu.js";import"./circle-DpFjhvB5.js";const H=100,re=[{value:"",label:"All channels"},{value:"email",label:"Email"},{value:"whatsapp",label:"WhatsApp"},{value:"push",label:"Push"}],ne=[{value:"",label:"All triggers"},{value:"re_engagement",label:"Re-engagement"},{value:"commitment_followup",label:"Commitment follow-up"},{value:"inactivity",label:"Inactivity"},{value:"onboarding",label:"Onboarding"},{value:"usage_warning",label:"Usage warning"}];function oe({channel:r,note:o}){const d=typeof o=="string"?o:"",i=d.startsWith("whatsapp_fallback:"),x=i?d.split(":")[1]:null;return e.jsxs("div",{className:"flex flex-col gap-1",children:[r==="whatsapp"?e.jsxs(b,{variant:"success",size:"sm",children:[e.jsx(ae,{className:"w-3 h-3"})," WhatsApp",d==="template"&&e.jsx("span",{className:"font-normal ml-0.5 opacity-80",children:"· template"})]}):r==="push"?e.jsx(b,{variant:"info",size:"sm",children:"Push"}):e.jsxs(b,{variant:"muted",size:"sm",children:[e.jsx(V,{className:"w-3 h-3"})," Email"]}),i&&e.jsxs(b,{variant:"warning",size:"sm",children:[e.jsx(T,{className:"w-3 h-3"}),x==="no_sid"?"WA fallback: no SID":"WA fallback: error"]})]})}function de({status:r}){return r==="delivered"?e.jsxs("span",{className:"inline-flex items-center gap-1 text-xs text-success",children:[e.jsx(C,{className:"w-3 h-3"})," Delivered"]}):r==="read"?e.jsxs("span",{className:"inline-flex items-center gap-1 text-xs text-info",children:[e.jsx(C,{className:"w-3 h-3"})," Read"]}):r==="sent"?e.jsxs("span",{className:"inline-flex items-center gap-1 text-xs text-muted-foreground",children:[e.jsx(C,{className:"w-3 h-3"})," Sent"]}):r==="undelivered"?e.jsxs("span",{className:"inline-flex items-center gap-1 text-xs text-warning-ink",children:[e.jsx(T,{className:"w-3 h-3"})," Undelivered"]}):r==="failed"?e.jsxs("span",{className:"inline-flex items-center gap-1 text-xs text-destructive",children:[e.jsx(T,{className:"w-3 h-3"})," Failed"]}):e.jsx("span",{className:"text-xs text-muted-foreground",children:r??"â"})}function Ce({token:r}){const{toast:o}=Z(),d={...K(),"Content-Type":"application/json"}
1,[i,x]=a.useState([]),[j,E]=a.useState(0),[P,R]=a.useState(!1),[n,N]=a.useState(1),[l,y]=a.useState(""),[m,F]=a.useState(""),[g,I]=a.useState(""),[p,L]=a.useState(""),[h,M]=a.useState(""),[f,J]=a.useState(""),[W,O]=a.useState(""),[v,_]=a.useState(!1),[w,G]=a.useState(null),[q,$]=a.useState(!1),S=a.useRef(void 0),c=async(t=n)=>{R(!0);try{const s=new URLSearchParams;s.set("page",String(t)),s.set("limit",String(H)),l&&s.set("search",l),m&&s.set("channel",m),g&&s.set("triggerType",g),p&&s.set("dateFrom",p),h&&s.set("dateTo",h);const B=await fetch(`/api/admin/nudge-log?${s.toString()}`,{headers:d});if(B.ok){const u=await B.json();Array.isArray(u)?(x(u),E(u.length)):(x(u.data??[]),E(u.total??0))}}catch{o({title:"Failed to load nudge log",variant:"destructive"})}finally{R(!1)}};a.useEffect(()=>{c(1),N(1)},[m,g,p,h]),a.useEffect(()=>(clearTimeout(S.current),S.current=setTimeout(()=>{c(1),N(1)},400),()=>clearTimeout(S.current)),[l]);const D=t=>{N(t),c(t)},Q=async()=>{$(!0);try{const t=await fetch("/api/admin/nudge/run",{method:"POST",headers:d}),s=await t.json();if(!t.ok)throw new Error(s.error??`HTTP ${t.status}`);o({title:s.message??"Nudge job started"}),c(n)}catch(t){o({title:"Failed to run nudge job",description:t.message,variant:"destructive"})}finally{$(!1)}},k=Math.max(1,Math.ceil(j/H)),z=async()=>{if(f.trim()){_(!0),O("");try{const t=await fetch("/api/admin/nudge/ai-query",{method:"POST",headers:d,body:JSON.stringify({query:f})}),s=await t.json();if(!t.ok)throw new Error(s.error||"AI query failed");O(s.answer),G({memberCount:s.memberCount,nudgeCount:s.nudgeCount})}catch(t){o({title:"AI query failed",description:t.message,variant:"destructive"})}finally{_(!1)}}};return e.jsxs("div",{className:"space-y-5",children:[e.jsxs("div",{className:"bg-secondary/20 border border-border/40 rounded-xl p-5 space-y-3",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(U,{className:"w-4 h-4 text-accent-gold"}),e.jsx("h3",{className:"font-semibold text-foreground",children:"AI Nudge & WhatsApp Insights"})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:'Ask anything about your nudge data. For example: "Who has had more than 5 nudges?", "Which members have WhatsApp linked but no nudges?", "Show me everyone who got a re-engagement nudge in May".'}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx("input",{"data-testid":"input-nudge-ai-query",value:f,onChange:t=>J(t.target.value),onKeyDown:t=>t.key==="Enter"&&!t.shiftKey&&z(),placeholder:"Type a question about your nudge or WhatsApp data...",className:"flex-1 bg-background border border-border/50 rounded-lg px-3 py-2 text-sm text-foreground placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring/50"}),e.jsxs("button",{"data-testid":"button-run-ai-query",onClick:z,disabled:v||!f.trim(),className:"flex items-center gap-1.5 px-4 py-2 bg-accent-gold hover:bg-accent-gold-light text-navy-deep text-sm font-semibold rounded-lg transition-colors disabled:opacity-50",children:[v?e.jsx(A,{className:"w-4 h-4 animate-spin"}):e.jsx(Y,{className:"w-4 h-4"}),v?"Thinking...":"Ask"]})]}),W&&e.jsxs("div",{className:"bg-background/60 border border-border/40 rounded-lg p-4",children:[e.jsxs("div",{className:"flex items-start justify-between gap-2 mb-2",children:[e.jsxs("span",{className:"text-xs font-medium text-accent-gold-ink flex items-center gap-1",children:[e.jsx(U,{className:"w-3.5 h-3.5"})," AI Answer"]}),w&&e.jsxs("span",{className:"text-xs text-muted-foreground shrink-0",children:[w.memberCount," members · ",w.nudgeCount?.toLocaleString()," nudges analysed"]})]}),e.jsx("p",{className:"text-sm text-foreground whitespace-pre-wrap leading-relaxed",children:W})]})]}),e.jsx("div",{className:"bg-secondary/20 border border-border/40 rounded-xl p-4",children:e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsxs("div",{className:"relative flex-1 min-w-[180px]",children:[e.jsx(ee,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-3.5 h-3.5 text-muted-foreground"}),e.jsx("input",{"data-testid":"input-nudge-search",value:l,onCh
1ange:t=>y(t.target.value),placeholder:"Search by name or email...",className:"w-full bg-background border border-border/50 rounded-lg pl-8 pr-3 py-1.5 text-sm text-foreground placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring/50"}),l&&e.jsx("button",{onClick:()=>y(""),className:"absolute right-2 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground",children:e.jsx(X,{className:"w-3.5 h-3.5"})})]}),e.jsx("select",{"data-testid":"select-channel-filter",value:m,onChange:t=>F(t.target.value),className:"bg-background border border-border/50 rounded-lg px-3 py-1.5 text-sm text-foreground focus:outline-none",children:re.map(t=>e.jsx("option",{value:t.value,children:t.label},t.value))}),e.jsx("select",{"data-testid":"select-trigger-filter",value:g,onChange:t=>I(t.target.value),className:"bg-background border border-border/50 rounded-lg px-3 py-1.5 text-sm text-foreground focus:outline-none",children:ne.map(t=>e.jsx("option",{value:t.value,children:t.label},t.value))}),e.jsx("input",{"data-testid":"input-date-from",type:"date",value:p,onChange:t=>L(t.target.value),className:"bg-background border border-border/50 rounded-lg px-3 py-1.5 text-sm text-foreground focus:outline-none"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"to"}),e.jsx("input",{"data-testid":"input-date-to",type:"date",value:h,onChange:t=>M(t.target.value),className:"bg-background border border-border/50 rounded-lg px-3 py-1.5 text-sm text-foreground focus:outline-none"}),e.jsx("button",{"data-testid":"button-clear-filters",onClick:()=>{y(""),F(""),I(""),L(""),M("")},className:"text-xs text-muted-foreground hover:text-foreground transition-colors px-2 py-1.5",children:"Clear"}),e.jsxs("button",{"data-testid":"button-run-nudge-job-now",onClick:Q,disabled:q,className:"flex items-center gap-1 text-xs font-medium px-2.5 py-1.5 rounded-lg bg-accent-gold text-navy-deep hover:bg-accent-gold-light transition-colors disabled:opacity-50",children:[e.jsx(te,{className:`w-3.5 h-3.5 ${q?"animate-pulse":""}`})," Run now"]}),e.jsx("button",{"data-testid":"button-refresh-nudge-log",onClick:()=>c(n),className:"flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground transition-colors px-2 py-1.5",children:e.jsx(A,{className:`w-3.5 h-3.5 ${P?"animate-spin":""}`})}),e.jsxs("span",{className:"text-xs text-muted-foreground ml-auto shrink-0",children:[j.toLocaleString()," entries"]})]})}),e.jsxs("div",{className:"bg-secondary/20 border border-border/40 rounded-xl overflow-hidden",children:[e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-border/30",children:[e.jsx("th",{className:"text-left py-3 px-4 text-xs text-muted-foreground font-medium",children:"Member"}),e.jsx("th",{className:"text-left py-3 px-3 text-xs text-muted-foreground font-medium",children:"Trigger"}),e.jsx("th",{className:"text-left py-3 px-3 text-xs text-muted-foreground font-medium",children:"Channel"}),e.jsx("th",{className:"text-left py-3 px-3 text-xs text-muted-foreground font-medium",children:"Status"}),e.jsx("th",{className:"text-left py-3 px-3 text-xs text-muted-foreground font-medium",children:"Sent"})]})}),e.jsx("tbody",{className:"divide-y divide-border/20",children:P?e.jsx("tr",{children:e.jsx("td",{colSpan:5,className:"text-center py-10 text-muted-foreground text-sm",children:e.jsx(A,{className:"w-5 h-5 animate-spin mx-auto mb-2"})})}):i.length===0?e.jsx("tr",{children:e.jsxs("td",{colSpan:5,className:"text-center py-10",children:[e.jsx(se,{className:"w-8 h-8 mx-auto mb-2 text-muted-foreground/30"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"No nudge deliveries match your filters"})]})}):i.map(t=>e.jsxs("tr",{"data-testid":`row-nudge-${t.id}`,className:"hover:bg-secondary/10",children:[e.jsxs("td",{className:"py-2.5 px-4",children:[e.jsx("div",{className:"text-sm font-medium text-foreground truncate max-w-[180px]",children:t.userName??`User #${t.userId}`}),t.userEmail&&e.jsx("div",{className:"text-xs text-muted-foreground truncate max-w-[180px]",children:t.userEmail}),t.userTier&&e.jsx("span",{className:`text-[10px] font-medium capitalize mt-0.5 inline-block ${t.userTier==="enterprise"?"text-purple-700":t.userTier==="professional"?"text-accent-gold":t.userTier==="starter"?"text-info":"text-muted-foreground"}`,children:t.userTier})]}),e.jsx("td",{className:"py-2.5 px-3",children:e.jsx("span",{className:"text-xs text-muted-foreground font-mono",children:t.triggerType})}),e.jsx("td",{className:"py-2.5 px-3",children:e.jsx(oe,{channel:t.channel,note:t.note})}),e.jsx("td",{className:"py-2.5 px-3",children:e.jsx(de,{status:t.status})}),e.jsx("td",{className:"py-2.5 px-3 text-xs text-muted-foreground whitespace-nowrap",children:t.sentAt?new Date(t.sentAt).toLocaleStr
1ing("en-US",{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"}):"â"})]},t.id))})]})}),k>1&&e.jsxs("div",{className:"flex items-center justify-between px-4 py-3 border-t border-border/30",children:[e.jsxs("span",{className:"text-xs text-muted-foreground",children:[j.toLocaleString()," total"]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("button",{onClick:()=>D(n-1),disabled:n<=1,"data-testid":"button-nudge-prev-page",className:"px-3 py-1 rounded text-xs border border-border/50 text-muted-foreground hover:text-foreground disabled:opacity-40 disabled:cursor-not-allowed transition-colors",children:"Previous"}),e.jsxs("span",{className:"text-xs text-muted-foreground",children:["Page ",n," of ",k]}),e.jsx("button",{onClick:()=>D(n+1),disabled:n>=k,"data-testid":"button-nudge-next-page",className:"px-3 py-1 rounded text-xs border border-border/50 text-muted-foreground hover:text-foreground disabled:opacity-40 disabled:cursor-not-allowed transition-colors",children:"Next"})]})]})]})]})}export{Ce as NudgeLogTab};
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.