1import{f as L,u as $,r as o,Q as F,m as O,j as e,L as U,F as w,K as P}from"./index-pJ_5jzzg.js";import{S as R}from"./ScoreHistoryChart-DnfFMt-b.js";import{A as B}from"./arrow-left-ByzZU6WN.js";import{A as f}from"./activity-BuHv8ser.js";import{T as y}from"./trending-up-SVn-oNCJ.js";import{C as _}from"./calendar-COtETNY9.js";const q=L("ShieldAlert",[["path",{d:"M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z",key:"oel41y"}],["path",{d:"M12 8v4",key:"1got3b"}],["path",{d:"M12 16h.01",key:"1drbdi"}]]);function H(i){return i>=70?"text-emerald-400":i>=40?"text-amber-400":"text-red-400"}function S({label:i,value:n,maxValue:t,color:l}){const h=t>0?Math.round(n/t*100):0,p=t>0?Math.max(n/t*100,4):4;return e.jsxs("div",{className:"flex items-center gap-4 mb-3",children:[e.jsx("span",{className:"text-sm text-slate-400 w-40 shrink-0 text-right",children:i}),e.jsxs("div",{className:"flex-1 h-8 bg-slate-700/50 rounded-lg overflow-hidden relative",children:[e.jsx("div",{className:`h-full rounded-lg transition-all duration-700 ${l}`,style:{width:`${p}%`}}),e.jsxs("span",{className:"absolute inset-0 flex items-center px-3 text-sm font-semibold text-white",children:[n.toLocaleString()," (",h,"%)"]})]})]})}function I({name:i,score:n,maxScore:t}){const l=t>0?Math.max(n/t*100,2):2;return e.jsxs("div",{className:"flex items-center gap-4 mb-2.5",children:[e.jsx("span",{className:"text-sm text-slate-400 w-36 shrink-0 truncate text-right",children:i}),e.jsx("div",{className:"flex-1 h-5 bg-slate-700/50 rounded-full overflow-hidden",children:e.jsx("div",{className:"h-full bg-accent-500 rounded-full transition-all duration-700",style:{width:`${l}%`}})}),e.jsx("span",{className:`text-sm font-bold w-10 text-right ${H(n)}`,children:n})]})}function X(){const{currentUser:i,getToken:n}=$(),[t,l]=o.useState(null),[h,p]=o.useState([]),[A,k]=o.useState(!0),[u,z]=o.useState(null),D=i?.role==="admin"||i?.role==="owner",g=o.useCallback(async()=>{try{const a=await n();if(!a)throw new Error("No authentication token");const[r,d]=await Promise.all([F(a).catch(()=>null),O(a).catch(()=>[])]);if(r)l(r);else if(d.length>0){const b=d.length,C=Math.round(d.reduce((s,c)=>s+(c.scores?.overall??0),0)/b),x={};for(const s of d)if(s.scores?.dimensions)for(const[c,v]of Object.entries(s.scores.dimensions))x[c]||(x[c]={sum:0,count:0,name:v.name}),x[c].sum+=v.score,x[c].count+=1;const N={};for(const[s,c]of Object.entries(x))N[c.name||s]=Math.round(c.sum/c.count);const E=d.slice(0,20).map(s=>({type:"scan_completed",description:`Scan completed for ${s.input?.businessName||s.input?.website||"Unknown"}`,date:s.generatedAt})),M=d.filter(s=>s.generatedAt).slice().sort((s,c)=>new Date(s.generatedAt).getTime()-new Date(c.generatedAt).getTime()).map(s=>({date:s.generatedAt.slice(0,10),score:s.scores?.overall??0}));l({totalScans:b,avgScore:C,activeMembers:1,activeSchedules:0,scoreTrend:M,dimensionAvgs:N,recentEvents:E})}else l({totalScans:0,avgScore:0,activeMembers:1,activeSchedules:0,scoreTrend:[],dimensionAvgs:{},recentEvents:[]});p(d)}catch(a){const r=a instanceof Error?a.message:"Failed to load analytics";z(r)}finally{k(!1)}},[n]);if(o.useEffect(()=>{g()},[g]),A)return e.jsx("div",{className:"flex items-center justify-center min-h-screen",children:e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"spinner mx-auto mb-4"}),e.jsx("p",{className:"text-slate-400",children:"Loading analytics..."})]})});if(!D)return e.jsx("div",{className:"max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12",children:e.jsxs("div",{className:"card text-center py-16",children:[e.jsx(q,{size:48,className:"text-red-400 mx-auto mb-4"}),e.jsx("h2",{className:"text-2xl font-bold text-white mb-3",children:"Access Denied"}),e.jsx("p",{className:"text-slate-400 mb-6",children:"Analytics are only available to organization admins and owners."}),e.jsxs(U,{to:"/dashboard",className:"btn-primary inline-flex items-center gap-2",children:[e.jsx(B,{size:18}),"Back to Dashboard"]})]})});const m={scanStarted:t?.totalScans??0,scanCompleted:h.length},j=Object.entries(t?.dimensionAvgs??{}).sort((a,r)=>r[1]-a[1]),T=j.length>0?100:0;return e.jsxs("div",{className:"max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12",children:[e.jsxs("div",{className:"flex items-center gap-3 mb-8",children:[e.jsx(w,{size:28,className:"text-accent-400"}),e.jsxs("div",{children:[e.jsx("h1",{className:"section-title mb-0",children:"Analytics"}),e.jsx("p",{className:"text-slate-400 text-sm",children:"Organization performance overview"})]})]}),u&&e.jsx("div",{className:"card border-red-500/50 bg-red-500/10 mb-6",children:e.jsx("p",{className:"text-red-300",children:u})}),t&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6 mb-10",children:[e.jsx("div",{className:"card border-accent-500/30 bg-gradient-to-br from-accent-500/10 to-transparent",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-slate-400 text-sm font-medium uppercase tracking-wide mb-1",children:"Total Scans"}),e.jsx("p",{className:"text-4xl font-bold text-white",children:t.totalScans.toLocaleStr
1ing()})]}),e.jsx("div",{className:"w-12 h-12 rounded-xl bg-accent-500/15 flex items-center justify-center",children:e.jsx(f,{size:24,className:"text-accent-400"})})]})}),e.jsx("div",{className:"card border-accent-500/30 bg-gradient-to-br from-accent-500/10 to-transparent",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-slate-400 text-sm font-medium uppercase tracking-wide mb-1",children:"Average Score"}),e.jsxs("p",{className:"text-4xl font-bold text-white",children:[t.avgScore,e.jsx("span",{className:"text-lg text-slate-500 font-normal",children:"/100"})]})]}),e.jsx("div",{className:"w-12 h-12 rounded-xl bg-accent-500/15 flex items-center justify-center",children:e.jsx(y,{size:24,className:"text-accent-400"})})]})}),e.jsx("div",{className:"card border-accent-500/30 bg-gradient-to-br from-accent-500/10 to-transparent",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-slate-400 text-sm font-medium uppercase tracking-wide mb-1",children:"Active Members"}),e.jsx("p",{className:"text-4xl font-bold text-white",children:t.activeMembers})]}),e.jsx("div",{className:"w-12 h-12 rounded-xl bg-accent-500/15 flex items-center justify-center",children:e.jsx(P,{size:24,className:"text-accent-400"})})]})}),e.jsx("div",{className:"card border-accent-500/30 bg-gradient-to-br from-accent-500/10 to-transparent",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-slate-400 text-sm font-medium uppercase tracking-wide mb-1",children:"Recurring Schedules"}),e.jsx("p",{className:"text-4xl font-bold text-white",children:t.activeSchedules})]}),e.jsx("div",{className:"w-12 h-12 rounded-xl bg-accent-500/15 flex items-center justify-center",children:e.jsx(_,{size:24,className:"text-accent-400"})})]})})]}),t.scoreTrend.length>0&&e.jsxs("div",{className:"card mb-10",children:[e.jsxs("h2",{className:"text-xl font-bold text-white flex items-center gap-2 mb-6",children:[e.jsx(y,{size:20,className:"text-accent-400"}),"Score Trend"]}),e.jsx(R,{data:t.scoreTrend})]}),e.jsxs("div",{className:"card mb-10",children:[e.jsxs("h2",{className:"text-xl font-bold text-white flex items-center gap-2 mb-6",children:[e.jsx(f,{size:20,className:"text-accent-400"}),"Scan Funnel"]}),e.jsxs("div",{className:"py-2",children:[e.jsx(S,{label:"Scans Started",value:m.scanStarted,maxValue:m.scanStarted,color:"bg-accent-500"}),e.jsx(S,{label:"Scans Completed",value:m.scanCompleted,maxValue:m.scanStarted,color:"bg-blue-500"}),e.jsx("p",{className:"text-xs text-slate-500 mt-4 ml-44",children:"Report views and conversion tracking not yet available. Event tracking required."})]})]}),j.length>0&&e.jsxs("div",{className:"card mb-10",children:[e.jsxs("h2",{className:"text-xl font-bold text-white flex items-center gap-2 mb-6",children:[e.jsx(w,{size:20,className:"text-accent-400"}),"Average Scores by Dimension"]}),e.jsx("div",{className:"py-2",children:j.map(([a,r])=>e.jsx(I,{name:a,score:r,maxScore:T},a))})]}),t.recentEvents.length>0&&e.jsxs("div",{className:"card",children:[e.jsxs("h2",{className:"text-xl font-bold text-white flex items-center gap-2 mb-6",children:[e.jsx(f,{size:20,className:"text-accent-400"}),"Recent Activity"]}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-slate-700/70",children:[e.jsx("th",{className:"text-left px-4 py-3 text-xs font-semibold text-slate-400 uppercase tracking-wider",children:"Type"}),e.jsx("th",{className:"text-left px-4 py-3 text-xs font-semibold text-slate-400 uppercase tracking-wider",children:"Description"}),e.jsx("th",{className:"text-left px-4 py-3 text-xs font-semibold text-slate-400 uppercase tracking-wider",children:"Date"})]})}),e.jsx("tbody",{className:"divide-y divide-slate-700/40",children:t.recentEvents.slice(0,20).map((a,r)=>e.jsxs("tr",{className:"hover:bg-white/[0.02] transition-colors",children:[e.jsx("td",{className:"px-4 py-3",children:e.jsx("span",{className:"inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-accent-500/15 text-accent-400 capitalize",children:a.type.replace(/_/g," ")})}),e.jsx("td",{className:"px-4 py-3 text-slate-300 text-sm",children:a.description}),e.jsx("td",{className:"px-4 py-3 text-slate-400 text-sm whitespace-nowrap",children:new Date(a.date).toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric",hour:"2-digit",minute:"2-digit"})})]},`${a.date}-${r}`))})]})})]})]})]})}export{X as AnalyticsPage,X 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.