1import{n as me,j as e,at as ue,au as he,av as pe,aw as je,ai as q,aD as ge,E as X,aO as _e,aF as ke,B as L,b8 as Se,aT as Ae,u as fe,J as R,r as T,C as d,b as f,c as b,cl as ve,e as o,a$ as I,by as Ne,aH as z,U as H,F as ee,b5 as Ce,S as se,s as te,t as ae,v as re,w as A,y as j,aI as ne,Z as Re,b4 as le,ax as W,aC as Te,i as M,ac as B,bj as Fe,cm as ie,T as Me,O as C,bi as ce,cb as qe,H as De,b2 as Le,Q as Pe,R as Oe,V as O,M as Ue,W as U}from"./index-_XRcRoWC.js";import{u as F}from"./useQuery-CsY5Jov_.js";import{T as K,a as Q,b as D,c as v,d as J,e as N}from"./table-C34BedAi.js";import{A as Be}from"./arrow-up-down-_BkTEGVZ.js";import{u as Ee}from"./useMutation-ClQQAB4a.js";import{R as de}from"./rotate-ccw-DkLyfNQA.js";import{T as Ie}from"./target-CcTnaW94.js";import{P as oe}from"./pause-C3Uc4v-x.js";import{R as ze}from"./refresh-cw-BoruOkkQ.js";import{C as He}from"./credit-card-B0dsPsu8.js";import{W as Ve}from"./wrench-_JRxxNmo.js";/** 2 * @license lucide-react v0.462.0 - ISC 3 * 4 * This source code is licensed under the ISC license. 5 * See the LICENSE file in the root directory of this source tree. 6 */const Ke=me("ListOrdered",[["path",{d:"M10 12h11",key:"6m4ad9"}],["path",{d:"M10 18h11",key:"11hvi2"}],["path",{d:"M10 6h11",key:"c7qv1k"}],["path",{d:"M4 10h2",key:"16xx2s"}],["path",{d:"M4 6h1v4",key:"cnovpq"}],["path",{d:"M6 18H4c0-1 2-2 2-3s-1-1.5-2-1",key:"m9a95d"}]]);/** 7 * @license lucide-react v0.462.0 - ISC 8 * 9 * This source code is licensed under the ISC license. 10 * See the LICENSE file in the root directory of this source tree. 11 */const Qe=me("Route",[["circle",{cx:"6",cy:"19",r:"3",key:"1kj8tv"}],["path",{d:"M9 19h8.5a3.5 3.5 0 0 0 0-7h-11a3.5 3.5 0 0 1 0-7H15",key:"1d8sl"}],["circle",{cx:"18",cy:"5",r:"3",key:"gq8acd"}]]);function Je({open:g,onOpenChange:n,title:u,description:y,changes:l,onConfirm:m,onCancel:c,confirmLabel:h="Confirm Changes",cancelLabel:w="Cancel",isDestructive:S=!1,isLoading:p=!1}){const _=l.some(t=>t.impact==="negative");return e.jsx(ue,{open:g,onOpenChange:n,children:e.jsxs(he,{className:"sm:max-w-md",children:[e.jsxs(pe,{children:[e.jsxs(je,{className:"flex items-center gap-2",children:[_&&e.jsx(q,{className:"h-5 w-5 text-warning"}),u]}),e.jsx(ge,{children:y})]}),e.jsxs("div",{className:"space-y-3 py-4",children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground uppercase tracking-wide",children:"What will change"}),l.map((t,i)=>e.jsxs("div",{className:X("flex items-center justify-between p-3 rounded-lg border bg-muted/30",t.impact==="negative"&&"border-destructive/30 bg-destructive/5",t.impact==="positive"&&"border-success/30 bg-success/5"),children:[e.jsx("span",{className:"text-sm font-medium",children:t.label}),e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx("span",{className:"text-muted-foreground",children:t.before}),e.jsx(_e,{className:"h-3 w-3 text-muted-foreground"}),e.jsx("span",{className:X("font-medium",t.impact==="negative"&&"text-destructive",t.impact==="positive"&&"text-success"),children:t.after})]})]},i))]}),_&&e.jsxs("div",{className:"flex items-start gap-2 p-3 rounded-lg bg-warning/10 border border-warning/20 text-sm",children:[e.jsx(q,{className:"h-4 w-4 text-warning mt-0.5 flex-shrink-0"}),e.jsx("p",{className:"text-muted-foreground",children:"This change may have a negative impact. Please review carefully before confirming."})]}),e.jsxs(ke,{className:"gap-2 sm:gap-0",children:[e.jsxs(L,{variant:"outline",onClick:c,disabled:p,children:[e.jsx(Se,{className:"h-4 w-4 mr-2"}),w]}),e.jsxs(L,{variant:S?"destructive":"default",onClick:m,disabled:p,children:[e.jsx(Ae,{className:"h-4 w-4 mr-2"}),h]})]})]})})}function We(){const{user:g}=fe(),n=F({queryKey:["is-platform-admin",g==null?void 0:g.id],queryFn:async()=>{if(!g)return!1;const{data:m,error:c}=await R.rpc("is_platform_admin",{_user_id:g.id});return c?(console.error("Error checking platform admin status:",c),!1):m===!0},enabled:!!g}),u=F({queryKey:["platform-stats"],queryFn:async()=>{const{data:m,error:c}=await R.rpc("get_platform_stats");if(c)throw c;return(m==null?void 0:m[0])||null},enabled:n.data===!0}),y=F({queryKey:["platform-tenants"],queryFn:async()=>{const{data:m,error:c}=await R.rpc("get_all_tenants");if(c)throw c;return m},enabled:n.data===!0}),l=F({queryKey:["platform-users"],queryFn:async()=>{const{data:m,error:c}=await R.rpc("get_all_users");if(c)throw c;return m},enabled:n.data===!0});return{isPlatformAdmin:n.data===!0,isLoading:n.isLoading,stats:u.data,statsLoading:u.isLoading,tenants:y.data||[],tenantsLoading:y.isLoading,users:l.data||[],usersLoading:l.isLoading,refetch:()=>{u.refetch(),y.refetch(),l.refetch()}}}const xe={bug:0,confusing:1,"nice-to-have":2},$e={bug:{label:"Blocker",color:"destructive",icon:ve},confusing:{label:"Friction",color:"outline",icon:I},"nice-to-have":{label:"Polish",color:"secondary",icon:Ne}};function Ge(){var _;const[g,n]=T.useState("priority"),[u,y]=T.useState("all"),{data:l=[],isLoading:m}=F({queryKey:["feedback-queue",u],queryFn:async()=>{let t=R.from("feedback_queue").select("*").order("created_at",{ascending:!1});u!=="all"&&(t=t.eq("status",u));const{data:i,error:s}=await t;if(s)throw s;return i}}),{data:c=[]}=F({queryKey:["feedback-resolutions"],queryFn:async()=>{const{data:t,error:i}=await R.from("feedback_resolutions").select("*").order("created_at",{ascending:!1});if(i)throw i;return t}}),h=c.reduce((t,i)=>(t[i.id]=i,t),{}),w=l.reduce((t,i)=>(t[i.current_screen]=(t[i.current_screen]||0)+1,t),{}),S=[...l].sort((t,i)=>{switch(g){case"priority":return(i.priority_score||0)-(t.priority_score||0);case"severity":return xe[t.severity]-xe[i.severity];
11case"recent":return new Date(i.created_at).getTime()-new Date(t.created_at).getTime();case"screen":return(w[i.current_screen]||0)-(w[t.current_screen]||0);default:return 0}}),p={blockers:l.filter(t=>t.severity==="bug").length,friction:l.filter(t=>t.severity==="confusing").length,polish:l.filter(t=>t.severity==="nice-to-have").length,autoResolved:l.filter(t=>t.auto_resolved).length,flaggedForReview:l.filter(t=>t.risk_level==="high").length,routedToManual:c.filter(t=>t.routed_to_manual).length,priorityBoosted:l.filter(t=>(t.similar_count||1)>=3).length,withRecommendations:c.filter(t=>t.product_recommendation).length,topScreen:((_=Object.entries(w).sort(([,t],[,i])=>i-t)[0])==null?void 0:_[0])||"N/A"};return m?e.jsx("div",{className:"text-muted-foreground",children:"Loading feedback..."}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 lg:grid-cols-9 gap-4",children:[e.jsxs(d,{children:[e.jsx(f,{className:"pb-2",children:e.jsxs(b,{className:"text-sm font-medium flex items-center gap-2",children:[e.jsx(ve,{className:"h-4 w-4 text-destructive"}),"Blockers"]})}),e.jsx(o,{children:e.jsx("div",{className:"text-2xl font-bold",children:p.blockers})})]}),e.jsxs(d,{children:[e.jsx(f,{className:"pb-2",children:e.jsxs(b,{className:"text-sm font-medium flex items-center gap-2",children:[e.jsx(I,{className:"h-4 w-4 text-warning"}),"Friction"]})}),e.jsx(o,{children:e.jsx("div",{className:"text-2xl font-bold",children:p.friction})})]}),e.jsxs(d,{children:[e.jsx(f,{className:"pb-2",children:e.jsxs(b,{className:"text-sm font-medium flex items-center gap-2",children:[e.jsx(Ne,{className:"h-4 w-4 text-muted-foreground"}),"Polish"]})}),e.jsx(o,{children:e.jsx("div",{className:"text-2xl font-bold",children:p.polish})})]}),e.jsxs(d,{className:"bg-green-50 dark:bg-green-950/20 border-green-200 dark:border-green-800",children:[e.jsx(f,{className:"pb-2",children:e.jsxs(b,{className:"text-sm font-medium flex items-center gap-2 text-green-700 dark:text-green-400",children:[e.jsx(z,{className:"h-4 w-4"}),"Auto-Resolved"]})}),e.jsx(o,{children:e.jsx("div",{className:"text-2xl font-bold text-green-700 dark:text-green-400",children:p.autoResolved})})]}),e.jsxs(d,{className:"bg-amber-50 dark:bg-amber-950/20 border-amber-200 dark:border-amber-800",children:[e.jsx(f,{className:"pb-2",children:e.jsxs(b,{className:"text-sm font-medium flex items-center gap-2 text-amber-700 dark:text-amber-400",children:[e.jsx(q,{className:"h-4 w-4"}),"Needs Review"]})}),e.jsx(o,{children:e.jsx("div",{className:"text-2xl font-bold text-amber-700 dark:text-amber-400",children:p.flaggedForReview})})]}),e.jsxs(d,{className:"bg-blue-50 dark:bg-blue-950/20 border-blue-200 dark:border-blue-800",children:[e.jsx(f,{className:"pb-2",children:e.jsxs(b,{className:"text-sm font-medium flex items-center gap-2 text-blue-700 dark:text-blue-400",children:[e.jsx(I,{className:"h-4 w-4"}),"Low Confidence"]})}),e.jsx(o,{children:e.jsx("div",{className:"text-2xl font-bold text-blue-700 dark:text-blue-400",children:p.routedToManual})})]}),e.jsxs(d,{className:"bg-purple-50 dark:bg-purple-950/20 border-purple-200 dark:border-purple-800",children:[e.jsx(f,{className:"pb-2",children:e.jsxs(b,{className:"text-sm font-medium flex items-center gap-2 text-purple-700 dark:text-purple-400",children:[e.jsx(H,{className:"h-4 w-4"}),"Priority Boosted"]})}),e.jsx(o,{children:e.jsx("div",{className:"text-2xl font-bold text-purple-700 dark:text-purple-400",children:p.priorityBoosted})})]}),e.jsxs(d,{children:[e.jsx(f,{className:"pb-2",children:e.jsxs(b,{className:"text-sm font-medium flex items-center gap-2",children:[e.jsx(ee,{className:"h-4 w-4"}),"Product Recs"]})}),e.jsx(o,{children:e.jsx("div",{className:"text-2xl font-bold",children:p.withRecommendations})})]}),e.jsxs(d,{children:[e.jsx(f,{className:"pb-2",children:e.jsxs(b,{className:"text-sm font-medium flex items-center gap-2",children:[e.jsx(Ce,{className:"h-4 w-4"}),"Top Screen"]})}),e.jsx(o,{children:e.jsx("div",{className:"text-sm font-mono truncate",children:p.topScreen})})]})]}),e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Be,{className:"h-4 w-4 text-muted-foreground"}),e.jsxs(se,{value:g,onValueChange:t=>n(t),children:[e.jsx(te,{className:"w-[180px]",children:e.jsx(ae,{placeholder:"Sort by"})}),e.jsxs(re,{children:[e.jsx(A,{value:"priority",children:"Priority Score"}),e.jsx(A,{value:"severity",children:"Severity"}),e.jsx(A,{value:"recent",children:"Most Recent"}),e.jsx(A,{value:"screen",children:"Screen Frequency"})]})]})]}),e.jsxs(se,{value:u,onValueChange:y,children:[e.jsx(te,{className:"w-[160px]",children:e.jsx(ae,{placeholder:"Status"})}),e.jsxs(re,{children:[e.jsx(A,{value:"all",children:"All"}),e.jsx(A,{value:"new",children:"New"}),e.jsx(A,{value:"auto_resolved",children:"Auto-Resolved"}),e.jsx(A,{value:"flagged",children:"Flagged for Review"}),e.jsx(A,{value:"pending_review",children:"Pending Review"}),e.jsx(A,{value:"reviewed",children:"Reviewed"}),e.jsx(A,{value:"resolved",children:"Resolved"}),e.jsx(A,{value:"dismissed",children:"Dismissed"})]})]})]}),e.jsx("div",{className:"space-y-3",children:S.length===0?e.jsx(d,{children:e.jsxs(o,{className:"py-8 text-center text-muted-foreground",children:["No ",u==="all"?"":u," feedback items"]})}):S.map(t=>{const i=$e[t.severity],s=i.icon,a=w[t.current_screen]||0,r=t.resolution_id?h[t.resolution_id]:null;return e.jsx(d,{className:t.auto_resolved?"border-green-200 dark:border-green-800 bg-green-50/50 dark:bg-green-950/10":t.risk_level==="high"?"border-amber-200 dark:border-amber-800 bg-amber-50/50 dark:bg-amber-950/10":"",children:e.jsx(o,{className:"py-4",children:e.jsxs("div",{className:"flex items-start gap-4",children:[e.jsx("div",{className:"mt-1",children:t.auto_resolved?e.jsx(z,{className:"h-5 w-5 text-green-600"}):t.risk_level==="high"?e.jsx(q,{className:"h-5 w-5 text-amber-600"}):e.jsx(s,{className:`h-5 w-5 ${t.severity==="bug"?"text-destructive":t.severity==="confusing"?"text-warning":"text-muted-foreground"}`})}),e.jsxs("div",{className:"flex-1 space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx(j,{variant:i.color,children:i.label}),e.jsx(j,{variant:"outline",className:"font-mono text-xs",children:t.current_screen}),a>1&&e.jsxs(j,{variant:"secondary",className:"text-xs",children:[a,"Ã on this screen"]}),t.auto_resolved&&e.jsxs(j,{className:"bg-green-500 text-white text-xs",children:[e.jsx(ne,{className:"h-3 w-3 mr-1"}),"Auto-Resolved"]}),(r==null?void 0:r.routed_to_manual)&&e.jsxs(j,{className:"bg-blue-500 text-white text-xs",children:[e.jsx(I,{className:"h-3 w-3 mr-1"}),"Routed to Manual"]}),t.risk_level==="high"&&!t.auto_resolved&&e.jsx(j,{variant:"destructive",className:"text-xs",children:"High Risk - Manual Review"}),(t.similar_count||1)>1&&e.jsxs(j,{className:"bg-purple-500 text-white text-xs",children:[e.jsx(H,{className:"h-3 w-3 mr-1"}),t.similar_count," reports"]}),(r==null?void 0:r.priority_boosted)&&e.jsxs(j,{className:"bg-orange-500 text-white text-xs",children:[e.jsx(Re,{className:"h-3 w-3 mr-1"}
11),"Prioritized"]}),t.category&&e.jsx(j,{variant:"outline",className:"text-xs",children:t.category.replace(/_/g," ")})]}),e.jsx("p",{className:"text-sm",children:t.message}),r&&e.jsxs("div",{className:"mt-2 p-3 rounded-md bg-muted/50 text-sm space-y-1",children:[e.jsxs("p",{className:"font-medium flex items-center gap-1",children:[e.jsx(ne,{className:"h-3 w-3"}),"AI Analysis"]}),r.ai_analysis&&e.jsx("p",{className:"text-muted-foreground",children:r.ai_analysis}),r.suggested_fix&&e.jsxs("p",{className:"text-xs",children:[e.jsx("span",{className:"font-medium",children:"Suggested fix:"})," ",r.suggested_fix]}),r.auto_applied&&r.applied_at&&e.jsxs("p",{className:"text-xs text-green-600 dark:text-green-400",children:["â Applied automatically ",le(new Date(r.applied_at),{addSuffix:!0}),r.auto_fix_expanded&&" (expanded eligibility due to multiple reports)"]}),r.routed_to_manual&&r.routing_reason&&e.jsxs("p",{className:"text-xs text-blue-600 dark:text-blue-400 italic",children:["â ",r.routing_reason]}),r.product_recommendation&&e.jsxs("div",{className:"mt-2 p-2 rounded bg-purple-100 dark:bg-purple-900/30 border border-purple-200 dark:border-purple-800",children:[e.jsxs("p",{className:"text-xs font-medium text-purple-700 dark:text-purple-300 flex items-center gap-1",children:[e.jsx(ee,{className:"h-3 w-3"}),"Product Recommendation"]}),e.jsx("p",{className:"text-xs text-purple-600 dark:text-purple-400 mt-1",children:r.product_recommendation})]})]}),e.jsxs("div",{className:"flex items-center gap-4 text-xs text-muted-foreground",children:[e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(W,{className:"h-3 w-3"}),le(new Date(t.created_at),{addSuffix:!0})]}),t.user_role&&e.jsxs("span",{children:["Role: ",t.user_role]}),t.risk_level&&e.jsxs("span",{children:["Risk: ",t.risk_level]})]})]})]})})},t.id)})})]})}function Ye(){var t,i;const g=Te(),[n,u]=T.useState(null),[y,l]=T.useState(!1),[m,c]=T.useState(!1),{data:h,isLoading:w}=F({queryKey:["auto-fixes-review"],queryFn:async()=>{const{data:s,error:a}=await R.from("feedback_resolutions").select(` 12 id, 13 feedback_id, 14 category, 15 resolution_type, 16 suggested_fix, 17 ai_analysis, 18 before_state, 19 after_state, 20 applied_at, 21 impacted_screens, 22 impacted_fields, 23 is_reverted, 24 reverted_at, 25 reverted_by, 26 feedback_queue!feedback_resolutions_feedback_id_fkey ( 27 message, 28 current_screen, 29 severity, 30 user_id 31 ) 32 `).eq("auto_applied",!0).order("applied_at",{ascending:!1}).limit(100);if(a)throw a;return(s||[]).map(r=>({...r,feedback:r.feedback_queue}))}}),S=Ee({mutationFn:async s=>{const{data:{user:a}}=await R.auth.getUser();if(!a)throw new Error("Not authenticated");const{error:r}=await R.from("feedback_resolutions").update({is_reverted:!0,reverted_at:new Date().toISOString(),reverted_by:a.id}).eq("id",s);if(r)throw r},onSuccess:()=>{g.invalidateQueries({queryKey:["auto-fixes-review"]}),ie({title:"Auto-fix reverted",description:"The change has been marked as reverted. Manual restoration may be needed."}),c(!1),u(null)},onError:s=>{ie({title:"Failed to revert",description:s.message,variant:"destructive"})}}),p=s=>s.replace(/_/g," ").replace(/\b\w/g,a=>a.toUpperCase()),_={total:(h==null?void 0:h.length)||0,active:(h==null?void 0:h.filter(s=>!s.is_reverted).length)||0,reverted:(h==null?void 0:h.filter(s=>s.is_reverted).length)||0};return w?e.jsxs(d,{children:[e.jsx(f,{children:e.jsx(b,{children:"Auto-Fix Review"})}),e.jsx(o,{children:e.jsx("p",{className:"text-muted-foreground",children:"Loading..."})})]}):e.jsxs(e.Fragment,{children:[e.jsxs(d,{children:[e.jsxs(f,{children:[e.jsxs(b,{className:"flex items-center gap-2",children:[e.jsx(z,{className:"h-5 w-5 text-success"}),"Auto-Fix Review Log"]}),e.jsx(M,{children:"Review all automatically applied fixes. All changes are logged and reversible."})]}),e.jsxs(o,{children:[e.jsxs("div",{className:"grid grid-cols-3 gap-4 mb-6",children:[e.jsxs("div",{className:"p-3 rounded-lg bg-muted/50 text-center",children:[e.jsx("p",{className:"text-2xl font-bold",children:_.total}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Total Auto-Fixes"})]}),e.jsxs("div",{className:"p-3 rounded-lg bg-success/10 text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-success",children:_.active}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Active"})]}),e.jsxs("div",{className:"p-3 rounded-lg bg-warning/10 text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-warning",children:_.reverted}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Reverted"})]})]}),h&&h.length>0?e.jsx("div",{className:"rounded-md border",children:e.jsxs(K,{children:[e.jsx(Q,{children:e.jsxs(D,{children:[e.jsx(v,{children:"Applied"}),e.jsx(v,{children:"Category"}),e.jsx(v,{children:"Change"}),e.jsx(v,{children:"Screen"}),e.jsx(v,{children:"Status"}),e.jsx(v,{className:"text-right",children:"Actions"})]})}),e.jsx(J,{children:h.map(s=>{var a;return e.jsxs(D,{children:[e.jsx(N,{className:"text-sm text-muted-foreground",children:e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(W,{className:"h-3 w-3"}),B(new Date(s.applied_at),"MMM d, HH:mm")]})}),e.jsx(N,{children:e.jsx(j,{variant:"outline",className:"text-xs",children:p(s.category)})}),e.jsx(N,{className:"max-w-[200px] truncate text-sm",children:s.suggested_fix}),e.jsx(N,{className:"text-sm text-muted-foreground",children:((a=s.feedback)==null?void 0:a.current_screen)||"Unknown"}),e.jsx(N,{children:s.is_reverted?e.jsxs(j,{variant:"secondary",className:"text-xs",children:[e.jsx(de,{className:"h-3 w-3 mr-1"}),"Reverted"]}):e.jsxs(j,{className:"text-xs bg-success/20 text-success border-success/30",children:[e.jsx(z,{className:"h-3 w-3 mr-1"}),"Active"]})}),e.jsx(N,{className:"text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-1",children:[e.jsx(L,{variant:"ghost",size:"sm",onClick:()=>{u(s),l(!0)},children:e.jsx(Fe,{className:"h-4 w-4"})}),!s.is_reverted&&e.jsx(L,{variant:"ghost",size:"sm",onClick:()=>{u(s),c(!0)},children:e.jsx(de,{className:"h-4 w-4"})})]})})]},s.id)})})]})}):e.jsx("p",{className:"text-center text-muted-foreground py-8",children:"No auto-fixes have been applied yet."})]})]}),e.jsx(ue,{open:y,onOpenChange:l,children:e.jsxs(he,{className:"sm:max-w-lg",children:[e.jsxs(pe,{children:[e.jsx(je,{children:"Auto-Fix Details"}),e.jsx(ge,{children:"Complete log of this automatically applied change."})]}),n&&e.jsxs("div",{className:"space-y-4 max-h-[60vh] overflow-y-auto",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground uppercase",children:"Applied At"}),e.jsx("p",{className:"text-sm",children:B(new Date(n.applied_at),"PPpp")})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground uppercase",children:"Category"}),e.jsx("p",{className:"text-sm",children:p(n.category)})]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground uppercase mb-1",children:"Original Feedback"}),e.jsx("p",{className:"text-sm bg-muted/50 p-2 rounded",children:(t=n.feedback)==null?void 0:t.message})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground uppercase mb-1",children:"Applied Fix"}),e.jsx("p",{className:"text-sm",children:n.suggested_fix})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground uppercase mb-1",children:"AI Analysis"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:n.ai_analysis})]}),n.impacted_screens&&n.impacted_screens.length>0&&e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground uppercase mb-1",children:"Impacted Screens"}),e.jsx("div",{className:"flex flex-wrap gap-1",children:n.impacted_screens.map((s,a)=>e.jsx(j,{variant:"outline",className:"text-xs",children:s},a))})]}),n.impacted_fields&&n.impacted_fields.length>0&&e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground uppercase mb-1",children:"Impacted Fields"}),e.jsx("div",{className:"flex flex-wrap gap-1",children:n.impacted_fields.map((s,a)=>e.jsx(j,{variant:"secondary",className:"text-xs",children:s},a))})]}),(n.before_state||n.after_state)&&e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[n.before_state&&e.jsxs("div",{className:"p-2 rounded-md bg-muted/50",children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground mb-1",children:"Before State"}),e.jsx("pre",{className:"text-xs overflow-auto max-h-32",children:JSON.stringify(n.before_state,null,2)})]}),n.after_state&&e.jsxs("div",{className:"p-2 rounded-md bg-success/10",children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground mb-1",children:"After State"}),e.jsx("pre",{className:"text-xs overflow-auto max-h-32",children:JSON.stringify(n.after_state,null,2)})]})]}),n.is_reverted&&e.jsxs("div",{className:"p-3 rounded-lg bg-warning/10 border border-warning/20",children:[e.jsx("p",{className:"text-sm font-medium text-warning",children:"This fix was reverted"}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Reverted on ",B(new Date(n.reverted_at),"PPpp")]})]})]})]})}),n&&e.jsx(Je,{open:m,onOpenChange:c,title:"Revert Auto-Fix",description:"This will mark the auto-fix as reverted. You may need to manually restore the original state.",changes:[{label:"Status",before:"Active",after:"Reverted",impact:"neutral"},{label:"Change",before:((i=n.suggested_fix)==null?void 0:i.slice(0,30))+"...",after:"Marked for restoration",impact:"negative"}],onConfirm:()=>S.mutate(n.id),onCancel:()=>{c(!1),u(null)},confirmLabel:"Revert Fix",cancelLabel:"Keep Active",isDestructive:!0,isLoading:S.isPending})]})}function Ze(){const[g,n]=T.useState([]),[u,y]=T.useState(!0),[l,m]=T.useState([]);T.useEffect(()=>{c()},[]);const c=async()=>{y(!0);try{const{data:s,error:a}=await R.from("user_journey_events").select("*").order("created_at",{ascending:!1}).limit(500);if(a)throw a;const r=s||[];n(r),h(r)}catch(s){console.error("Failed to fetch journey data:",s)}finally{y(!1)}},h=s=>{const a=s.filter(x=>x.event_type==="value_reached"),r=a.length>0?Math.round(a.reduce((x,k)=>x+(k.duration_seconds||0),0)/a.length):null,be=s.filter(x=>x.event_type==="pause_detected").reduce((x,k)=>{const Z=k.screen_path||"unknown";return x[Z]=(x[Z]||0)+1,x},{}),E=Object.entries(be).sort(([,x],[,k])=>k-x)[0],$=s.filter(x=>x.event_type==="step_abandoned"),ye=$.reduce((x,k)=>(x[k.event_name]=(x[k.event_name]||0)+1,x),{}),P=Object.entries(ye).sort(([,x],[,k])=>k-x)[0],G=s.filter(x=>x.event_type==="step_completed").length,Y=G+$.length,V=Y>0?Math.round(G/Y*100):100,we=new Set(s.map(x=>{var k;
32return((k=x.metadata)==null?void 0:k.user_id)||x.id})).size;m([{label:"Avg. Time to First Value",value:r?`${r}s`:"N/A",description:"Average time until users see their first margin insights",icon:e.jsx(W,{className:"h-5 w-5 text-primary"}),trend:r&&r<120?"up":"down"},{label:"Step Completion Rate",value:`${V}%`,description:"Percentage of onboarding steps completed vs abandoned",icon:e.jsx(Ie,{className:"h-5 w-5 text-green-500"}),trend:V>=80?"up":V>=50?"neutral":"down"},{label:"Top Pause Screen",value:E?w(E[0]):"None",description:E?`${E[1]} pauses detected`:"No pauses detected yet",icon:e.jsx(oe,{className:"h-5 w-5 text-amber-500"}),trend:"neutral"},{label:"Most Abandoned Step",value:P?S(P[0]):"None",description:P?`${P[1]} abandonments`:"No abandonments detected",icon:e.jsx(q,{className:"h-5 w-5 text-destructive"}),trend:P?"down":"up"},{label:"Tracked Users",value:we,description:"Unique users with journey data",icon:e.jsx(H,{className:"h-5 w-5 text-blue-500"}),trend:"neutral"},{label:"Total Events",value:s.length,description:"Journey events captured",icon:e.jsx(Me,{className:"h-5 w-5 text-purple-500"}),trend:"neutral"}])},w=s=>s.replace(/^\//,"").replace(/-/g," ").replace(/\b\w/g,a=>a.toUpperCase())||"Home",S=s=>s.replace(/_/g," ").replace(/\b\w/g,a=>a.toUpperCase()),p=g.filter(s=>s.event_type==="pause_detected").reduce((s,a)=>{const r=a.screen_path||"unknown";return s[r]||(s[r]={count:0,avgDuration:0,durations:[]}),s[r].count++,a.duration_seconds&&s[r].durations.push(a.duration_seconds),s},{});Object.values(p).forEach(s=>{s.avgDuration=s.durations.length>0?Math.round(s.durations.reduce((a,r)=>a+r,0)/s.durations.length):0});const _=Object.entries(p).sort(([,s],[,a])=>a.count-s.count).slice(0,5),t=g.filter(s=>s.event_type==="step_abandoned").reduce((s,a)=>(s[a.event_name]=(s[a.event_name]||0)+1,s),{}),i=Object.entries(t).sort(([,s],[,a])=>a-s).slice(0,5);return u?e.jsx("div",{className:"space-y-6",children:e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4",children:[...Array(6)].map((s,a)=>e.jsxs(d,{children:[e.jsx(f,{className:"pb-2",children:e.jsx(C,{className:"h-4 w-24"})}),e.jsxs(o,{children:[e.jsx(C,{className:"h-8 w-16"}),e.jsx(C,{className:"h-3 w-full mt-2"})]})]},a))})}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-4",children:"First-Time User Flow Insights"}),e.jsx("p",{className:"text-sm text-muted-foreground mb-6",children:"Track onboarding effectiveness, identify friction points, and optimize conversion."})]}),e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4",children:l.map((s,a)=>e.jsxs(d,{children:[e.jsx(f,{className:"pb-2",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(M,{className:"text-xs font-medium uppercase tracking-wider",children:s.label}),s.icon]})}),e.jsxs(o,{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-2xl font-bold",children:s.value}),s.trend&&e.jsx(j,{variant:s.trend==="up"?"default":s.trend==="down"?"destructive":"secondary",className:"text-xs",children:s.trend==="up"?"â Good":s.trend==="down"?"â Needs attention":"â"})]}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:s.description})]})]},a))}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[e.jsxs(d,{children:[e.jsxs(f,{children:[e.jsxs(b,{className:"text-base flex items-center gap-2",children:[e.jsx(oe,{className:"h-4 w-4 text-amber-500"}),"Screens Where Users Pause (>10s)"]}),e.jsx(M,{children:"Identify where users get stuck or confused"})]}),e.jsx(o,{children:_.length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:"No pause data yet"}):e.jsx("div",{className:"space-y-3",children:_.map(([s,a])=>e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium",children:w(s)}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Avg. pause: ",a.avgDuration,"s"]})]}),e.jsxs(j,{variant:"outline",children:[a.count," pauses"]})]},s))})})]}),e.jsxs(d,{children:[e.jsxs(f,{children:[e.jsxs(b,{className:"text-base flex items-center gap-2",children:[e.jsx(q,{className:"h-4 w-4 text-destructive"}),"Abandoned Setup Steps"]}),e.jsx(M,{children:"Steps where users drop off during onboarding"})]}),e.jsx(o,{children:i.length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:"No abandonment data yet"}):e.jsx("div",{className:"space-y-3",children:i.map(([s,a])=>e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-sm font-medium",children:S(s)}),e.jsxs(j,{variant:"destructive",children:[a," abandoned"]})]},s))})})]})]})]})}function xs(){const{user:g,isLoading:n}=fe(),{isPlatformAdmin:u,isLoading:y,stats:l,statsLoading:m,tenants:c,tenantsLoading:h,users:w,usersLoading:S,refetch:p}=We(),[_,t]=T.useState("overview");if(n||y)return e.jsx("div",{className:"min-h-screen flex items-center justify-center bg-background",children:e.jsxs("div",{className:"text-center space-y-4",children:[e.jsx(ce,{className:"w-12 h-12 text-primary mx-auto animate-pulse"}),e.jsx("p",{className:"text-muted-foreground",children:"Verifying platform admin access..."})]})});if(!g)return e.jsx(qe,{to:"/auth",replace:!0});if(!u)return e.jsx("div",{className:"min-h-screen flex items-center justify-center bg-background",children:e.jsxs(d,{className:"max-w-md",children:[e.jsxs(f,{className:"text-center",children:[e.jsx(q,{className:"w-12 h-12 text-destructive mx-auto mb-2"}),e.jsx(b,{children:"Access Denied"}),e.jsx(M,{children:"You don't have platform admin privileges. This page is only accessible to GroundedAI OS platform administrators."})]}),e.jsx(o,{className:"text-center",children:e.jsx(L,{variant:"outline",onClick:()=>window.history.back(),children:"Go Back"})})]})});const i=a=>{switch(a){case"enterprise":return"default";case"pro":return"secondary";case"basic":return"outline";default:return"outline"}},s=a=>{switch(a){case"active":return"default";case"canceled":return"destructive";case"past_due":return"destructive";default:return"secondary"}};return e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx("div",{className:"border-b bg-card",children:e.jsx("div",{className:"container py-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-10 h-10 rounded-full bg-primary/10 flex items-center justify-center",children:e.jsx(ce,{className:"w-5 h-5 text-primary"})}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-xl font-bold",children:"Platform Admin"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"GroundedAI OS Management Console"})]})]}),e.jsxs(L,{variant:"outline",size:"sm",onClick:p,children:[e.jsx(ze,{className:"w-4 h-4 mr-2"}),"Refresh"]})]})})}),e.jsxs("div",{className:"container py-6 space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-5 ga
32p-4",children:[e.jsx(d,{children:e.jsx(o,{className:"pt-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(De,{className:"w-8 h-8 text-primary"}),e.jsxs("div",{children:[m?e.jsx(C,{className:"h-8 w-16"}):e.jsx("p",{className:"text-2xl font-bold",children:(l==null?void 0:l.total_tenants)||0}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Organizations"})]})]})})}),e.jsx(d,{children:e.jsx(o,{className:"pt-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(H,{className:"w-8 h-8 text-blue-500"}),e.jsxs("div",{children:[m?e.jsx(C,{className:"h-8 w-16"}):e.jsx("p",{className:"text-2xl font-bold",children:(l==null?void 0:l.total_users)||0}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Users"})]})]})})}),e.jsx(d,{children:e.jsx(o,{className:"pt-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(Le,{className:"w-8 h-8 text-amber-500"}),e.jsxs("div",{children:[m?e.jsx(C,{className:"h-8 w-16"}):e.jsx("p",{className:"text-2xl font-bold",children:(l==null?void 0:l.total_stores)||0}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Stores"})]})]})})}),e.jsx(d,{children:e.jsx(o,{className:"pt-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(He,{className:"w-8 h-8 text-green-500"}),e.jsxs("div",{children:[m?e.jsx(C,{className:"h-8 w-16"}):e.jsx("p",{className:"text-2xl font-bold",children:(l==null?void 0:l.active_subscriptions)||0}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Active Subs"})]})]})})}),e.jsx(d,{children:e.jsx(o,{className:"pt-4",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(Ke,{className:"w-8 h-8 text-purple-500"}),e.jsxs("div",{children:[m?e.jsx(C,{className:"h-8 w-16"}):e.jsx("p",{className:"text-2xl font-bold",children:(l==null?void 0:l.waitlist_count)||0}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Waitlist"})]})]})})})]}),e.jsxs(Pe,{value:_,onValueChange:t,children:[e.jsxs(Oe,{children:[e.jsx(O,{value:"overview",children:"Organizations"}),e.jsx(O,{value:"users",children:"All Users"}),e.jsxs(O,{value:"feedback",className:"flex items-center gap-1",children:[e.jsx(Ue,{className:"h-3 w-3"}),"Feedback"]}),e.jsxs(O,{value:"autofixes",className:"flex items-center gap-1",children:[e.jsx(Ve,{className:"h-3 w-3"}),"Auto-Fixes"]}),e.jsxs(O,{value:"journey",className:"flex items-center gap-1",children:[e.jsx(Qe,{className:"h-3 w-3"}),"User Journeys"]})]}),e.jsx(U,{value:"overview",className:"space-y-4",children:e.jsxs(d,{children:[e.jsxs(f,{children:[e.jsx(b,{children:"All Organizations"}),e.jsx(M,{children:"View and manage all tenant organizations on the platform"})]}),e.jsx(o,{children:h?e.jsx("div",{className:"space-y-2",children:[1,2,3].map(a=>e.jsx(C,{className:"h-12 w-full"},a))}):c.length===0?e.jsx("p",{className:"text-muted-foreground text-center py-8",children:"No organizations yet"}):e.jsxs(K,{children:[e.jsx(Q,{children:e.jsxs(D,{children:[e.jsx(v,{children:"Organization"}),e.jsx(v,{children:"Created"}),e.jsx(v,{children:"Subscription"}),e.jsx(v,{children:"Status"}),e.jsx(v,{className:"text-right",children:"Stores"}),e.jsx(v,{className:"text-right",children:"Users"})]})}),e.jsx(J,{children:c.map(a=>e.jsxs(D,{children:[e.jsx(N,{className:"font-medium",children:a.name}),e.jsx(N,{className:"text-muted-foreground",children:B(new Date(a.created_at),"MMM d, yyyy")}),e.jsx(N,{children:e.jsx(j,{variant:i(a.subscription_tier),children:a.subscription_tier})}),e.jsx(N,{children:e.jsx(j,{variant:s(a.subscription_status),children:a.subscription_status})}),e.jsx(N,{className:"text-right",children:a.store_count}),e.jsx(N,{className:"text-right",children:a.user_count})]},a.id))})]})})]})}),e.jsx(U,{value:"users",className:"space-y-4",children:e.jsxs(d,{children:[e.jsxs(f,{children:[e.jsx(b,{children:"All Users"}),e.jsx(M,{children:"View all users registered on the platform"})]}),e.jsx(o,{children:S?e.jsx("div",{className:"space-y-2",children:[1,2,3].map(a=>e.jsx(C,{className:"h-12 w-full"},a))}):w.length===0?e.jsx("p",{className:"text-muted-foreground text-center py-8",children:"No users yet"}):e.jsxs(K,{children:[e.jsx(Q,{children:e.jsxs(D,{children:[e.jsx(v,{children:"User"}),e.jsx(v,{children:"Email"}),e.jsx(v,{children:"Role"}),e.jsx(v,{children:"Joined"}),e.jsx(v,{className:"text-right",children:"Organizations"})]})}),e.jsx(J,{children:w.map(a=>e.jsxs(D,{children:[e.jsx(N,{className:"font-medium",children:a.full_name||"â"}),e.jsx(N,{className:"text-muted-foreground",children:a.email}),e.jsx(N,{children:e.jsx(j,{variant:"outline",children:a.role})}),e.jsx(N,{className:"text-muted-foreground",children:B(new Date(a.created_at),"MMM d, yyyy")}),e.jsx(N,{className:"text-right",children:a.tenant_count})]},a.id))})]})})]})}),e.jsx(U,{value:"feedback",className:"space-y-4",children:e.jsx(Ge,{})}),e.jsx(U,{value:"autofixes",className:"space-y-4",children:e.jsx(Ye,{})}),e.jsx(U,{value:"journey",className:"space-y-4",children:e.jsx(Ze,{})})]})]})]})}export{xs 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.