1import{y as Ne,z as ce,D as de,E as oe,e as h,au as $,r as g,f as E,K as q,t as x,j as e,C as F,a as D,b as I,ar as me,c as W,d as T,h as w,I as P,N as A,ab as X,ac as Z,ad as ee,ae as se,af as Q,B as v,ak as be,k as M,L as ae,az as Se,aA as _e,aB as K,S as ue,aC as R,u as xe,aU as V,aV as Y,aW as L,aX as S,aY as G,aZ as f,ay as he,am as te,c8 as pe,an as ne,ao as re,ap as ie,aN as we}from"./index-DHwBXtP2.js";import{l as je,a as ke,b as Ce,p as Pe,c as Fe,d as ge,u as Te,e as ve,f as Ee,t as qe,s as De,g as Ie,h as Le,i as Ae,j as Oe,k as Ke}from"./admin.functions-BJnqFSqj.js";import{s as Re}from"./impersonation.functions-DxrFoY7O.js";import{s as We,i as $e}from"./demo.functions-wGdlujU8.js";import{a as ze,u as Me,s as Qe,l as Be,t as He}from"./platform-settings.functions-DD3T-TpA.js";import{S as fe}from"./search-Cl67PjLD.js";import{S as Ue}from"./shield-Dld_081g.js";import{H as Ve}from"./heart-pulse-DSYrgPZG.js";import{T as Ye}from"./timer-DyoLSnLZ.js";import{L as Ge}from"./life-buoy-Dtfyv4sg.js";import{C as Je}from"./chart-column-BRu-MsVd.js";import{T as Xe}from"./trending-up-BjyiYEVM.js";import{L as Ze}from"./layers-BTSX6eIt.js";import{U as es}from"./users-Cd2QSwK4.js";import{C as ss}from"./credit-card-CLqCDBfS.js";import{H as as}from"./history-DdO_7tcu.js";import{S as ts}from"./settings-2-DxqHAQDa.js";import{U as ns}from"./user-check-CFtJ1L1k.js";const rs=[["circle",{cx:"9",cy:"12",r:"3",key:"u3jwor"}],["rect",{width:"20",height:"14",x:"2",y:"5",rx:"7",key:"g7kal2"}]],is=Ne("toggle-left",rs),ls=ce({method:"POST"}).middleware([de]).handler(oe("src_lib_workspace-duplicate_functions_ts--previewWorkspaceDuplicate_createServerFn_handler")),cs=ce({method:"POST"}).middleware([de]).handler(oe("src_lib_workspace-duplicate_functions_ts--duplicateWorkspace_createServerFn_handler")),le=["brand","credentials","offers","pipelines","phone_numbers","ai_callers","ai_emails","outreach","organic","optimization","team"],ds={brand:"Brand & business context",credentials:"Integration credentials",offers:"Offers",pipelines:"Pipelines & stages",phone_numbers:"Phone numbers",ai_callers:"AI callers & voices",ai_emails:"AI email accounts",outreach:"Outreach setup",organic:"Organic setup",optimization:"Optimization & lead settings",team:"Team members & roles"},os={brand:"Branding, business context, objective mapping, lead-form settings",credentials:"Meta, Telnyx, ElevenLabs, Arcads, HeyGen, Stripe, PandaDoc keys",offers:"Offer definitions and their prompts",pipelines:"Pipelines and their stages",phone_numbers:"AI/dialer numbers",ai_callers:"ElevenLabs voices and agent dialer numbers",ai_emails:"Agent mailboxes and sender identity (needs re-auth)",outreach:"Cadences, message/SMS templates, first-touch, DNC",organic:"Personas, organic and Instagram settings",optimization:"Optimization rules, lead scoring, feature overrides",team:"Members, sales agents, heads of strategy"},ms=le.filter(t=>t!=="team");function us(){const t=h(je),n=h(ls),c=h(cs),l=$(),[a,u]=g.useState(""),[d,y]=g.useState(""),[j,r]=g.useState(""),[N,p]=g.useState("source_owner"),[o,k]=g.useState(ms),[C,_]=g.useState(null),m=E({queryKey:["admin-workspaces"],queryFn:()=>t()}),O=E({queryKey:["dup-preview",a],enabled:!!a,queryFn:()=>n({data:{sourceWorkspaceId:a}})}),z=g.useMemo(()=>{const i=m.data?.workspaces??[];if(!d.trim())return i;const s=d.toLowerCase();return i.filter(b=>b.name?.toLowerCase().includes(s)||b.id?.toLowerCase().includes(s)||b.owner?.email?.toLowerCase().includes(s))},[m.data,d]),J=new Map;for(const i of O.data?.sections??[])J.set(i.section,i.rows);const B=q({mutationFn:()=>c({data:{sourceWorkspaceId:a,name:j.trim(),sections:o,ownerMode:N}}),onSuccess:i=>{_(i),x.success(`Duplicated into "${i.name}"`),l.invalidateQueries({queryKey:["admin-workspaces"]}),l.invalidateQueries({queryKey:["workspaces"]})},onError:i=>x.error(i.message)}),H=(i,s)=>k(b=>s?[...new Set([...b,i])]:b.filter(ye=>ye!==i));return e.jsxs(F,{className:"mt-4",children:[e.jsxs(D,{children:[e.jsxs(I,{className:"flex items-center gap-2",children:[e.jsx(me,{className:"h-4 w-4"}),"Duplicate workspace"]}),e.jsx(W,{children:"Copies configuration only â integrations and API keys, phone numbers, AI callers, mailboxes, outreach setup, pipelines, offers and organic setup. Never copies leads, appointments, notes or message history. Everything that can send or spend lands paused."})]}),e.jsxs(T,{className:"space-y-5",children:[e.jsxs("div",{className:"grid gap-4 md:grid-cols-2",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(w,{children:"Source workspace"}),e.jsxs("div",{className:"relative",children:[e.jsx(fe,{className:"absolute left-2 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-muted-foreground"}),e.jsx(P,{value:d,onChange:i=>y(i.target.value),placeholder:"Search workspacesâ¦",className:"h-8 pl-7 text-xs"})]}),m.isLoading?e.jsx(A,{className:"h-9"}):e.jsxs(X,{value:a,onValueChange:u,children:[e.jsx(Z,{children:e.jsx(ee,{placeholder:"Pick a workspace to duplicate"})}),e.jsx(se,{children:z.slice(0,100).map(i=>e.jsxs(Q,{value:i.id,children:[i.name,i.owner?.email?` â ${i.owner.email}`:""]},i.id))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(w,{htmlFor:"dup-name",children:"New workspace name"}),e.jsx(P,{id:"dup-name",value:j,onChange:i=>r(i.target.value),placeholder:"Acme Co. (copy)",maxLength:80}),e.jsx(w,{className:"pt-2 block",children:"Owner"}),e.jsxs(X,{value:N,onValueChange:i=>p(i),children:[e.jsx(Z,{children:e.jsx(ee,{})}),e.jsxs(se,{children:[e.jsx(Q,{value:"source_owner",children:"Same owner as source workspace"}),e.jsx(Q,{value:"me",children:"Me (platform admin)"})]})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(w,{children:"What to copy"}),e.jsx(v,{size:"sm",variant:"ghost",className:"h-6 text-xs",onClick:()=>k([...le]),children:"Select all"}),e.jsx(v,{size:"sm",variant:"ghost",className:"h-6 text-xs",onClick:()=>k([]),children:"Clear"})]}),e.jsx("div",{className:"grid gap-2 md:grid-cols-2",children:le.map(i=>e.jsxs("label",{className:"flex items-start gap-3 rounded-md border p-3 cursor-pointer hover:bg-muted/40",children:[e.jsx(be,{checked:o.includes(i),onCheckedChange:s=>H(i,!!s),className:"mt-0.5"}),e.jsxs("span",{className:"min-w-0",children:[e.jsxs("span",{className:"flex items-center gap-2 text-sm font-medium",children:[ds[i],a&&!O.isLoading?e.jsxs(M,{variant:"secondary",className:"text-[10px]",children:[J.get(i)??0," rows"]}):null]}),e.jsx("span",{className:"block text-xs text-muted-foreground",children:os[i]})]})]},i))}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"OAuth connections (Google, Meta, Instagram, agent mailboxes) are copied as configuration but must be re-authorized in the new workspace â their tokens are tied to the original account."})]}),e.jsx(v,{onClick:()=>B.mutate(),disabled:!a||!j.trim()||o.length===0||B.isPending,children:B.isPending?"Duplicatingâ¦":"Duplicate workspace"}),C?e.jsxs("div",{className:"rounded-md border p-3 space-y-2",children:[e.jsxs("div",{className:"text-sm font-medium",children:["Created â",C.name,"â ",e.jsxs("span",{className:"text-xs text-muted-foreground",children:["(",C.workspaceId,")"]})]}),e.jsx("ul",{className:"space-y-1 text-xs",children:C.results.map(i=>e.jsxs("li",{className:"flex flex-wrap items-center gap-2",children:[e.jsx("span",{className:"font-medium",children:i.label}),e.jsxs(M,{variant:"secondary",children:[i.copied," copied"]}),i.errors.length>0?e.jsx("span",{className:"text-destructive",children:i.errors.join(" · ")}):null]},i.section))})]}):null]})]})}const U=["trial","starter","pro","agency","internal"];function Bs(){const t=h(ke),{data:n,isLoading:c}=E({queryKey:["am-i-admin"],queryFn:()=>t()});return c?e.jsx("div",{className:"p-8",children:e.jsx(A,{className:"h-64"})}):n?.isAdmin?e.jsxs("div",{className:"container mx-auto px-4 py-6 space-y-6",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ue,{className:"h-6 w-6 text-primary"}),e.jsx("h1",{className:"text-2xl font-semibold",children:"Platform Admin"}),e.jsx(M,{variant:"secondary",children:"Super-admin"}),e.jsxs("div",{className:"ml-auto flex items-center gap-2",children:[e.jsx(v,{asChild:!0,size:"sm",variant:"outline",children:e.jsxs(ae,{to:"/admin/jobs",children:[e.jsx(Ve,{className:"h-4 w-4 mr-1"}
1),"Background jobs"]})}),e.jsx(v,{asChild:!0,size:"sm",variant:"outline",children:e.jsxs(ae,{to:"/admin/latency",children:[e.jsx(Ye,{className:"h-4 w-4 mr-1"}),"Save speed"]})}),e.jsx(v,{asChild:!0,size:"sm",variant:"outline",children:e.jsxs(ae,{to:"/admin/support",children:[e.jsx(Ge,{className:"h-4 w-4 mr-1"}),"Support inbox"]})})]})]}),e.jsxs(Se,{defaultValue:"stats",className:"w-full",children:[e.jsxs(_e,{children:[e.jsxs(K,{value:"stats",children:[e.jsx(Je,{className:"h-4 w-4 mr-1"}),"Stats"]}),e.jsxs(K,{value:"revenue",children:[e.jsx(Xe,{className:"h-4 w-4 mr-1"}),"Revenue"]}),e.jsxs(K,{value:"plans",children:[e.jsx(Ze,{className:"h-4 w-4 mr-1"}),"Plans"]}),e.jsxs(K,{value:"features",children:[e.jsx(is,{className:"h-4 w-4 mr-1"}),"Feature matrix"]}),e.jsxs(K,{value:"workspaces",children:[e.jsx(es,{className:"h-4 w-4 mr-1"}),"Workspaces"]}),e.jsxs(K,{value:"stripe",children:[e.jsx(ss,{className:"h-4 w-4 mr-1"}),"Stripe"]}),e.jsxs(K,{value:"audit",children:[e.jsx(as,{className:"h-4 w-4 mr-1"}),"Audit log"]}),e.jsxs(K,{value:"duplicate",children:[e.jsx(me,{className:"h-4 w-4 mr-1"}),"Duplicate"]}),e.jsxs(K,{value:"demo",children:[e.jsx(ue,{className:"h-4 w-4 mr-1"}),"Demo mode"]})]}),e.jsx(R,{value:"stats",children:e.jsx(hs,{})}),e.jsx(R,{value:"revenue",children:e.jsx(_s,{})}),e.jsx(R,{value:"plans",children:e.jsx(ps,{})}),e.jsx(R,{value:"features",children:e.jsx(gs,{})}),e.jsx(R,{value:"workspaces",children:e.jsx(vs,{})}),e.jsx(R,{value:"stripe",children:e.jsx(Ns,{})}),e.jsx(R,{value:"audit",children:e.jsx(Ss,{})}),e.jsx(R,{value:"duplicate",children:e.jsx(us,{})}),e.jsx(R,{value:"demo",children:e.jsx(ks,{})})]})]}):e.jsx(xs,{})}function xs(){const t=xe(),n=$(),c=h(Ce),l=q({mutationFn:()=>c(),onSuccess:a=>{a.alreadyAdmin?x.success("You are already a platform admin."):x.success("You are now the platform admin."),n.invalidateQueries({queryKey:["am-i-admin"]})},onError:a=>x.error(a.message)});return e.jsx("div",{className:"container mx-auto px-4 py-12 max-w-lg",children:e.jsxs(F,{children:[e.jsxs(D,{children:[e.jsx(I,{children:"Claim platform admin"}),e.jsx(W,{children:"The first authenticated user to claim this becomes the super-admin of the platform. This is a one-time action â once a platform admin exists, this gate is permanently locked."})]}),e.jsxs(T,{className:"flex gap-2",children:[e.jsx(v,{onClick:()=>l.mutate(),disabled:l.isPending,children:l.isPending?"Claiming...":"Claim platform admin"}),e.jsx(v,{variant:"ghost",onClick:()=>t({to:"/dashboard"}),children:"Cancel"})]})]})})}function hs(){const t=h(Pe),{data:n,isLoading:c}=E({queryKey:["admin-stats"],queryFn:()=>t()});if(c||!n)return e.jsx(A,{className:"h-48"});const l=[{label:"Workspaces",value:n.workspaceCount},{label:"Users",value:n.userCount},{label:"Active subscriptions",value:n.activeSubscriptions},{label:"Platform AI spend (30d)",value:`$${n.aiSpend30d.toFixed(2)}`}];return e.jsxs("div",{className:"space-y-4 pt-4",children:[e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-4",children:l.map(a=>e.jsxs(F,{children:[e.jsx(D,{className:"pb-2",children:e.jsx(W,{children:a.label})}),e.jsx(T,{children:e.jsx("div",{className:"text-2xl font-semibold",children:a.value})})]},a.label))}),e.jsxs(F,{children:[e.jsx(D,{children:e.jsx(I,{children:"By plan"})}),e.jsx(T,{children:e.jsx("div",{className:"flex gap-4 flex-wrap",children:U.map(a=>e.jsxs("div",{className:"px-4 py-3 rounded-md border bg-muted/30 min-w-[120px]",children:[e.jsx("div",{className:"text-xs uppercase text-muted-foreground",children:a}),e.jsx("div",{className:"text-xl font-semibold",children:n.byPlan[a]??0})]},a))})})]})]})}function ps(){const t=h(ge),n=h(Te),c=$(),{data:l,isLoading:a}=E({queryKey:["admin-plans"],queryFn:()=>t()}),u=q({mutationFn:d=>n({data:d}),onSuccess:()=>{x.success("Plan saved"),c.invalidateQueries({queryKey:["admin-plans"]})},onError:d=>x.error(d.message)});return a||!l?e.jsx(A,{className:"h-48"}):e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4 pt-4",children:l.plans.map(d=>e.jsx(js,{plan:d,onSave:y=>u.mutate({code:d.code,...y})},d.code))})}function js({plan:t,onSave:n}){const[c,l]=g.useState(t.name),[a,u]=g.useState(t.description??""),[d,y]=g.useState(Number(t.price_usd_monthly??0)),[j,r]=g.useState(t.seat_limit?.toString()??""),[N,p]=g.useState(!!t.is_public);return e.jsxs(F,{children:[e.jsx(D,{children:e.jsxs(I,{className:"flex items-center gap-2",children:[t.name,e.jsx(M,{variant:"outline",children:t.code})]})}),e.jsxs(T,{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx(w,{children:"Name"}),e.jsx(P,{value:c,onChange:o=>l(o.target.value)})]}),e.jsxs("div",{children:[e.jsx(w,{children:"Description"}),e.jsx(P,{value:a,onChange:o=>u(o.target.value)})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(w,{children:"Price / month (USD)"}),e.jsx(P,{type:"number",value:d,onChange:o=>y(Number(o.target.value))})]}),e.jsxs("div",{children:[e.jsx(w,{children:"Seat limit (blank = unlimited)"}),e.jsx(P,{value:j,onChange:o=>r(o.target.value)})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(he,{checked:N,onCheckedChange:p}),e.jsx(w,{children:"Public (listed on pricing page)"})]}),e.jsx(v,{size:"sm",onClick:()=>n({name:c,description:a,price_usd_monthly:d,seat_limit:j===""?null:Number(j),is_public:N}),children:"Save"})]})]})}function gs(){const t=h(ve),n=h(Ee),c=h(qe),l=$(),a=E({queryKey:["admin-features"],queryFn:()=>t()}),u=E({queryKey:["admin-plan-features"],queryFn:()=>n()}),d=q({mutationFn:r=>c({data:r}),onSuccess:()=>l.invalidateQueries({queryKey:["admin-plan-features"]}),onError:r=>x.error(r.message)});if(a.isLoading||u.isLoading)return e.jsx(A,{className:"h-96"});const y=new Map;for(const r of u.data?.rows??[])y.set(`${r.plan_code}:${r.feature_code}`,r.enabled);const j=new Map;for(const r of a.data?.features??[])j.has(r.category)||j.set(r.category,[]),j.get(r.category).push(r);return e.jsx("div",{className:"pt-4 space-y-6",children:[...j.entries()].map(([r,N])=>e.jsxs(F,{children:[e.jsx(D,{children:e.jsx(I,{className:"capitalize",children:r})}),e.jsx(T,{className:"overflow-x-auto",children:e.jsxs(V,{children:[e.jsx(Y,{children:e.jsxs(L,{children:[e.jsx(S,{children:"Feature"}),U.map(p=>e.jsx(S,{className:"text-center capitalize",children:p},p))]})}),e.jsx(G,{children:N.map(p=>e.jsxs(L,{children:[e.jsxs(f,{children:[e.jsx("div",{className:"font-medium",children:p.name}),e.jsx("div",{className:"text-xs text-muted-foreground",children:p.code})]}),U.map(o=>{const k=y.get(`${o}:${p.code}`)??!1;return e.jsx(f,{className:"text-center",children:e.jsx(he,{checked:k,onCheckedChange:C=>d.mutate({plan_code:o,feature_code:p.code,enabled:C})})},o)})]},p.code))})]})})]},r))})}function vs(){const t=h(je),n=h(De),c=h(Ie),l=h(Le),a=h(Ae),u=$(),{data:d,isLoading:y}=E({queryKey:["admin-workspaces"],queryFn:()=>t()}),[j,r]=g.useState(""),[N,p]=g.useState(!1),[o,k]=g.useState(""),[C,_]=g.useState(null),[m,O]=g.useState(""),z=q({mutationFn:s=>a({data:s}),onSuccess:s=>{x.success(`Created "${s?.name??"workspace"}
1"`),p(!1),k(""),u.invalidateQueries({queryKey:["admin-workspaces"]}),u.invalidateQueries({queryKey:["workspaces"]})},onError:s=>x.error(s.message)}),J=q({mutationFn:s=>n({data:s}),onSuccess:()=>{x.success("Plan updated"),u.invalidateQueries({queryKey:["admin-workspaces"]})},onError:s=>x.error(s.message)}),B=q({mutationFn:s=>c({data:s}),onSuccess:s=>{const b=s?.expires_at;x.success(b?`Internal plan granted â expires ${new Date(b).toLocaleDateString()}`:"Internal plan granted"),u.invalidateQueries({queryKey:["admin-workspaces"]})},onError:s=>x.error(s.message)}),H=q({mutationFn:s=>l({data:s}),onSuccess:s=>{x.success(`Deleted "${s?.name??"workspace"}"`),_(null),O(""),u.invalidateQueries({queryKey:["admin-workspaces"]})},onError:s=>x.error(s.message)});if(y||!d)return e.jsx(A,{className:"h-96"});const i=d.workspaces.filter(s=>{if(!j.trim())return!0;const b=j.toLowerCase();return s.name?.toLowerCase().includes(b)||s.id?.toLowerCase().includes(b)||s.owner?.email?.toLowerCase().includes(b)||s.owner?.display_name?.toLowerCase().includes(b)});return e.jsx(F,{className:"mt-4",children:e.jsxs(T,{className:"pt-6",children:[e.jsxs("div",{className:"mb-3 flex items-center gap-2",children:[e.jsxs("div",{className:"relative max-w-sm flex-1",children:[e.jsx(fe,{className:"absolute left-2 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-muted-foreground"}),e.jsx(P,{value:j,onChange:s=>r(s.target.value),placeholder:"Search by name, owner email, or IDâ¦",className:"h-8 pl-7 text-xs"})]}),e.jsxs("div",{className:"text-xs text-muted-foreground",children:[i.length," of ",d.workspaces.length]}),e.jsxs(te,{open:N,onOpenChange:p,children:[e.jsx(pe,{asChild:!0,children:e.jsx(v,{size:"sm",className:"h-8",children:"New workspace"})}),e.jsxs(ne,{children:[e.jsx(re,{children:e.jsx(ie,{children:"Create workspace"})}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(w,{htmlFor:"new-ws-name",children:"Name"}),e.jsx(P,{id:"new-ws-name",value:o,onChange:s=>k(s.target.value),placeholder:"Acme Co.",maxLength:80,autoFocus:!0}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"You will be the owner. You can invite teammates afterwards from workspace settings."})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[e.jsx(v,{variant:"outline",onClick:()=>p(!1),disabled:z.isPending,children:"Cancel"}),e.jsx(v,{onClick:()=>z.mutate({name:o.trim()}),disabled:z.isPending||o.trim().length===0,children:z.isPending?"Creatingâ¦":"Create"})]})]})]})]}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(V,{children:[e.jsx(Y,{children:e.jsxs(L,{children:[e.jsx(S,{children:"Workspace"}),e.jsx(S,{children:"Owner"}),e.jsx(S,{children:"Members"}),e.jsx(S,{children:"Plan"}),e.jsx(S,{children:"Status"}),e.jsx(S,{children:"Trial / Renews"}),e.jsx(S,{children:"Created"}),e.jsx(S,{className:"text-right",children:"Actions"})]})}),e.jsx(G,{children:i.map(s=>e.jsxs(L,{children:[e.jsxs(f,{className:"align-top",children:[e.jsx("div",{className:"font-medium",children:s.name}),e.jsxs("div",{className:"text-[11px] text-muted-foreground font-mono",children:[s.id.slice(0,8),"â¦"]})]}),e.jsx(f,{className:"align-top",children:s.owner?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"text-xs font-medium",children:s.owner.display_name??"â"}),e.jsx("div",{className:"text-[11px] text-muted-foreground",children:s.owner.email??"â"})]}):e.jsx("span",{className:"text-xs text-muted-foreground",children:"â"})}),e.jsx(f,{className:"align-top",children:s.member_count}),e.jsx(f,{className:"align-top",children:e.jsxs(X,{value:s.subscription?.plan_code??"trial",onValueChange:b=>J.mutate({workspace_id:s.id,plan_code:b}),children:[e.jsx(Z,{className:"h-7 w-[110px] text-xs",children:e.jsx(ee,{})}),e.jsx(se,{children:U.map(b=>e.jsx(Q,{value:b,children:b},b))})]})}),e.jsx(f,{className:"align-top",children:e.jsx(M,{variant:s.subscription?.status==="active"?"default":"secondary",children:s.subscription?.status??"â"})}),e.jsx(f,{className:"align-top text-xs text-muted-foreground",children:s.subscription?.current_period_end?new Date(s.subscription.current_period_end).toLocaleDateString():s.subscription?.trial_ends_at?new Date(s.subscription.tri
1al_ends_at).toLocaleDateString():"â"}),e.jsx(f,{className:"align-top text-xs text-muted-foreground",children:new Date(s.created_at).toLocaleDateString()}),e.jsx(f,{className:"align-top",children:e.jsxs("div",{className:"flex flex-wrap items-center justify-end gap-1.5",children:[e.jsx(fs,{workspaceId:s.id,workspaceName:s.name}),e.jsx(ws,{workspaceId:s.id,workspaceName:s.name}),e.jsx(v,{size:"sm",variant:"outline",disabled:B.isPending,onClick:()=>{confirm(`Grant Internal plan (free, 365 days, Agency features) to ${s.name}?`)&&B.mutate({workspace_id:s.id})},children:"Internal"}),e.jsx(v,{size:"sm",variant:"destructive",onClick:()=>{_({id:s.id,name:s.name}),O("")},children:e.jsx(we,{className:"h-3 w-3"})})]})})]},s.id))})]})}),e.jsx(te,{open:!!C,onOpenChange:s=>{s||(_(null),O(""))},children:e.jsxs(ne,{children:[e.jsx(re,{children:e.jsx(ie,{className:"text-destructive",children:"Delete workspace"})}),C?e.jsxs("div",{className:"space-y-3 text-sm",children:[e.jsxs("p",{children:["This permanently deletes ",e.jsx("span",{className:"font-semibold",children:C.name})," and every associated lead, campaign, creative, and subscription. This cannot be undone."]}),e.jsxs("div",{className:"space-y-1",children:[e.jsxs(w,{className:"text-xs",children:["Type ",e.jsx("span",{className:"font-mono",children:C.name})," to confirm"]}),e.jsx(P,{value:m,onChange:s=>O(s.target.value),placeholder:C.name})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-1",children:[e.jsx(v,{variant:"outline",onClick:()=>{_(null),O("")},children:"Cancel"}),e.jsx(v,{variant:"destructive",disabled:H.isPending||m.trim()!==C.name.trim(),onClick:()=>H.mutate({workspace_id:C.id,confirm_name:m}),children:H.isPending?"Deletingâ¦":"Delete forever"})]})]}):null]})})]})})}function fs({workspaceId:t,workspaceName:n}){const[c,l]=g.useState(!1);return e.jsxs(te,{open:c,onOpenChange:l,children:[e.jsx(pe,{asChild:!0,children:e.jsxs(v,{size:"sm",variant:"outline",children:[e.jsx(ts,{className:"h-3 w-3 mr-1"}),"Features"]})}),e.jsxs(ne,{className:"max-w-2xl max-h-[80vh] overflow-y-auto",children:[e.jsx(re,{children:e.jsxs(ie,{children:["Feature overrides â ",n]})}),c?e.jsx(ys,{workspaceId:t}):null]})]})}function ys({workspaceId:t}){const n=h(ve),c=h(Oe),l=h(Ke),a=$(),u=E({queryKey:["admin-features"],queryFn:()=>n()}),d=E({queryKey:["admin-ws-overrides",t],queryFn:()=>c({data:{workspace_id:t}})}),y=q({mutationFn:r=>l({data:{workspace_id:t,...r}}),onSuccess:()=>{a.invalidateQueries({queryKey:["admin-ws-overrides",t]})},onError:r=>x.error(r.message)});if(u.isLoading||d.isLoading)return e.jsx(A,{className:"h-64"});const j=new Map((d.data?.overrides??[]).map(r=>[r.feature_code,r.enabled]));return e.jsxs("div",{className:"space-y-3",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:`Overrides take precedence over the workspace's plan. "Plan default" removes the override.`}),e.jsxs(V,{children:[e.jsx(Y,{children:e.jsxs(L,{children:[e.jsx(S,{children:"Feature"}),e.jsx(S,{className:"text-right",children:"State"})]})}),e.jsx(G,{children:(u.data?.features??[]).map(r=>{const N=j.has(r.code)?j.get(r.code)?"on":"off":"default";return e.jsxs(L,{children:[e.jsxs(f,{children:[e.jsx("div",{className:"font-medium text-sm",children:r.name}),e.jsx("div",{className:"text-xs text-muted-foreground",children:r.code})]}),e.jsx(f,{className:"text-right",children:e.jsxs(X,{value:N,onValueChange:p=>y.mutate({feature_code:r.code,enabled:p==="default"?null:p==="on"}),children:[e.jsx(Z,{className:"w-[140px] ml-auto",children:e.jsx(ee,{})}),e.jsxs(se,{children:[e.jsx(Q,{value:"default",children:"Plan default"}),e.jsx(Q,{value:"on",children:"Force on"}),e.jsx(Q,{value:"off",children:"Force off"})]})]})})]},r.code)})})]})]})}function Ns(){const t=h(ze),n=h(Me),c=h(Qe),l=h(ge),a=$(),u=E({queryKey:["platform-settings"],queryFn:()=>t()}),d=E({queryKey:["admin-plans"],queryFn:()=>l()}),[y,j]=g.useState(""),[r,N]=g.useState(""),[p,o]=g.useState(""),k=q({mutationFn:m=>n({data:m}),onSuccess:()=>{x.success("Stripe settings saved"),a.invalidateQueries({queryKey:["platform-settings"]})},onError:m=>x.error(m.message)}
1),C=q({mutationFn:m=>c({data:m}),onSuccess:()=>{x.success("Price ID saved"),a.invalidateQueries({queryKey:["admin-plans"]})},onError:m=>x.error(m.message)});if(u.isLoading||!u.data)return e.jsx(A,{className:"h-64"});const _=u.data;return e.jsxs("div",{className:"pt-4 space-y-4",children:[e.jsxs(F,{children:[e.jsxs(D,{children:[e.jsx(I,{children:"Free trials"}),e.jsx(W,{children:"When OFF, new workspaces are created without trial access â users must subscribe to a paid plan before they can use the app."})]}),e.jsxs(T,{className:"flex items-center justify-between gap-4",children:[e.jsxs("div",{className:"text-sm",children:["Currently:"," ",e.jsx("span",{className:_.trials_enabled?"text-emerald-500 font-medium":"text-amber-500 font-medium",children:_.trials_enabled?"Enabled (14-day free trial)":"Disabled (pay to activate)"})]}),e.jsx(v,{variant:_.trials_enabled?"destructive":"default",disabled:k.isPending,onClick:()=>k.mutate({trials_enabled:!_.trials_enabled}),children:_.trials_enabled?"Disable trials":"Enable trials"})]})]}),e.jsxs(F,{children:[e.jsxs(D,{children:[e.jsx(I,{children:"Stripe credentials"}),e.jsxs(W,{children:["Secret key and webhook signing secret are stored as server-side environment secrets (",e.jsx("code",{children:"STRIPE_SECRET_KEY"}),","," ",e.jsx("code",{children:"STRIPE_WEBHOOK_SECRET"}),") â never in the database. Edit them in Project Settings â Secrets. Non-secret fields below are saved in the database.",e.jsx("br",{}),"Webhook endpoint: ",e.jsx("code",{children:"/api/public/hooks/stripe-webhook"})]})]}),e.jsxs(T,{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsxs(w,{children:["Secret key"," ",e.jsxs("span",{className:"text-xs text-muted-foreground",children:["(env: ",_.has_secret_key?_.stripe_secret_key_masked:"not set",")"]})]}),e.jsx(P,{type:"password",placeholder:"sk_live_⦠(paste into Project Settings â Secrets as STRIPE_SECRET_KEY)",disabled:!0,readOnly:!0}),e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:["Managed via the ",e.jsx("code",{children:"STRIPE_SECRET_KEY"})," server secret. Add or edit in Project Settings â Secrets."]})]}),e.jsxs("div",{children:[e.jsx(w,{children:"Publishable key"}),e.jsx(P,{placeholder:"pk_live_â¦",defaultValue:_.stripe_publishable_key,onChange:m=>j(m.target.value)})]}),e.jsxs("div",{children:[e.jsxs(w,{children:["Webhook signing secret"," ",e.jsxs("span",{className:"text-xs text-muted-foreground",children:["(env: ",_.has_webhook_secret?_.stripe_webhook_secret_masked:"not set",")"]})]}),e.jsx(P,{type:"password",placeholder:"whsec_⦠(paste into Project Settings â Secrets as STRIPE_WEBHOOK_SECRET)",disabled:!0,readOnly:!0}),e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:["Managed via the ",e.jsx("code",{children:"STRIPE_WEBHOOK_SECRET"})," server secret. Add or edit in Project Settings â Secrets."]})]}),e.jsxs("div",{children:[e.jsx(w,{children:"App base URL (used in checkout success/cancel)"}),e.jsx(P,{placeholder:"https://trybrain.io",defaultValue:_.app_base_url,onChange:m=>N(m.target.value)})]}),e.jsxs("div",{children:[e.jsx(w,{children:"Billing portal return URL (optional)"}),e.jsx(P,{placeholder:"https://trybrain.io/settings/billing",defaultValue:_.stripe_billing_portal_return_url,onChange:m=>o(m.target.value)})]}),e.jsx(v,{onClick:()=>{const m={};if(y&&(m.stripe_publishable_key=y),r&&(m.app_base_url=r),p&&(m.stripe_billing_portal_return_url=p),Object.keys(m).length===0){x.message("Nothing changed");return}k.mutate(m)},disabled:k.isPending,children:k.isPending?"Savingâ¦":"Save"})]})]}),e.jsxs(F,{children:[e.jsxs(D,{children:[e.jsx(I,{children:"Stripe price IDs per plan"}),e.jsx(W,{children:"Paste the monthly and yearly recurring price IDs from Stripe for each plan."})]}),e.jsx(T,{className:"space-y-3",children:(d.data?.plans??[]).map(m=>e.jsx(bs,{plan:m,onSave:(O,z)=>C.mutate({code:m.code,stripe_price_id_monthly:O,stripe_price_id_yearly:z})},m.code))})]})]})}function bs({plan:t,onSave:n}){const[c,l]=g.useState(t.stripe_price_id_monthly??""),[a,u]=g.useState(t.stripe_price_id_yearly??""),d=h(He),[y,j]=g.useState(null),r=async N=>{const p=N==="m"?c:a;if(!p){x.error("Paste a price ID first");return}j(N);try{const o=await d({data:{price_id:p.trim()}});if(!o.ok)x.error(o.error??"Stripe rejected this price ID");
1else{const k=o.interval_count&&o.interval_count>1?`${o.interval_count} `:"";x.success(`â ${o.currency} ${o.amount} every ${k}${o.interval}${o.active?"":" (inactive)"}`)}}finally{j(null)}};return e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-[1fr_1fr_auto] gap-2 items-end border-b pb-3",children:[e.jsxs("div",{children:[e.jsxs(w,{className:"capitalize",children:[t.name," â monthly"]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(P,{placeholder:"price_⦠(monthly)",value:c,onChange:N=>l(N.target.value)}),e.jsx(v,{size:"sm",variant:"outline",disabled:y!==null,onClick:()=>r("m"),children:y==="m"?"Testingâ¦":"Test"})]})]}),e.jsxs("div",{children:[e.jsxs(w,{className:"capitalize",children:[t.name," â yearly"]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(P,{placeholder:"price_⦠(yearly)",value:a,onChange:N=>u(N.target.value)}),e.jsx(v,{size:"sm",variant:"outline",disabled:y!==null,onClick:()=>r("y"),children:y==="y"?"Testingâ¦":"Test"})]})]}),e.jsx(v,{size:"sm",onClick:()=>n(c||null,a||null),children:"Save"})]})}function Ss(){const t=h(Be),{data:n,isLoading:c}=E({queryKey:["admin-audit"],queryFn:()=>t()});return c||!n?e.jsx(A,{className:"h-64"}):e.jsx(F,{className:"mt-4",children:e.jsx(T,{className:"overflow-x-auto pt-6",children:e.jsxs(V,{children:[e.jsx(Y,{children:e.jsxs(L,{children:[e.jsx(S,{children:"When"}),e.jsx(S,{children:"Workspace"}),e.jsx(S,{children:"Kind"}),e.jsx(S,{children:"Payload"})]})}),e.jsx(G,{children:n.events.map(l=>e.jsxs(L,{children:[e.jsx(f,{className:"text-xs text-muted-foreground whitespace-nowrap",children:new Date(l.occurred_at).toLocaleString()}),e.jsx(f,{className:"text-xs",children:l.workspace_name}),e.jsx(f,{children:e.jsx(M,{variant:"outline",children:l.kind})}),e.jsx(f,{className:"text-xs font-mono max-w-md truncate",children:JSON.stringify(l.payload)})]},l.id))})]})})})}function _s(){const t=h(Fe),{data:n,isLoading:c}=E({queryKey:["admin-analytics"],queryFn:()=>t()});if(c||!n)return e.jsx(A,{className:"h-64"});const l=[{label:"MRR",value:`$${n.mrr.toFixed(2)}`},{label:"ARR",value:`$${n.arr.toFixed(0)}`},{label:"Trialing",value:n.trialingCount},{label:"Churned (30d)",value:n.churned30d}];return e.jsxs("div",{className:"space-y-4 pt-4",children:[e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-4",children:l.map(a=>e.jsxs(F,{children:[e.jsx(D,{className:"pb-2",children:e.jsx(W,{children:a.label})}),e.jsx(T,{children:e.jsx("div",{className:"text-2xl font-semibold",children:a.value})})]},a.label))}),e.jsxs(F,{children:[e.jsx(D,{children:e.jsx(I,{children:"MRR by plan"})}),e.jsx(T,{children:e.jsx("div",{className:"flex gap-4 flex-wrap",children:U.map(a=>e.jsxs("div",{className:"px-4 py-3 rounded-md border bg-muted/30 min-w-[140px]",children:[e.jsx("div",{className:"text-xs uppercase text-muted-foreground",children:a}),e.jsxs("div",{className:"text-xl font-semibold",children:["$",(n.mrrByPlan[a]??0).toFixed(2)]})]},a))})})]}),e.jsxs(F,{children:[e.jsxs(D,{children:[e.jsx(I,{children:"Top spenders this month"}),e.jsx(W,{children:"AI Gateway + vendor (Arcads, Firecrawl, image gen) costs."})]}),e.jsx(T,{className:"overflow-x-auto",children:e.jsxs(V,{children:[e.jsx(Y,{children:e.jsxs(L,{children:[e.jsx(S,{children:"Workspace"}),e.jsx(S,{className:"text-right",children:"AI"}),e.jsx(S,{className:"text-right",children:"Vendor"}),e.jsx(S,{className:"text-right",children:"Total"})]})}),e.jsxs(G,{children:[n.topSpenders.map(a=>e.jsxs(L,{children:[e.jsx(f,{className:"font-medium",children:a.name}),e.jsxs(f,{className:"text-right",children:["$",a.ai_usd.toFixed(2)]}),e.jsxs(f,{className:"text-right",children:["$",a.vendor_usd.toFixed(2)]}),e.jsxs(f,{className:"text-right font-semibold",children:["$",a.total_usd.toFixed(2)]})]},a.workspace_id)),n.topSpenders.length===0?e.jsx(L,{children:e.jsx(f,{colSpan:4,className:"text-sm text-muted-foreground text-center",children:"No spend yet this month."})}):null]})]})})]})]})}function ws({workspaceId:t,workspaceName:n}){const c=xe(),l=$(),a=h(Re),u=q({mutationFn:()=>a({data:{workspace_id:t}}),onSuccess:()=>{x.success(`Impersonating ${n}`);try{window.localStorage.setItem("active_workspace_id",t)}catch{}l.invalidateQueries({queryKey:["workspaces"]}
1),l.invalidateQueries({queryKey:["impersonations"]}),c({to:"/dashboard"})},onError:d=>x.error(d.message)});return e.jsxs(v,{size:"sm",variant:"ghost",className:"ml-2",onClick:()=>u.mutate(),disabled:u.isPending,children:[e.jsx(ns,{className:"h-3 w-3 mr-1"}),"Impersonate"]})}function ks(){const t=$(),n=h(We),c=h($e),{data:l,isLoading:a}=E({queryKey:["demo-enabled"],queryFn:()=>c()}),u=q({mutationFn:()=>n(),onSuccess:d=>{d.reused?x.info("Demo template already exists â reused."):x.success("Demo template workspace created and activated."),t.invalidateQueries({queryKey:["demo-enabled"]})},onError:d=>x.error(d.message)});return e.jsxs(F,{children:[e.jsxs(D,{children:[e.jsxs(I,{className:"flex items-center gap-2",children:[e.jsx(ue,{className:"h-5 w-5"})," Public demo mode"]}),e.jsxs(W,{children:["Visitors capture their email on ",e.jsx("code",{children:"/demo"})," and get a 24h sandbox workspace seeded with fictional Acme Fitness data. External side effects (Meta API, email, SMS, billing) are blocked in demo workspaces."]})]}),e.jsxs(T,{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx("span",{className:"text-muted-foreground",children:"Status:"}),a?e.jsx(A,{className:"h-5 w-20"}):l?.enabled?e.jsx(M,{variant:"default",children:"Live â accepting demo signups"}):e.jsx(M,{variant:"secondary",children:"Not yet configured"})]}),e.jsxs("div",{className:"rounded-md border p-4 bg-muted/30",children:[e.jsx("div",{className:"font-medium mb-1",children:"Set up the Acme Fitness template"}),e.jsx("p",{className:"text-sm text-muted-foreground mb-3",children:"One-time setup. Creates a hidden template workspace, seeds it with sample offers / ad copy / hooks / CTAs, and enables public demo signups. Idempotent â safe to click multiple times."}),e.jsx(v,{onClick:()=>u.mutate(),disabled:u.isPending,children:u.isPending?"Setting upâ¦":"Set up demo template"})]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Public signup form lives at ",e.jsx("code",{children:"/demo"}),". Demo session length (default 24h) can be tuned in the database via"," ",e.jsx("code",{children:"platform_settings.demo_session_hours"}),"."]})]})]})}export{Bs as component};
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.