1import{b as A,u as S,j as e}from"./query-_koPiPsI.js";import{k as H,r as j,L as x}from"./react-vendor-h1rQ_Bae.js";import{ai as O,B as k,L as D,C as m,n as u,o as b,p as g,q as y,k as p,P as E,bh as I,a6 as L,V as P,s as v,g as F,ao as T}from"./index-Dq2zLArw.js";import{H as G}from"./HowToUseExplainer-BI-BNIDX.js";import{F as K}from"./FixRequestDialog-DEMwH9vL.js";import{C as M}from"./circle-play-JCqM3Pnw.js";import{C as N}from"./chevron-right-N8QrdNsP.js";import"./ui-radix-CKwvnS8F.js";import"./supabase-DL6nu9mS.js";import"./charts-Xu79y1bN.js";import"./motion-nVmI7L5T.js";import"./info-DI8_flyN.js";import"./select-S0HS5dUY.js";import"./chevron-up-Dbq7Df3-.js";const h=t=>t==null?{label:"â",tone:"secondary"}:t>=80?{label:"Strong",tone:"default"}:t>=60?{label:"Needs work",tone:"secondary"}:{label:"Critical",tone:"destructive"};function ne(){const{id:t}=H(),i=A(),[l,c]=j.useState(!1),[f,d]=j.useState(!1),[w,$]=j.useState(null),{data:C}=S({queryKey:["brand",t],enabled:!!t,queryFn:async()=>{const{data:r}=await v.from("brand_properties").select("id, domain, display_name").eq("id",t).maybeSingle();return r}}),{data:s}=S({queryKey:["brand_health_latest",t],enabled:!!t,refetchInterval:5e3,queryFn:async()=>{const{data:r}=await v.from("brand_health_runs").select("*").eq("brand_id",t).order("created_at",{ascending:!1}).limit(1).maybeSingle();return r}}),q=async()=>{if(t){c(!0);try{const{data:r,error:a}=await v.functions.invoke("run-brand-health",{body:{brand_id:t}});if(a)throw a;F.success("Brand Health run started â this takes 60â90 seconds."),i.invalidateQueries({queryKey:["brand_health_latest",t]})}catch(r){F.error(r.message??"Failed to start")}finally{c(!1)}}},o=s?.status==="running";return e.jsxs("div",{className:"max-w-4xl mx-auto p-4 md:p-6 space-y-6",children:[e.jsx("div",{className:"flex items-center gap-2 text-sm",children:e.jsxs(x,{to:"/brands",className:"inline-flex items-center gap-1 text-muted-foreground hover:text-foreground",children:[e.jsx(O,{className:"w-3.5 h-3.5"})," Back to Brands"]})}),e.jsxs("header",{className:"flex items-start justify-between gap-4 flex-wrap",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold",children:"Brand Health"}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:[C?.display_name??"â¦"," · ",e.jsx("span",{className:"font-mono",children:C?.domain??""})]})]}),e.jsx(k,{onClick:q,disabled:l||o,children:l||o?e.jsxs(e.Fragment,{children:[e.jsx(D,{className:"mr-2 h-4 w-4 animate-spin"}),"Runningâ¦"]}):e.jsxs(e.Fragment,{children:[e.jsx(M,{className:"mr-2 h-4 w-4"}),"Run Brand Health"]})})]}),e.jsx(G,{id:"brand-health",title:"Brand Health",what:"Runs Entity Reconciler, Adversarial Matrix and RAG Sandbox for this brand and rolls the results into one score.",when:"Once a month per brand, or right after you ship JSON-LD / structural fixes and want to prove lift.",read:"80+ Strong · 60â79 Needs work · under 60 Critical. Fix the lowest of the three sub-scores first.",next:e.jsx(e.Fragment,{children:"Ship the Top 3 fixes below, wait 2â3 days, re-run to see the delta."})}),!s&&e.jsx(m,{children:e.jsxs(u,{className:"py-10 text-center text-sm text-muted-foreground",children:["No runs yet â click ",e.jsx("strong",{children:"Run Brand Health"})," to score this brand."]})}),s&&(()=>{const r=[s.identity_score,s.durability_score,s.retrieval_score].filter(n=>typeof n=="number").length,a=s.status==="partial"||r<3;return e.jsxs(e.Fragment,{children:[e.jsxs(m,{children:[e.jsx(b,{className:"pb-2",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs(g,{className:"text-3xl flex items-center gap-2",children:[s.overall_score??"â",e.jsx("span",{className:"text-lg text-muted-foreground",children:"/100"}),!a&&e.jsx(B,{value:s.deltas?.overall})]}),e.jsx(y,{children:a?`Incomplete â based on ${r} of 3 checks`:`Overall â ${h(s.overall_score).label}`})]}),e.jsx(p,{variant:s.status==="error"?"destructive":a?"outline":h(s.overall_score).tone,children:o?"Runningâ¦":s.status==="error"?"Error":a?"Incomplete":h(s.overall_score).label})]})}),e.jsxs(u,{className:"space-y-4",children:[e.jsx(_,{icon:e.jsx(E,{className:"w-4 h-4"}),label:"Identity",score:s.identity_score,delta:s.deltas?.identity,help:"Entity Reconciler"}),e.jsx(_,{icon:e.jsx(I,{className:"w-4 h-4"}),label:"Durability",score:s.durability_score,delta:s.deltas?.durability,help:"Adversarial Matrix"}),e.jsx(_,{icon:e.jsx(L,{className:"w-4 h-4"}),label:"Retrieval",score:s.retrieval_score,delta:s.deltas?.retrieval,help:"RAG Sandbox"}),a&&e.jsxs("div",{className:"text-xs text-muted-foreground",children:["Only ",r," of 3 checks finished, so this
1score is not a full picture and can read higher than reality. Re-run the missing check before acting on it."]}),s.error&&e.jsxs("div",{className:"text-xs text-destructive",children:["Error: ",s.error]})]})]}),e.jsxs(m,{children:[e.jsxs(b,{children:[e.jsx(g,{className:"text-lg",children:"Top fixes"}),e.jsx(y,{children:"Ship these first â they move Identity, Durability or Retrieval the most."})]}),e.jsxs(u,{className:"space-y-2",children:[(s.top_fixes??[]).length===0&&e.jsx("p",{className:"text-sm text-muted-foreground",children:o?"Fixes will appear as engines completeâ¦":"No high-impact fixes found â you're in good shape."}),(s.top_fixes??[]).map((n,R)=>e.jsxs("div",{className:"flex gap-3 p-3 border rounded-md",children:[e.jsx("div",{className:"text-xs font-mono text-muted-foreground pt-0.5",children:R+1}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("div",{className:"font-medium text-sm",children:n.title}),e.jsx("div",{className:"text-xs text-muted-foreground mt-0.5",children:n.detail}),e.jsxs("div",{className:"mt-2 flex flex-wrap items-center gap-2",children:[e.jsx(p,{variant:"outline",className:"text-[10px] uppercase",children:n.source}),e.jsxs(k,{size:"sm",variant:"secondary",className:"h-7 text-xs",onClick:()=>{$({title:n.title,detail:n.detail}),d(!0)},children:[e.jsx(P,{className:"w-3 h-3 mr-1"})," Send to team"]})]})]})]},R))]})]}),e.jsxs(m,{children:[e.jsxs(b,{children:[e.jsx(g,{className:"text-base",children:"Drill in"}),e.jsx(y,{children:"Open the underlying engine run for this brand."})]}),e.jsxs(u,{className:"grid gap-2 sm:grid-cols-3 text-sm",children:[e.jsxs(x,{to:`/enterprise/entity-reconciler?brand_id=${t}${s.entity_run_id?`&run_id=${s.entity_run_id}`:""}`,className:"inline-flex items-center justify-between p-3 border rounded-md hover:bg-muted/40",children:["Entity Reconciler ",e.jsx(N,{className:"w-4 h-4"})]}),e.jsxs(x,{to:`/enterprise/adversarial-matrix?brand_id=${t}${s.matrix_run_id?`&run_id=${s.matrix_run_id}`:""}`,className:"inline-flex items-center justify-between p-3 border rounded-md hover:bg-muted/40",children:["Adversarial Matrix ",e.jsx(N,{className:"w-4 h-4"})]}),e.jsxs(x,{to:`/enterprise/rag-sandbox?brand_id=${t}${s.rag_run_id?`&run_id=${s.rag_run_id}`:""}`,className:"inline-flex items-center justify-between p-3 border rounded-md hover:bg-muted/40",children:["RAG Sandbox ",e.jsx(N,{className:"w-4 h-4"})]})]})]})]})})(),e.jsx(K,{open:f,onOpenChange:d,requestType:"fix",brandId:t,sourceRunId:s?.id??null,defaultTitle:w?.title??"",defaultDetail:w?.detail??""})]})}function _({icon:t,label:i,score:l,delta:c,help:f}){const d=h(l);return e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs("div",{className:"flex items-center justify-between text-sm",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[t,e.jsx("span",{className:"font-medium",children:i}),e.jsxs("span",{className:"text-xs text-muted-foreground",children:["· ",f]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(B,{value:c}),e.jsx(p,{variant:d.tone,className:"text-[10px]",children:d.label}),e.jsxs("span",{className:"font-mono text-sm",children:[l??"â","/100"]})]})]}),e.jsx(T,{value:l??0,className:"h-2"})]})}function B({value:t}){if(t==null||t===0)return null;const i=t>0;return e.jsxs(p,{variant:"outline",className:`text-[10px] ${i?"text-emerald-600 border-emerald-500/40 bg-emerald-500/10":"text-destructive border-destructive/40 bg-destructive/10"}`,children:[i?"+":"",t," since last run"]})}export{ne 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.