1import{r as c,au as O,I as N,j as e,B as T,K as $e,ak as ks,aB as $s,L as ls,E as is,C as de,a8 as me,aa as ue,a9 as xe,b as he,aR as Ts,a0 as pe,bq as Fs,W as F,$ as P,aI as Ds,bp as Es,aw as As,ap as Ls,ar as Ms,as as qs,at as Bs,a as Is,aJ as Os,a2 as Rs,J as fe}from"./index-CwRyDTkN.js";import{S as X,a as G,b as Y,c as Q,d as y}from"./select-DwBWUARa.js";import{T as Ks,a as Us,b as Ie,c as Ce,d as zs,e as I}from"./table-DPuTNloT.js";import{M as os}from"./minus-CmqZi-Qk.js";import{C as Vs}from"./chevron-left-BQuCIw7X.js";import{R as Hs,k as Ws,X as Js,Y as Ps,T as Xs,p as Gs,q as Ys,B as cs,o as Qs}from"./generateCategoricalChart-BiZ2bqEV.js";import{B as Zs}from"./BarChart-m9dXRF_b.js";import{B as et}from"./ban-DPgO7VN_.js";import{A as st}from"./arrow-up-CJIe-MWM.js";import{A as tt}from"./arrow-down-qrIYdecD.js";import{A as at}from"./arrow-up-down-CI5qNw42.js";import"./chevron-up-iCYBxH0c.js";import"./isEqual-CVrr51JR.js";import"./string-Dx7P_S-w.js";import"./index-D95X0B-W.js";import"./linear-DGECpK-G.js";import"./init-Dmth1JHB.js";import"./defaultLocale-C4B-KCzX.js";import"./time-ByyzYylg.js";import"./band-CquvqAHh.js";import"./ordinal-DILIJJjt.js";const ke=["24h","7d","30d"],rt={app_return:1,content_started:2,content_completed:3,active_day:1,dm_reply:2,retained_7d:3,retained_30d:4,subscription_started:8,email_opened:0,email_clicked:0},ge=i=>{if(!i||typeof i!="object")return 0;const C=i;let p=0;for(const[k,b]of Object.entries(rt)){if(b===0)continue;const E=C[k];p+=(typeof E=="number"?E:E===!0?1:0)*b}return p},nt={treatment:"bg-primary/10 text-primary border-primary/30",holdout:"bg-muted text-muted-foreground border-border"},S=(i,C=2)=>Number.isFinite(i)?i.toFixed(C):"â",ds=i=>{if(!i)return"";if(typeof i=="string")return i;if(typeof i=="object"){const C=i,p=["subject","preheader","headline","body","cta","cta_label","message"].map(k=>typeof C[k]=="string"?C[k]:"").filter(Boolean);return p.length?p.join(" â "):JSON.stringify(i)}return String(i)},D=i=>i,ms=D("id, user_id, trigger_type, selected_action, eligible_actions, suppression_reasons, rationale, cohort, dry_run, policy_version, selection_probability, evaluated_at, created_by, context_snapshot, strategy_id, variant_id, experiment_arm_id, manual_suppressed, manual_suppressed_at, manual_suppressed_by, manual_suppression_note, strategy:nicole_strategies(code, version, intent), variant:nicole_variants(label, channel, content_template, status), user_state:nicole_user_states(state_key, lifecycle_stage, engagement_tier, receptivity), windows:nicole_outcome_windows(window_label, status, metrics, measured_at, closes_at)"),lt=D("cohort, user_state:nicole_user_states(state_key, lifecycle_stage), windows:nicole_outcome_windows(window_label, status, metrics, measured_at)"),it=[25,50,100],ot=({decision:i,onChanged:C})=>{const[p,k]=c.useState(""),[b,E]=c.useState(!1),x=i.manual_suppressed===!0,{data:je,refetch:n}=O({queryKey:["nicole-audit-suppression-events",i.id],queryFn:async()=>{const{data:d,error:$}=await N.from("nicole_decision_suppression_events").select(D("id, action, note, actor_id, created_at")).eq("decision_id",i.id).order("created_at",{ascending:!1}).limit(20).returns();if($)throw $;return d??[]}}),Z=async d=>{var $;E(!0);try{const{data:j}=await N.auth.getUser(),_e=(($=j.user)==null?void 0:$.id)??null,A=new Date().toISOString(),ve={manual_suppressed:d,manual_suppressed_at:d?A:null,manual_suppressed_by:d?_e:null,manual_suppression_note:d&&p.trim()||null},{error:L}=await N.from("nicole_decision_opportunities").update(ve).eq("id",i.id);if(L)throw L;const{error:ee}=await N.from("nicole_decision_suppression_events").insert({decision_id:i.id,action:d?"suppress":"unsuppress",note:p.trim()||null,actor_id:_e});if(ee)throw ee;C(ve),k(""),await n(),fe.success(d?"Intervention suppressed":"Intervention unsuppressed")}catch(j){console.error("[nicole-audit] suppression change failed",j),fe.error(j instanceof Error?j.message:"Could not record the change")}finally{E(!1)}};return e.jsxs("div",{className:"rounded-md border border-border p-4 space-y-3",children:[e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium",children:"Admin suppression"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:x?`Suppressed${i.manual_suppressed_at?` on ${new Date(i.manual_suppressed_at).toLocaleStr
1ing()}`:""}`:"This recommendation is currently allowed."})]}),e.jsx(F,{variant:"outline",className:x?"border-destructive/40 text-destructive":"border-primary/30 text-primary",children:x?"Suppressed":"Active"})]}),x&&i.manual_suppression_note&&e.jsx("p",{className:"text-sm bg-muted rounded-md p-2",children:i.manual_suppression_note}),e.jsx(Rs,{value:p,onChange:d=>k(d.target.value),placeholder:x?"Why are you unsuppressing this? (optional)":"Why are you suppressing this? (optional)",rows:2}),e.jsxs(T,{size:"sm",variant:x?"outline":"destructive",disabled:b,onClick:()=>Z(!x),children:[b?e.jsx($e,{className:"h-4 w-4 mr-2 animate-spin"}):e.jsx(et,{className:"h-4 w-4 mr-2"}),x?"Unsuppress intervention":"Suppress intervention"]}),(je??[]).length>0&&e.jsxs("div",{className:"space-y-1 pt-1",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Change history"}),(je??[]).map(d=>e.jsxs("div",{className:"text-xs flex flex-wrap gap-2",children:[e.jsx("span",{className:"font-medium",children:d.action}),e.jsx("span",{className:"text-muted-foreground",children:new Date(d.created_at).toLocaleString()}),d.note&&e.jsxs("span",{className:"text-muted-foreground",children:["â ",d.note]})]},d.id))]})]})},Et=()=>{const[i,C]=c.useState(""),[p,k]=c.useState(""),[b,E]=c.useState("all"),[x,je]=c.useState("7d"),[n,Z]=c.useState(null),[d,$]=c.useState(0),[j,_e]=c.useState(25),[A,ve]=c.useState("evaluated_at"),[L,ee]=c.useState(!1),[Te,Oe]=c.useState(!1),[Re,us]=c.useState(!1),[K,Ke]=c.useState("all"),[U,Ue]=c.useState("all"),[z,ze]=c.useState("all"),[V,Ve]=c.useState(""),[H,He]=c.useState(""),[Fe,We]=c.useState(""),[De,Je]=c.useState("");c.useEffect(()=>{const s=setTimeout(()=>k(i.trim()),350);return()=>clearTimeout(s)},[i]),c.useEffect(()=>{$(0)},[p,b,j,A,L,K,U,z,V,H]);const{data:Ee}=O({queryKey:["nicole-audit-search-users",p],enabled:p.length>=2,queryFn:async()=>{const s=`%${p}%`,{data:t,error:l}=await N.from("profiles").select(D("id")).or(`email.ilike.${s},display_name.ilike.${s},first_name.ilike.${s},last_name.ilike.${s}`).limit(200).returns();if(l)throw l;return(t??[]).map(o=>o.id)}}),{data:xs=[]}=O({queryKey:["nicole-audit-trigger-options"],staleTime:5*60*1e3,queryFn:async()=>{const{data:s,error:t}=await N.from("nicole_decision_opportunities").select(D("trigger_type")).order("evaluated_at",{ascending:!1}).limit(1e3).returns();if(t)throw t;return Array.from(new Set((s??[]).map(l=>l.trigger_type).filter(Boolean))).sort()}}),{data:hs=[]}=O({queryKey:["nicole-audit-strategy-options"],staleTime:5*60*1e3,queryFn:async()=>{const{data:s,error:t}=await N.from("nicole_strategies").select(D("id, code, version")).order("code",{ascending:!0}).limit(500).returns();if(t)throw t;return s??[]}}),{data:ps=[]}=O({queryKey:["nicole-audit-variant-options"],staleTime:5*60*1e3,queryFn:async()=>{const{data:s,error:t}=await N.from("nicole_variants").select(D("id, label, channel")).order("created_at",{ascending:!1}).limit(500).returns();if(t)throw t;return s??[]}}),Pe=s=>{let t=s;if(b!=="all"&&(t=t.eq("cohort",b)),K!=="all"&&(t=t.eq("trigger_type",K)),U!=="all"&&(t=t.eq("strategy_id",U)),z!=="all"&&(t=t.eq("variant_id",z)),V&&(t=t.gte("evaluated_at",new Date(`${V}T00:00:00`).toISOString())),H&&(t=t.lte("evaluated_at",new Date(`${H}T23:59:59`).toISOString())),p.length>=2){const l=`%${p}%`,o=[`trigger_type.ilike.${l}`,`selected_action.ilike.${l}`,`policy_version.ilike.${l}`],m=Ee??[];m.length&&o.push(`user_id.in.(${m.join(",")})`),t=t.or(o.join(","))}return t},{data:W,isLoading:gs,isFetching:se,refetch:Xe}=O({queryKey:["nicole-audit-decisions",d,j,A,L,b,p,(Ee??[]).length,K,U,z,V,H],enabled:p.length<2||Ee!==void 0,placeholderData:s=>s,queryFn:async()=>{const s=d*j,t=Pe(N.from("nicole_decision_opportunities").select(ms,{count:"exact"})),{data:l,error:o,count:m}=await t.order(A,{ascending:L}).order("id",{ascending:!1}).range(s,s+j-1).returns();if(o)throw o;return{rows:l??[],total:m??0}}}),Ae=(W==null?void 0:W.rows)??[],te=(W==null?void 0:W.total)??0,Le=Math.max(1,Math.ceil(te/j)),Me=c.useMemo(()=>Array.from(new Set(Ae.map(s=>s.user_id))),[Ae]),{data:Ge=[]}
1=O({queryKey:["nicole-audit-profiles",Me.join(",")],enabled:Me.length>0,queryFn:async()=>{const{data:s,error:t}=await N.from("profiles").select(D("id, email, first_name, last_name, display_name")).in("id",Me).returns();if(t)throw t;return s??[]}}),fs=c.useMemo(()=>{const s=new Map;return Ge.forEach(t=>s.set(t.id,t)),s},[Ge]),Ye=s=>{const t=fs.get(s.user_id);return t?t.display_name||[t.first_name,t.last_name].filter(Boolean).join(" ")||t.email:s.user_id.slice(0,8)},{data:Qe=[]}=O({queryKey:["nicole-audit-baselines"],staleTime:5*60*1e3,queryFn:async()=>{const{data:s,error:t}=await N.from("nicole_decision_opportunities").select(lt).eq("cohort","holdout").order("evaluated_at",{ascending:!1}).limit(2e3).returns();if(t)throw t;return s??[]}}),ae=c.useMemo(()=>{var t,l,o;const s=new Map;for(const m of ke){const r=new Map;for(const f of Qe){const _=(t=f.windows)==null?void 0:t.find(q=>q.window_label===m);if(!_||_.status!=="measured"&&!_.measured_at)continue;const M=((l=f.user_state)==null?void 0:l.lifecycle_stage)??((o=f.user_state)==null?void 0:o.state_key)??"unknown",R=r.get(M)??[];R.push(ge(_.metrics)),r.set(M,R)}const h=new Map;let w=0,g=0;r.forEach((f,_)=>{const M=f.reduce((R,q)=>R+q,0);w+=M,g+=f.length,h.set(_,{mean:M/f.length,n:f.length})}),s.set(m,{byStage:h,overall:g?{mean:w/g,n:g}:null})}return s},[Qe]),J=c.useMemo(()=>ae.get(x)??{byStage:new Map,overall:null},[ae,x]),ye=c.useMemo(()=>{var t,l;if(!n)return[];const s=((t=n.user_state)==null?void 0:t.lifecycle_stage)??((l=n.user_state)==null?void 0:l.state_key)??"unknown";return ke.map(o=>{var f;const m=(f=n.windows)==null?void 0:f.find(_=>_.window_label===o),h=!!m&&(m.status==="measured"||!!m.measured_at)?ge(m.metrics):null,w=ae.get(o),g=w?w.byStage.get(s)??w.overall:null;return{window:o,status:m?m.status:"not opened",decision:h,control:g?g.mean:null,controlN:g?g.n:0,delta:h!==null&&g?h-g.mean:null}})},[n,ae]),Ne=s=>{var w,g,f;const t=(w=s.windows)==null?void 0:w.find(_=>_.window_label===x),l=!!t&&(t.status==="measured"||!!t.measured_at),o=l?ge(t.metrics):null,m=((g=s.user_state)==null?void 0:g.lifecycle_stage)??((f=s.user_state)==null?void 0:f.state_key)??"unknown",r=J.byStage.get(m)??J.overall,h=o!==null&&r?o-r.mean:null;return{window:t,measured:l,score:o,control:r,delta:h,stage:m}},re=Fe.trim()===""?null:Number(Fe),ne=De.trim()===""?null:Number(De),Ze=re!==null&&Number.isFinite(re)||ne!==null&&Number.isFinite(ne),es=s=>
1{if(!Ze)return!0;const{delta:t}=Ne(s);return!(t===null||re!==null&&Number.isFinite(re)&&t<re||ne!==null&&Number.isFinite(ne)&&t>ne)},be=Ae.filter(es),js=be.filter(s=>Ne(s).measured).length,_s=be.filter(s=>(s.suppression_reasons??[]).length>0).length,ss=[b!=="all",K!=="all",U!=="all",z!=="all",!!V,!!H,Ze].filter(Boolean).length,vs=()=>{Ke("all"),Ue("all"),ze("all"),Ve(""),He(""),We(""),Je("")},ys=async()=>{var s,t,l,o,m,r,h,w,g,f,_,M,R;Oe(!0);try{const le=[];for(let a=0;a<2e4;a+=1e3){const u=Pe(N.from("nicole_decision_opportunities").select(ms)),{data:oe,error:B}=await u.order(A,{ascending:L}).order("id",{ascending:!1}).range(a,a+1e3-1).returns();if(B)throw B;const ce=oe??[];if(le.push(...ce.filter(es)),ce.length<1e3)break}if(!le.length){fe.info("No rows match the current filters.");return}const ts=Array.from(new Set(le.map(a=>a.user_id))),as=new Map;for(let a=0;a<ts.length;a+=200){const u=ts.slice(a,a+200),{data:oe}=await N.from("profiles").select(D("id, email, first_name, last_name, display_name")).in("id",u).returns();(oe??[]).forEach(B=>as.set(B.id,B))}const bs=["decision_id","evaluated_at","family","email","user_id","trigger_type","selected_action","eligible_actions","cohort","dry_run","policy_version","selection_probability","strategy_code","strategy_version","strategy_intent","variant_label","variant_channel","variant_status","recommended_content","lifecycle_stage","engagement_tier","receptivity","rationale","suppression_reasons","manual_suppressed","manual_suppressed_at","manual_suppression_note","created_by",...ke.flatMap(a=>[`${a}_status`,`${a}_score`,`${a}_control_mean`,`${a}_delta`,`${a}_measured_at`,`${a}_metrics`])],ws=a=>{if(a==null)return"";const u=typeof a=="string"?a:typeof a=="number"||typeof a=="boolean"?String(a):JSON.stringify(a);return/[",\n\r]/.test(u)?`"${u.replace(/"/g,'""')}"`:u},rs=[bs.join(",")];for(const a of le){const u=as.get(a.user_id),oe=((s=a.user_state)==null?void 0:s.lifecycle_stage)??((t=a.user_state)==null?void 0:t.state_key)??"unknown",B=[a.id,a.evaluated_at,(u==null?void 0:u.display_name)||[u==null?void 0:u.first_name,u==null?void 0:u.last_name].filter(Boolean).join(" ")||(u==null?void 0:u.email)||"",(u==null?void 0:u.email)??"",a.user_id,a.trigger_type,a.selected_action,(a.eligible_actions??[]).join(" | "),a.cohort,a.dry_run,a.policy_version,a.selection_probability,((l=a.strategy)==null?void 0:l.code)??"",((o=a.strategy)==null?void 0:o.version)??"",((m=a.strategy)==null?void 0:m.intent)??"",((r=a.variant)==null?void 0:r.label)??"",((h=a.variant)==null?void 0:h.channel)??"",((w=a.variant)==null?void 0:w.status)??"",ds((g=a.variant)==null?void 0:g.content_template),((f=a.user_state)==null?void 0:f.lifecycle_stage)??"",((_=a.user_state)==null?void 0:_.engagement_tier)??"",((M=a.user_state)==null?void 0:M.receptivity)??"",(a.rationale??[]).join(" | "),(a.suppression_reasons??[]).join(" | "),a.manual_suppressed===!0,a.manual_suppressed_at??"",a.manual_suppression_note??"",a.created_by];for(const ce of ke){const v=(R=a.windows)==null?void 0:R.find(Cs=>Cs.window_label===ce),qe=!!v&&(v.status==="measured"||!!v.measured_at)?ge(v.metrics):null,Be=ae.get(ce),Se=Be?Be.byStage.get(oe)??Be.overall:null;B.push((v==null?void 0:v.status)??"not opened",qe??"",Se?Se.mean.toFixed(4):"",qe!==null&&Se?(qe-Se.mean).toFixed(4):"",(v==null?void 0:v.measured_at)??"",(v==null?void 0:v.metrics)??"")}rs.push(B.map(ws).join(","))}const Ss=new Blob([`\uFEFF${rs.join(`\r 2`)}`],{type:"text/csv;charset=utf-8;"}),ns=URL.createObjectURL(Ss),ie=document.createElement("a");ie.href=ns,ie.download=`nicole-intervention-audit-${new Date().toISOString().slice(0,10)}.csv`,document.body.appendChild(ie),ie.click(),ie.remove(),URL.revokeObjectURL(ns),fe.success(`Exported ${le.length.toLocaleString()} decisions`)}catch(q){console.error("[nicole-audit] csv export failed",q),fe.error(q instanceof Error?q.message:"Export failed")}finally{Oe(!1)}},Ns=s=>{if(A===s){ee(t=>!t);return}ve(s),ee(s!=="evaluated_at")},we=({column:s,label:t,className:l})=>e.jsx(Ce,{className:l,children:e.jsxs("button",{type:"button",onClick:()=>Ns(s),className:"inline-flex items-center gap-1 font-medium hover:text-foreground transition-colors min-h-11",children:[t,A===s?L?e.jsx(st,{className:"h-3.5 w-3.5"}):e.jsx(tt,{className:"h-3.5 w-3.5"}):e.jsx(at,{className:"h-3.5 w-3.5 opacity-40"})]})});return e.jsxs("div",{className:"space-y-8",children:[e.jsxs("div",{className:"flex flex-col gap-4 md:flex-row md:items-end md:justify-between",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx("h1",{className:"text-2xl font-semibold tracking-tight",children:"Intervention Audit Log"}),e.jsx("p",{className:"text-sm text-muted-foreground max-w-2xl",children:"Every decision Nicole made â who it was for, what triggered it, the exact strategy and variant chosen, the content recommended, and how the outcome compares with the control cohort."})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-3",children:[e.jsxs(X,{value:x,onValueChange:je,children:[e.jsx(G,{className:"w-[160px] min-h-11",children:e.jsx(Y,{})}),e.jsxs(Q,{children:[e.jsx(y,{value:"24h",children:"24h outcome"}),e.jsx(y,{value:"7d",children:"7-day outcome"}),e.jsx(y,{value:"30d",children:"30-day outcome"})]})]}),e.jsxs(T,{variant:"outline",size:"sm",className:"min-h-11",onClick:()=>Xe(),disabled:se,children:[se?e.jsx($e,{className:"h-4 w-4 animate-spin"}):e.jsx(ks,{className:"h-4 w-4"}),e.jsx("span",{className:"ml-2",children:"Refresh"})]}),e.jsxs(T,{variant:"outline",size:"sm",className:"min-h-11",onClick:ys,disabled:Te,children:[Te?e.jsx($e,{className:"h-4 w-4 animate-spin"}):e.jsx($s,{className:"h-4 w-4"}),e.jsx("span",{className:"ml-2",children:Te?"Exportingâ¦":"Export CSV"})]}),e.jsx(T,{variant:"outline",size:"sm",className:"min-h-11",asChild:!0,children:e.jsxs(ls,{to:"/admin/nicole-lift",children:["Lift dashboard ",e.jsx(is,{className:"h-3.5 w-3.5 ml-2"})]})})]})]}),e.jsxs("div",{className:"grid gap-6 md:grid-cols-4",children:[e.jsxs(de,{children:[e.jsxs(me,{className:"pb-2",children:[e.jsx(ue,{children:"Decisions logged"}
2),e.jsx(xe,{className:"text-2xl",children:te.toLocaleString()})]}),e.jsx(he,{className:"text-sm text-muted-foreground",children:"matching current filters"})]}),e.jsxs(de,{children:[e.jsxs(me,{className:"pb-2",children:[e.jsxs(ue,{children:["With measured ",x," outcome"]}),e.jsx(xe,{className:"text-2xl",children:js})]}),e.jsx(he,{className:"text-sm text-muted-foreground",children:"on this page"})]}),e.jsxs(de,{children:[e.jsxs(me,{className:"pb-2",children:[e.jsx(ue,{children:"Control cohort baseline"}),e.jsx(xe,{className:"text-2xl",children:J.overall?S(J.overall.mean):"â"})]}),e.jsx(he,{className:"text-sm text-muted-foreground",children:J.overall?`${J.overall.n} holdout windows`:"no holdout data yet"})]}),e.jsxs(de,{children:[e.jsxs(me,{className:"pb-2",children:[e.jsx(ue,{children:"Suppressed decisions"}),e.jsx(xe,{className:"text-2xl",children:_s})]}),e.jsx(he,{className:"text-sm text-muted-foreground",children:"quiet by design (this page)"})]})]}),e.jsxs(de,{children:[e.jsxs(me,{className:"gap-4",children:[e.jsxs("div",{className:"flex flex-col gap-4 md:flex-row md:items-center md:justify-between",children:[e.jsxs("div",{children:[e.jsx(xe,{className:"text-lg",children:"Decision trail"}),e.jsx(ue,{children:"Click a row for the full rationale and recommended content."})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-3",children:[e.jsxs("div",{className:"relative",children:[e.jsx(Ts,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(pe,{value:i,onChange:s=>C(s.target.value),placeholder:"Search family, trigger, strategyâ¦",className:"pl-9 w-[260px] min-h-11"})]}),e.jsxs(X,{value:b,onValueChange:E,children:[e.jsx(G,{className:"w-[150px] min-h-11",children:e.jsx(Y,{})}),e.jsxs(Q,{children:[e.jsx(y,{value:"all",children:"All cohorts"}),e.jsx(y,{value:"treatment",children:"Treatment"}),e.jsx(y,{value:"holdout",children:"Holdout (control)"})]})]}),e.jsxs(T,{variant:Re?"secondary":"outline",size:"sm",className:"min-h-11",onClick:()=>us(s=>!s),children:[e.jsx(Fs,{className:"h-4 w-4"}),e.jsx("span",{className:"ml-2",children:"Filters"}),ss>0&&e.jsx(F,{variant:"secondary",className:"ml-2",children:ss})]})]})]}),Re&&e.jsxs("div",{className:"rounded-lg border bg-muted/30 p-4 space-y-4",children:[e.jsxs("div",{className:"grid gap-4 md:grid-cols-3",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(P,{className:"text-xs uppercase tracking-wide text-muted-foreground",children:"Trigger"}),e.jsxs(X,{value:K,onValueChange:Ke,children:[e.jsx(G,{className:"min-h-11",children:e.jsx(Y,{placeholder:"All triggers"})}),e.jsxs(Q,{children:[e.jsx(y,{value:"all",children:"All triggers"}),xs.map(s=>e.jsx(y,{value:s,children:s},s))]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(P,{className:"text-xs uppercase tracking-wide text-muted-foreground",children:"Strategy"}),e.jsxs(X,{value:U,onValueChange:Ue,children:[e.jsx(G,{className:"min-h-11",children:e.jsx(Y,{placeholder:"All strategies"})}),e.jsxs(Q,{children:[e.jsx(y,{value:"all",children:"All strategies"}),hs.map(s=>e.jsxs(y,{value:s.id,children:[s.code," v",s.version]},s.id))]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(P,{className:"text-xs uppercase tracking-wide text-muted-foreground",children:"Variant"}),e.jsxs(X,{value:z,onValueChange:ze,children:[e.jsx(G,{className:"min-h-11",children:e.jsx(Y,{placeholder:"All variants"})}),e.jsxs(Q,{children:[e.jsx(y,{value:"all",children:"All variants"}),ps.map(s=>e.jsxs(y,{value:s.id,children:[s.label||s.id.slice(0,8)," · ",s.channel]},s.id))]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(P,{htmlFor:"audit-date-from",className:"text-xs uppercase tracking-wide text-muted-foreground",children:"Evaluated from"}),e.jsx(pe,{id:"audit-date-from",type:"date",value:V,onChange:s=>Ve(s.target.value),className:"min-h-11"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(P,{htmlFor:"audit-date-to",className:"text-xs uppercase tracking-wide text-muted-foreground",children:"Evaluated to"}),e.jsx(pe,{id:"audit-date-to",type:"date",value:H,onChange:s=>He(s.target.value),className:"min-h-11"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs(P,{className:"text-xs uppercase tracking-wide text-muted-foreground",children:[x," delta range"]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(pe,{type:"number",step:"0.01",inputMode:"decimal",value:Fe,onCh
2ange:s=>We(s.target.value),placeholder:"min",className:"min-h-11","aria-label":`Minimum ${x} delta`}),e.jsx("span",{className:"text-muted-foreground",children:"to"}),e.jsx(pe,{type:"number",step:"0.01",inputMode:"decimal",value:De,onChange:s=>Je(s.target.value),placeholder:"max",className:"min-h-11","aria-label":`Maximum ${x} delta`})]})]})]}),e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-3",children:[e.jsxs("p",{className:"text-xs text-muted-foreground max-w-xl",children:["Trigger, strategy, variant and date filters run in the database. Delta thresholds compare each measured ",x," outcome with its holdout baseline, so they refine the rows on this page and drop decisions with no measured outcome yet."]}),e.jsx(T,{variant:"ghost",size:"sm",className:"min-h-11",onClick:vs,children:"Clear filters"})]})]})]}),e.jsxs(he,{children:[gs?e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground py-8",children:[e.jsx($e,{className:"h-4 w-4 animate-spin"})," Loading decisionsâ¦"]}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(Ks,{children:[e.jsx(Us,{children:e.jsxs(Ie,{children:[e.jsx(we,{column:"user_id",label:"Family"}),e.jsx(we,{column:"trigger_type",label:"Trigger"}),e.jsx(we,{column:"selected_action",label:"Strategy"}),e.jsx(Ce,{children:"Variant"}),e.jsx(Ce,{className:"min-w-[240px]",children:"Recommended content"}),e.jsx(we,{column:"evaluated_at",label:"Timestamp"}),e.jsxs(Ce,{className:"text-right",children:["vs control (",x,")"]})]})}),e.jsx(zs,{children:be.length===0?e.jsx(Ie,{children:e.jsx(I,{colSpan:7,className:"text-center text-muted-foreground py-10",children:"No decisions match these filters yet."})}):be.map(s=>{var l,o;const t=Ne(s);return e.jsxs(Ie,{className:"cursor-pointer",onClick:()=>Z(s),children:[e.jsxs(I,{children:[e.jsx("div",{className:"font-medium",children:Ye(s)}),e.jsxs("div",{className:"flex items-center gap-2 mt-1",children:[e.jsx(F,{variant:"outline",className:nt[s.cohort]??"border-border text-muted-foreground",children:s.cohort==="holdout"?"control":s.cohort}),s.dry_run&&e.jsx(F,{variant:"outline",className:"border-border text-muted-foreground",children:"dry run"})]})]}),e.jsxs(I,{children:[e.jsx("div",{children:s.trigger_type}),e.jsx("div",{className:"text-xs text-muted-foreground mt-1",children:t.stage})]}),e.jsx(I,{children:s.strategy?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"font-medium",children:[s.strategy.code," v",s.strategy.version]}),e.jsx("div",{className:"text-xs text-muted-foreground mt-1",children:s.strategy.intent})]}):e.jsx("span",{className:"text-muted-foreground",children:s.selected_action})}),e.jsx(I,{children:s.variant?e.jsxs(e.Fragment,{children:[e.jsx("div",{children:s.variant.label??((l=s.variant_id)==null?void 0:l.slice(0,8))}),e.jsx("div",{className:"text-xs text-muted-foreground mt-1",children:s.variant.channel})]}):e.jsx("span",{className:"text-muted-foreground",children:"â"})}),e.jsx(I,{className:"max-w-[320px]",children:e.jsx("p",{className:"text-sm text-muted-foreground line-clamp-2",children:ds((o=s.variant)==null?void 0:o.content_template)||"No content (suppressed or control)"})}),e.jsx(I,{className:"whitespace-nowrap text-sm text-muted-foreground",children:new Date(s.evaluated_at).toLocaleString()}),e.jsx(I,{className:"text-right",children:t.measured?t.delta===null?e.jsxs(F,{variant:"outline",className:"border-border text-muted-foreground",children:[e.jsx(os,{className:"h-3 w-3 mr-1"})," no control"]}):e.jsxs(F,{variant:"outline",className:t.delta>1e-4?"border-primary/30 bg-primary/10 text-primary":t.delta<-1e-4?"border-destructive/30 bg-destructive/10 text-destructive":"border-border text-muted-foreground",children:[t.delta>1e-4?e.jsx(Ds,{className:"h-3 w-3 mr-1"}):t.delta<-1e-4?e.jsx(Es,{className:"h-3 w-3 mr-1"}):e.jsx(os,{className:"h-3 w-3 mr-1"}),t.delta>=0?"+":"",S(t.delta)]}):e.jsx(F,{variant:"outline",className:"border-border text-muted-foreground",children:"pending"})})]},s.id)})})]})}),e.jsxs("div",{className:"flex flex-col gap-4 pt-6 sm:flex-row sm:items-center sm:justify-between",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:[te===0?"No decisions":`Showing ${d*j+1}â${Math.min((d+1)*j,te)}
2 of ${te.toLocaleString()}`,se&&e.jsx("span",{className:"ml-2 opacity-70",children:"updatingâ¦"})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-3",children:[e.jsxs(X,{value:String(j),onValueChange:s=>_e(Number(s)),children:[e.jsx(G,{className:"w-[130px] min-h-11",children:e.jsx(Y,{})}),e.jsx(Q,{children:it.map(s=>e.jsxs(y,{value:String(s),children:[s," per page"]},s))})]}),e.jsxs(T,{variant:"outline",size:"sm",className:"min-h-11",onClick:()=>$(s=>Math.max(0,s-1)),disabled:d===0||se,children:[e.jsx(Vs,{className:"h-4 w-4"}),e.jsx("span",{className:"ml-1",children:"Previous"})]}),e.jsxs("span",{className:"text-sm text-muted-foreground",children:["Page ",d+1," of ",Le]}),e.jsxs(T,{variant:"outline",size:"sm",className:"min-h-11",onClick:()=>$(s=>Math.min(Le-1,s+1)),disabled:d+1>=Le||se,children:[e.jsx("span",{className:"mr-1",children:"Next"}),e.jsx(As,{className:"h-4 w-4"})]})]})]})]})]}),e.jsx(Ls,{open:!!n,onOpenChange:s=>!s&&Z(null),children:e.jsx(Ms,{className:"max-w-2xl max-h-[85vh] overflow-y-auto",children:n&&(()=>{var t,l,o,m;const s=Ne(n);return e.jsxs(e.Fragment,{children:[e.jsxs(qs,{children:[e.jsxs(Bs,{className:"flex items-center gap-2",children:[e.jsx(Is,{className:"h-5 w-5 text-primary"}),Ye(n)]}),e.jsxs(Os,{children:[n.trigger_type," · ",new Date(n.evaluated_at).toLocaleString()," · policy ",n.policy_version]})]}),e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"grid gap-4 sm:grid-cols-2 text-sm",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-muted-foreground",children:"Cohort"}),e.jsx("p",{className:"font-medium",children:n.cohort==="holdout"?"control (holdout)":n.cohort})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-muted-foreground",children:"Selected action"}),e.jsx("p",{className:"font-medium",children:n.selected_action})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-muted-foreground",children:"Strategy"}),e.jsx("p",{className:"font-medium",children:n.strategy?`${n.strategy.code} v${n.strategy.version}`:"â"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-muted-foreground",children:"Variant"}),e.jsx("p",{className:"font-medium",children:n.variant?`${n.variant.label??((t=n.variant_id)==null?void 0:t.slice(0,8))} (${n.variant.channel})`:"â"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-muted-foreground",children:"Selection probability"}),e.jsxs("p",{className:"font-medium",children:[S(Number(n.selection_probability)*100,1),"%"]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-muted-foreground",children:"User state"}),e.jsxs("p",{className:"font-medium",children:[s.stage,(l=n.user_state)!=null&&l.engagement_tier?` · ${n.user_state.engagement_tier}`:"",(o=n.user_state)!=null&&o.receptivity?` · ${n.user_state.receptivity}`:""]})]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-2",children:"Recommended content"}),e.jsx("pre",{className:"text-xs bg-muted rounded-md p-3 overflow-x-auto whitespace-pre-wrap",children:(m=n.variant)!=null&&m.content_template?JSON.stringify(n.variant.content_template,null,2):"No content sent for this decision."})]}),(n.rationale??[]).length>0&&e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-2",children:"Rationale"}),e.jsx("ul",{className:"list-disc pl-5 text-sm space-y-1",children:n.rationale.map((r,h)=>e.jsx("li",{children:r},h))})]}),(n.suppression_reasons??[]).length>0&&e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-2",children:"Suppression reasons"}),e.jsx("div",{className:"flex flex-wrap gap-2",children:n.suppression_reasons.map(r=>e.jsx(F,{variant:"outline",className:"border-border text-muted-foreground",children:r},r))})]}),e.jsx(ot,{decision:n,onChanged:r=>{Z(h=>h&&{...h,...r}),Xe()}}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-2",children:"Outcome vs control cohort"}),e.jsxs("div",{className:"grid gap-4 sm:grid-cols-3 text-sm",children:[e.jsxs("div",{className:"rounded-md border border-border p-3",children:[e.jsxs("p",{className:"text-muted-foreground",children:["This decision (",x,")"]}),e.jsx("p",{className:"text-lg font-semibold",children:s.score!==null?S(s.score):"pending"})]}),e.jsxs("div",{className:"rounded-md border border-border p-3",children:[e.jsxs("p",{className:"text-muted-foreground",children:["Control mean (",s.stage,")"]}),e.jsx("p",{className:"text-lg font-semibold",children:s.control?S(s.control.mean):"â"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:s.control?`${s.control.n} windows`:"no holdout data"})]}),e.jsxs("div",{className:"rounded-md border border-border p-3",children:[e.jsx("p",{className:"text-muted-foreground",children:"Difference"}),e.jsx("p",{className:"text-lg font-semibold",children:s.delta===null?"â":`${s.delta>=0?"+":""}${S(s.delta)}`})]})]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-2",children:"Delta vs control across all windows"}),ye.every(r=>r.decision===null&&r.control===null)?e.jsx("p",{className:"text-sm text-muted-foreground",children:"No measured windows yet for this decision or its control cohort."}):e.jsxs("div",{className:"space-y-4",children:[e.jsx("div",{className:"h-56 w-full",children:e.jsx(Hs,{width:"100%",height:"100%",children:e.jsxs(Zs,{data:ye,margin:{top:8,right:8,left:0,bottom:0},children:[e.jsx(Ws,{strokeDasharray:"3 3",stroke:"hsl(var(--border))",vertical:!1}
2),e.jsx(Js,{dataKey:"window",stroke:"hsl(var(--muted-foreground))",fontSize:12}),e.jsx(Ps,{stroke:"hsl(var(--muted-foreground))",fontSize:12}),e.jsx(Xs,{contentStyle:{background:"hsl(var(--popover))",border:"1px solid hsl(var(--border))",borderRadius:8,color:"hsl(var(--popover-foreground))",fontSize:12},formatter:(r,h)=>[r===null?"pending":S(r),h]}),e.jsx(Gs,{wrapperStyle:{fontSize:12}}),e.jsx(Ys,{y:0,stroke:"hsl(var(--border))"}),e.jsx(cs,{dataKey:"control",name:"Control cohort",fill:"hsl(var(--muted-foreground))",radius:[4,4,0,0]}),e.jsx(cs,{dataKey:"decision",name:"This decision",radius:[4,4,0,0],children:ye.map(r=>e.jsx(Qs,{fill:r.delta!==null&&r.delta<-1e-4?"hsl(var(--destructive))":"hsl(var(--primary))"},r.window))})]})})}),e.jsx("div",{className:"grid gap-2 sm:grid-cols-3",children:ye.map(r=>e.jsxs("div",{className:"rounded-md border border-border p-3 text-sm",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"font-medium",children:r.window}),e.jsx(F,{variant:"outline",className:r.delta===null?"border-border text-muted-foreground":r.delta>1e-4?"border-emerald-500/40 text-emerald-600":r.delta<-1e-4?"border-destructive/40 text-destructive":"border-border text-muted-foreground",children:r.delta===null?r.status:`${r.delta>=0?"+":""}${S(r.delta)}`})]}),e.jsxs("p",{className:"text-xs text-muted-foreground mt-2",children:["decision ",r.decision===null?"pending":S(r.decision)," · control"," ",r.control===null?"â":S(r.control),r.controlN?` (${r.controlN})`:""]})]},r.window))})]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-2",children:"Outcome windows"}),e.jsx("div",{className:"space-y-2",children:(n.windows??[]).length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:"No windows opened."}):n.windows.map(r=>e.jsxs("div",{className:"flex items-center justify-between rounded-md border border-border px-3 py-2 text-sm",children:[e.jsx("span",{className:"font-medium",children:r.window_label}),e.jsxs("span",{className:"text-muted-foreground",children:[r.status,r.measured_at?` · score ${S(ge(r.metrics))}`:` · closes ${new Date(r.closes_at).toLocaleDateString()}`]})]},r.window_label))})]}),e.jsx(T,{variant:"outline",className:"min-h-11",asChild:!0,children:e.jsxs(ls,{to:`/admin/users/${n.user_id}`,children:["Open family timeline ",e.jsx(is,{className:"h-3.5 w-3.5 ml-2"})]})})]})]})})()})})]})};export{Et 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.