1import{w as I,r as h,C as e,aI as o,bj as d,bi as x,aJ as m,b9 as $,aH as M,br as p,bf as B,F as j,an as u}from"./index-85vHsyJ2.js";import{T as H,a as Q,b as v,c as b}from"./tabs-Dm0GC7UW.js";import{x as J,a as V,T as y,j as w,v as W}from"./main-B2dcDIOH.js";const K=()=>{const g=I(),[_,C]=h.useState(!1),[A,N]=h.useState(!0),[s,T]=h.useState(null),[D,R]=h.useState([]),[E,F]=h.useState([]);h.useEffect(()=>{(async()=>{const{data:{user:i}}=await j.auth.getUser();if(!i){g("/");return}const{data:c}=await j.from("profiles").select("role").eq("id",i.id).single();if((c==null?void 0:c.role)!=="admin"){u.error("Unauthorized access"),g("/");return}C(!0),P()})()},[g]);const P=async()=>{N(!0);try{const{data:t,error:i}=await j.from("quiz_sessions").select("*");if(i)throw i;if(t){const c=t.filter(l=>l.completed_at),f=t.filter(l=>!l.completed_at&&l.completion_rate>0),r=c.filter(l=>l.overall_score),n=t.filter(l=>l.was_resumed),L={totalSessions:t.length,completedSessions:c.length,abandonedSessions:f.length,completionRate:t.length>0?c.length/t.length*100:0,avgCompletionRate:t.length>0?t.reduce((l,a)=>l+(a.completion_rate||0),0)/t.length:0,avgTimeSpent:c.length>0?c.reduce((l,a)=>l+(a.time_spent_seconds||0),0)/c.length:0,avgOverallScore:r.length>0?r.reduce((l,a)=>l+(a.overall_score||0),0)/r.length:0,avgStressScore:r.length>0?r.reduce((l,a)=>l+(a.stress_score||0),0)/r.length:0,avgAlphaScore:r.length>0?r.reduce((l,a)=>l+(a.alpha_score||0),0)/r.length:0,avgSleepScore:r.length>0?r.reduce((l,a)=>l+(a.sleep_score||0),0)/r.length:0,avgCreativityScore:r.length>0?r.reduce((l,a)=>l+(a.creativity_score||0),0)/r.length:0,avgExecutiveScore:r.length>0?r.reduce((l,a)=>l+(a.executive_score||0),0)/r.length:0,resumeRate:t.length>0?n.length/t.length*100:0,resumedSessions:n.length};T(L);const O=c.reduce((l,a)=>{const S=a.profile_type||"unknown";return l[S]=(l[S]||0)+1,l},{}),q=Object.entries(O).map(([l,a])=>({profile_type:l,count:a,percentage:a/c.length*100})).sort((l,a)=>a.count-l.count);R(q);const U=t.sort((l,a)=>new Date(a.created_at).getTime()-new Date(l.created_at).getTime()).slice(0,10);F(U)}}catch(t){console.error("Error loading analytics:",t),u.error("Failed to load analytics")}finally{N(!1)}},k=async()=>{try{const{data:t}=await j.from("quiz_sessions").select("*").order("created_at",{ascending:!1});if(t){const i=[["Email","First Name","Completed","Overall Score","Stress","Alpha","Sleep","Creativity","Executive","Profile Type","Time Spent (min)","Started At"].join(","),...t.map(n=>[n.email,n.first_name||"",n.completed_at?"Yes":"No",n.overall_score||"",n.stress_score||"",n.alpha_score||"",n.sleep_score||"",n.creativity_score||"",n.executive_score||"",n.profile_type||"",n.time_spent_seconds?Math.round(n.time_spent_seconds/60):"",new Date(n.created_at).toLocaleString()].join(","))].join(` 2`),c=new Blob([i],{type:"text/csv"}),f=URL.createObjectURL(c),r=document.createElement("a");r.href=f,r.download=`quiz-analytics-${new Date().toISOString().split("T")[0]}.csv`,r.click(),u.success("Data exported successfully")}}catch(t){console.error("Export error:",t),u.error("Failed to export data")}},z=t=>({stressed_achiever:"Stressed Achiever",scattered_creative:"Scattered Creative",rigid_performer:"Rigid Performer",exhausted_innovator:"Exhausted Innovator",optimal_performer:"Optimal Performer",untapped_potential:"Untapped Potential"})[t]||t;return!_||A?e.jsx("div",{className:"min-h-screen flex items-center justify-center",children:e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"animate-spin rounded-full h-12 w-12 border-b-2 border-primary mx-auto mb-4"}),e.jsx("p",{className:"text-slate-600",children:"Loading analytics..."})]})}):e.jsx("div",{className:"min-h-screen bg-slate-50 p-8",children:e.jsxs("div",{className:"max-w-7xl mx-auto",children:[e.jsxs("div",{className:"flex justify-between items-center mb-8",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold text-slate-900",children:"Quiz Analytics Dashboard"}),e.jsx("p",{className:"text-slate-600 mt-1",children:"Performance assessment insights and metrics"})]}),e.jsxs("button",{onClick:k,className:"flex items-center gap-2 px-4 py-2 bg-primary text-white rounded-lg hover:bg-primary/90 transition-colors",children:[e.jsx(J,{className:"w-4 h-4"}),"Export Data"]})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8",children:[e.jsxs(o,{children:[e.jsxs(d,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(x,{className:"text-sm font-medium text-slate-600",children:"Total Sessions"}),e.jsx(V,{className:"w-4 h-4 text-slate-400"})]}),e.jsxs(m,{children:[e.jsx("div",{className:"text-2xl font-bold",children:(s==null?void 0:s.totalSessions)||0}),e.jsx("p",{className:"text-xs text-slate-500 mt-1",children:"All quiz attempts"})]})]}),e.jsxs(o,{children:[e.jsxs(d,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(x,{className:"text-sm font-medium text-slate-600",children:"Completion Rate"}),e.jsx(y,{className:"w-4 h-4 text-slate-400"})]}),e.jsxs(m,{children:[e.jsxs("div",{className:"text-2xl font-bold",children:[s==null?void 0:s.completionRate.toFixed(1),"%"]}),e.jsxs("p",{className:"text-xs text-slate-500 mt-1",children:[s==null?void 0:s.completedSessions," / ",s==null?void 0:s.totalSessions," completed"]})]})]}),e.jsxs(o,{children:[e.jsxs(d,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(x,{className:"text-sm font-medium text-slate-600",children:"Avg Time"}),e.jsx($,{className:"w-4 h-4 text-slate-400"})]}),e.jsxs(m,{children:[e.jsxs("div",{className:"text-2xl font-bold",children:[s!=null&&s.avgTimeSpent?Math.round(s.avgTimeSpent/60):0," min"]}),e.jsx("p",{className:"text-xs text-slate-500 mt-1",children:"Average completion time"})]})]}),e.jsxs(o,{children:[e.jsxs(d,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(x,{className:"text-sm font-medium text-slate-600",children:"Avg Score"}),e.jsx(M,{className:"w-4 h-4 text-slate-400"})]}),e.jsxs(m,{children:[e.jsxs("div",{className:"text-2xl font-bold",children:[s==null?void 0:s.avgOverallScore.toFixed(0),"/100"]}),e.jsx("p",{className:"text-xs text-slate-500 mt-1",children:"Overall performance"})]})]}),e.jsxs(o,{children:[e.jsxs(d,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsx(x,{className:"text-sm font-medium text-slate-600",children:"Resume Rate"}),e.jsx(y,{className:"w-4 h-4 text-slate-400"})]}),e.jsxs(m,{children:[e.jsxs("div",{className:"text-2xl font-bold text-purple-600",children:[s==null?void 0:s.resumeRate.toFixed(1),"%"]}),e.jsxs("p",{className:"text-xs text-slate-500 mt-1",children:[s==null?void 0:s.resumedSessions," resumed sessions"]})]})]})]}),e.jsxs(H,{defaultValue:"overview",className:"space-y-6",children:[e.jsxs(Q,{children:[e.jsx(v,{value:"overview",children:"Overview"}),e.jsx(v,{value:"profiles",children:"Profiles"}),e.jsx(v,{value:"recent",children:"Recent Sessions"})]}),e.jsx(b,{value:"overview",className:"space-y-6",children:e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[e.jsxs(o,{children:[e.jsxs(d,{children:[e.jsx(x,{children:"Average Dimension Scores"}),e.jsx(p,{children:"Performance across 5 key areas"})]}),e.jsx(m,{className:"space-y-4",children:[{label:"Stress Management",value:(s==null?void 0:s.avgStressScore)||0,color:"bg-emerald-500"},{label:"Alpha State Access",value:(s==null?void 0:s.avgAlphaScore)||0,color:"bg-blue-500"},{label:"Sleep & Recovery",value:(s==null?void 0:s.avgSleepScore)||0,color:"bg-indigo-500"},{label:"Creativity",value:(s==null?void 0:s.avgCreativityScore)||0,color:"bg-purple-500"},{label:"Executive Function",value:(s==null?void 0:s.avgExecutiveScore)||0,color:"bg-orange-500"}].map(t=>e.jsxs("div",{children:[e.jsxs("div",{className:"flex justify-between mb-2",children:[e.jsx("span",{className:"text-sm font-medium text-slate-700",children:t.label}),e.jsx("span",{className:"text-sm font-semibold",children:t.value.toFixed(0)})]}),e.jsx("div",{className:"w-full bg-slate-200 rounded-full h-2",children:e.jsx("div",{className:`${t.color} h-2 rounded-full transition-all`,style:{width:`${t.value}%`}})})]},t.label))})]}),e.jsxs(o,{children:[e.jsxs(d,{children:[e.jsx(x,{children:"Session Status"}),e.jsx(p,{children:"Breakdown of quiz attempts"})]}),e.jsxs(m,{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between p-4 bg-green-50 rounded-lg",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(w,{className:"w-5 h-5 text-green-600"}),e.jsxs("div",{children:[e.jsx("p",{className:"font-semibold text-slate-900",children:"Completed"}),e.jsx("p",{className:"text-sm text-slate-600",children:"Finished assessment"})]})]}),e.jsx("span",{className:"text-2xl font-bold text-green-600",children:(s==null?void 0:s.completedSessions)||0})]}),e.jsxs("div",{className:"flex items-center justify-between p-4 bg-amber-50 rounded-lg",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(B,{className:"w-5 h-5 text-amber-600"}),e.jsxs("div",{children:[e.jsx("p",{className:"font-semibold text-slate-900",children:"Abandoned"}),e.jsx("p",{className:"text-sm text-slate-600",children:"Started but not finished"})]})]}),e.jsx("span",{className:"text-2xl font-bold text-amber-600",children:(s==null?void 0:s.abandonedSessions)||0})]})]})]})]})}),e.jsx(b,{value:"profiles",children:e.jsxs(o,{children:[e.jsxs(d,{children:[e.jsx(x,{children:"Profile Type Distribution"}),e.jsx(p,{children:"Performance personality breakdown"})]}),e.jsx(m,{children:e.jsx("div",{className:"space-y-4",children:D.map(t=>e.jsxs("div",{children:[e.jsxs("div",{className:"flex justify-between mb-2",children:[e.jsx("span",{className:"text-sm font-medium text-slate-700",children:z(t.profile_type)}),e.jsxs("span",{className:"text-sm font-semibold",children:[t.count," (",t.percentage.toFixed(1),"%)"]})]}),e.jsx("div",{className:"w-full bg-slate-200 rounded-full h-2",children:e.jsx("div",{className:"bg-primary h-2 rounded-full transition-all",style:{width:`${t.percentage}%`}})})]},t.profile_type))})})]})}),e.jsx(b,{value:"recent",children:e.jsxs(o,{children:[e.jsxs(d,{children:[e.jsx(x,{children:"Recent Sessions"}),e.jsx(p,{children:"Latest quiz attempts"})]}),e.jsx(m,{children:e.jsx("div",{className:"space-y-3",children:E.map(t=>{var i;return e.jsxs("div",{className:"flex items-center justify-between p-4 bg-slate-50 rounded-lg hover:bg-slate-100 transition-colors",children:[e.jsxs("div",{className:"flex-1",children:[e.jsxs("p",{className:"font-semibold text-slate-900",children:[t.first_name||"Anonymous"," (",t.email,")"]}),e.jsxs("div",{className:"flex gap-4 mt-1 text-sm text-slate-600",children:[e.jsx("span",{children:new Date(t.created_at).toLocaleDateString()}),e.jsx("span",{children:"â¢"}),e.jsxs("span",{children:[(i=t.completion_rate)==null?void 0:i.toFixed(0),"% complete"]}),t.overall_score&&e.jsxs(e.Fragment,{children:[e.jsx("span",{children:"â¢"}),e.jsxs("span",{children:["Score: ",t.overall_score,"/100"]})]})]})]}),t.completed_at?e.jsx(w,{className:"w-5 h-5 text-green-600"}):e.jsx(W,{className:"w-5 h-5 text-slate-400"})]},t.id)})})})]})})]})]})})};export{K 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.