1import{r as g,u as I,d as $,j as e,e as De}from"./vendor-query-CnAhr2B8.js";import{g as te,a as je,u as Ee,a_ as z,H as T,ak as j,B as c,I as P,p as v,q as B,r as K,aT as _,P as fe,s as N,aG as $e,aU as Te,ab as Ie,F as Ue,d as L,i as Ye,aB as Oe,J as Re,j as qe,k as ze,l as Be,N as Ke,aC as C,O as _e,Q as Qe,R as He,T as Ve,V as F,m as Ge,b2 as me,K as Je,a8 as We,bf as Ze,ae as ue,b0 as Xe}from"./index-CLUtDhnY.js";import{S as es}from"./separator-CVR53GEz.js";import{E as ee}from"./external-link-Ck-FWxuF.js";import{T as se}from"./tag-Bc53W9r2.js";import{I as ss}from"./info-CEGee0l4.js";import"./vendor-ui-CYA7SV0W.js";import"./vendor-charts-CPMWc2oJ.js";import"./vendor-forms-UY-il7MQ.js";function m(r){if(r==null)return 0;const i=typeof r=="number"?r:Number(r);return Number.isFinite(i)?i:0}function ts({businessId:r}){var oe,ce,de;const{user:i}=te(),{toast:o}=je(),[,l]=Ee(),[x,u]=g.useState(null),[n,S]=g.useState("monthly"),[f,b]=g.useState(""),[A,d]=g.useState(null),[y,h]=g.useState(null),[Q,a]=g.useState(!1),{data:M=[],isLoading:G}=I({queryKey:["/api/subscription/plans"],enabled:!!i}),{data:t,isLoading:be}=I({queryKey:[`/api/subscription/status/${r}`],enabled:!!r}),ae=$({mutationFn:async s=>await(await T("POST","/api/subscription/create-subscription",{businessId:r,planId:s})).json(),onSuccess:s=>{if(z.invalidateQueries({queryKey:["/api/subscription/status",r]}),s.status==="trialing"&&!s.clientSecret){o({title:"Plan selected!",description:`You're on the ${s.planName||""} plan. Your 14-day free trial is active.`}),l("/dashboard");return}s.clientSecret&&(o({title:"Subscription created",description:"You will be redirected to complete payment"}),l("/payment?clientSecret="+s.clientSecret))},onError:s=>{o({title:"Failed to create subscription",description:s.message,variant:"destructive"})}}),R=$({mutationFn:async s=>await(await T("POST",`/api/subscription/cancel/${r}`,s)).json(),onSuccess:()=>{o({title:"Subscription cancelled",description:"You won't be charged again. You'll keep access until the end of the current billing period."}),z.invalidateQueries({queryKey:["/api/subscription/status",r]}),Y(!1),Z(1)},onError:s=>{o({title:"Failed to cancel subscription",description:s.message,variant:"destructive"})}}),ne=$({mutationFn:async()=>await(await T("POST",`/api/subscription/resume/${r}`)).json(),onSuccess:()=>{o({title:"Subscription resumed",description:"Your subscription has been resumed"}),z.invalidateQueries({queryKey:["/api/subscription/status",r]})},onError:s=>{o({title:"Failed to resume subscription",description:s.message,variant:"destructive"})}}),J=$({mutationFn:async s=>{const k=await(await T("POST",`/api/subscription/apply-promo/${r}`,{code:s})).json();if(!k.success)throw new Error(k.error||"Failed to apply promo code");return k},onSuccess:s=>{h({code:f.trim(),description:s.description||"Promo code applied!"}),d(null),b(""),o({title:"Promo code applied",description:s.description||"The discount has been applied to your subscription."}),z.invalidateQueries({queryKey:["/api/subscription/status",r]})},onError:s=>{d(s.message)}}),W=$({mutationFn:async()=>await(await T("POST",`/api/subscription/billing-portal/${r}`,{returnUrl:window.location.href})).json(),onSuccess:s=>{s.url&&(window.location.href=s.url)},onError:s=>{o({title:"Unable to open billing portal",description:s.message,variant:"destructive"})}}),q=$({mutationFn:async s=>await(await T("POST",`/api/subscription/change-plan/${r}`,{planId:s})).json(),onSuccess:s=>{o({title:"Plan changed",description:s.message||"Your subscription plan has been updated."}),U(null),z.invalidateQueries({queryKey:["/api/subscription/status",r]})},onError:s=>{o({title:"Failed to change plan",description:s.message,variant:"destructive"}),U(null)}}),[D,U]=g.useState(null),[ye,Y]=g.useState(!1),[ve,Z]=g.useState(1),[X,Ne]=g.useState(""),[re,we]=g.useState(""),Ce=[{value:"too_expensive",label:"Too expensive"},{value:"not_enough_calls",label:"Not getting enough calls to justify it"},{value:"missing_features",label:"Missing a feature I need"},{value:"switching_provider",label:"Switching to another provider"},{value:"business_change",label:"Business closing or changing"},{value:"other",label:"Other"}],ie=()=>{f.trim()&&(d(null),J.mutate(f.trim()))},Pe=G||be,E=ae.isPending||R.isPending||ne.isPending||q.isPending,H=(t==null?void 0:t.status)==="active"||(t==null?void 0:t.status)==="trialing",le=t==null?void 0:t.cancelAtPeriodEnd,
1ke=s=>{u(s)},Se=()=>{x?ae.mutate(x):o({title:"Please select a plan",description:"You need to select a subscription plan before proceeding",variant:"destructive"})},Fe=()=>{R.mutate({reason:X||void 0,feedback:re.trim()||void 0})},Le=()=>{ne.mutate()};if(Pe)return e.jsx("div",{className:"flex justify-center items-center h-48",children:e.jsx(j,{className:"h-8 w-8 animate-spin text-primary"})});const Ae=(M==null?void 0:M.filter(s=>s.interval===n))||[];return e.jsxs("div",{className:"container mx-auto py-8",children:[e.jsx("h2",{className:"text-3xl font-bold mb-2",children:"Subscription Plans"}),e.jsx("p",{className:"text-muted-foreground mb-4",children:"Choose the plan that's right for your business"}),e.jsxs("div",{className:"flex items-center gap-3 mb-8",children:[e.jsx("button",{onClick:()=>S("monthly"),className:`px-4 py-2 rounded-full text-sm font-medium transition-colors border ${n==="monthly"?"bg-primary text-primary-foreground border-primary":"bg-muted text-muted-foreground border-border hover:bg-accent"}`,children:"Monthly"}),e.jsxs("button",{onClick:()=>S("yearly"),className:`px-4 py-2 rounded-full text-sm font-medium transition-colors border ${n==="yearly"?"bg-primary text-primary-foreground border-primary":"bg-muted text-muted-foreground border-border hover:bg-accent"}`,children:["Annual ",e.jsx("span",{className:"text-green-500 ml-1",children:"Save 20%"})]})]}),H&&!le&&e.jsxs("div",{className:"mb-8 p-4 bg-muted rounded-lg",children:[e.jsx("h3",{className:"text-lg font-semibold",children:"Current Subscription"}),e.jsxs("p",{children:["You're currently subscribed to the ",(oe=t==null?void 0:t.plan)==null?void 0:oe.name," plan."]}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-2",children:["Your next billing date is ",new Date(t==null?void 0:t.currentPeriodEnd).toLocaleDateString(),"."]}),e.jsxs("div",{className:"flex items-center gap-3 mt-4 flex-wrap",children:[e.jsxs(c,{variant:"default",onClick:()=>W.mutate(),disabled:W.isPending,children:[W.isPending?e.jsx(j,{className:"mr-2 h-4 w-4 animate-spin"}):e.jsx(ee,{className:"mr-2 h-4 w-4"}),"Manage Billing"]}),e.jsxs(c,{variant:"outline",disabled:E,onClick:()=>{Z(1),Y(!0)},"data-testid":"cancel-subscription-button",children:[R.isPending&&e.jsx(j,{className:"mr-2 h-4 w-4 animate-spin"}),"Cancel Subscription"]}),ye&&e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",onClick:()=>Y(!1),children:e.jsx("div",{className:"bg-background rounded-lg p-6 max-w-md w-full mx-4 shadow-xl",onClick:s=>s.stopPropagation(),children:ve===1?e.jsxs(e.Fragment,{children:[e.jsx("h3",{className:"text-lg font-semibold mb-2",children:"Before you cancel"}),e.jsx("p",{className:"text-sm text-muted-foreground mb-3",children:"Here's what changes when your plan ends:"}),e.jsxs("ul",{className:"text-sm space-y-1.5 mb-4",children:[e.jsxs("li",{className:"flex gap-2",children:[e.jsx("span",{className:"text-destructive",children:"â"})," Your AI receptionist stops answering calls"]}),e.jsxs("li",{className:"flex gap-2",children:[e.jsx("span",{className:"text-destructive",children:"â"})," Your business phone number is released after 30 days"]}),e.jsxs("li",{className:"flex gap-2",children:[e.jsx("span",{className:"text-destructive",children:"â"})," SMS reminders, follow-ups, and booking agents stop"]}),e.jsxs("li",{className:"flex gap-2",children:[e.jsx("span",{className:"text-green-600",children:"â"})," Your CRM, customers, and invoices stay free forever"]})]}),(()=>{var k;const s=m((k=t==null?void 0:t.plan)==null?void 0:k.price),p=(M||[]).filter(w=>{var O;return w.interval===(((O=t==null?void 0:t.plan)==null?void 0:O.interval)||"monthly")}).filter(w=>w.planTier!=="free"&&m(w.price)>0&&m(w.price)<s).sort((w,O)=>m(O.price)-m(w.price))[0];return!p||(t==null?void 0:t.status)==="trialing"?null:e.jsxs("div",{className:"border rounded-md p-3 mb-4 bg-muted/40",children:[e.jsx("p",{className:"text-sm font-medium mb-1",children:"Want to keep the AI at a lower cost?"}),e.jsxs("p",{className:"text-xs text-muted-foreground mb-2",children:["Switch to ",p.name," for $",m(p.price),"/",p.interval==="yearly"?"yr":"mo"," â you keep your phone number and receptionist."]}),e.jsxs(c,{size:"sm",variant:"secondary",onClick:()=>{Y(!1),U(p)},"data-testid":"cancel-save-offer-downgrade",children:["Switch to ",p.name," instead"]})]})})(),e.jsxs("label",{className:"text-sm font-medium block mb-1.5",children:["What's the main reason? ",e.jsx("span",{className:"text-muted-foreground font-normal",children:"(optional)"})]}),e.jsxs("select",{className:"w-full border rounded-md bg-background p-2 text-sm mb-3",value:X,onChange:s=>Ne(s.target.value),"data-testid":"cancel-reason-select",children:[e.jsx("option",{value:"",children:"Select a reasonâ¦"}),Ce.map(s=>e.jsx("option",{value:s.value,children:s.label},s.value))]}),X&&e.jsx("textarea",{className:"w-full border rounded-md bg-background p-2 text-sm mb-3",rows:2,maxLength:1e3,placeholder:"Anything else we should know? (optional)",value:re,onChange:s=>we(s.target.value),"data-testid":"cancel-feedback-text"}),e.jsxs("div",{className:"flex justify-end gap-3 mt-2",children:[e.jsx(c,{variant:"outline",onClick:()=>Y(!1),"data-testid":"cancel-subscription-keep",children:"Keep my plan"}),e.jsx(c,{variant:"ghost",className:"text-destructive hover:text-destructive",onClick:()=>Z(2),"data-testid":"cancel-subscription-continue",children:"Continue to cancel"})]})]}):e.jsxs(e.Fragment,{children:[e.jsx("h3",{className:"text-lg font-semibold mb-2",children:(t==null?void 0:t.status)==="trialing"?"Cancel before your trial ends?":"Cancel your subscription?"}),e.jsx("p",{className:"text-sm text-muted-foreground mb-4",children:(t==null?void 0:t.status)==="trialing"?e.jsxs(e.Fragment,{children:[e.jsx("span",{className:"block mb-2 font-medium text-foreground",children:"You won't be charged."}),"Your trial ends on"," ",e.jsx("span",{className:"font-medium",children:t!=null&&t.trialEndsAt?new Date(t.trialEndsAt).toLocaleDateString():new Date(t==null?void 0:t.currentPeriodEnd).toLocaleDateString()}),". You'll keep full access until then. After that, your AI receptionist and SMS will pause, but your CRM stays free forever."]}):e.jsxs(e.Fragment,{children:["You'll keep access until"," ",e.jsx("span",{className:"font-medium",children:new Date(t==null?void 0:t.currentPeriodEnd).toLocaleDateString()}),". After that, your AI receptionist and SMS will pause, but your CRM stays free forever. You won't be charged again."]})}),e.jsxs("div",{className:"flex justify-end gap-3",children:[e.jsx(c,{variant:"outline",onClick:()=>Y(!1),children:"Keep subscription"}),e.jsxs(c,{variant:"destructive",onClick:Fe,disabled:R.isPending,"data-testid":"cancel-subscription-confirm",children:[R.isPending&&e.jsx(j,{className:"mr-2 h-4 w-4 animate-spin"}),"Yes, cancel"]})]})]})})}),!y&&!Q&&e.jsxs(c,{variant:"ghost",size:"sm",onClick:()=>a(!0),children:[e.jsx(se,{className:"mr-2 h-4 w-4"}),"Apply Promo Code"]})]}),y&&e.jsxs("div",{className:"mt-4 p-3 rounded-lg border border-green-500/40 bg-green-50 dark:bg-green-950/40 flex items-start gap-3",children:[e.jsx(se,{className:"h-4 w-4 text-green-600 dark:text-green-400 mt-0.5 shrink-0"}),e.jsxs("div",{className:"flex-1",children:[e.jsxs("p",{className:"text-sm font-medium text-green-700 dark:text-green-300",children:[y.code," applied"]}),e.jsx("p",{className:"text-xs text-green-600/80 dark:text-green-400/80 mt-0.5",children:y.description})]}),e.jsx(c,{variant:"ghost",size:"sm",onClick:()=>{h(null),b(""),d(null)},className:"h-7 text-xs",children:"Remove"})]}),!y&&Q&&e.jsxs("div",{className:"mt-4 p-3 rounded-lg border bg-background",children:[e.jsx("p",{className:"text-sm font-medium mb-2",children:"Enter promo code"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(P,{placeholder:"e.g. LAUNCH20",value:f,onChange:s=>{b(s.target.value.toUpperCase()),d(null)},onKeyDown:s=>s.key==="Enter"&&ie(),className:"flex-1"}),e.jsx(c,{onClick:ie,disabled:!f.trim()||J.isPending,size:"sm",children:J.isPending?e.jsx(j,{className:"h-4 w-4 animate-spin"}):"Apply"}),e.jsx(c,{variant:"ghost",size:"sm",onClick:()=>{a(!1),b(""),d(null)},children:"Cancel"})]}),A&&e.jsx("p",{className:"text-sm text-red-600 mt-2",children:A})]})]}),H&&le&&e.jsxs("div",{className:"mb-8 p-4 bg-muted rounded-lg",children:[e.jsx("h3",{className:"text-lg font-semibold",children:"Subscription Cancellation"}),e.jsx("p",{children:"Your subscription will be canceled at the end of the current billing period."}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-2",children:["Access ends on ",new Date(t==null?void 0:t.currentPeriodEnd).toLocaleDateString(),"."]}),e.jsxs(c,{variant:"outline",className:"mt-4",onClick:Le,disabled:E,children:[E&&e.jsx(j,{className:"mr-2 h-4 w-4 animate-spin"}),"Resume Subscription"]})]}),e.jsx("div",{className:"grid gap-8 md:grid-cols-3",children:Ae.map(s=>{var p,k,w;return e.jsxs(v,{className:`flex flex-col h-full ${x===s.id?"border-primary ring-2 ring-primary":""} ${s.planTier==="growth"||s.planTier==="professional"?"border-primary":""}`,children:[(s.planTier==="growth"||s.planTier==="professional")&&e.jsx("div",{className:"text-center py-1 bg-primary text-primary-foreground text-xs font-semibold rounded-t-lg",children:"Most Popular"}),e.jsxs(B,{children:[e.jsx(K,{className:"text-2xl font-bold",children:s.name}),e.jsx(_,{className:"mt-2",children:s.description}),e.jsxs("div",{className:"mt-4",children:[e.jsxs("span",{className:"text-3xl font-bold",children:["$",n==="yearly"?Math.round(m(s.price)/12):m(s.price)]}),e.jsx("span",{className:"text-muted-foreground ml-1",children:"/month"})]}),n==="yearly"&&e.jsxs("p",{className:"text-xs text-green-600 mt-1",children:["Billed annually at $",m(s.price),"/yr"]}),e.jsxs("div",{className:"flex items-center gap-1.5 mt-3 text-sm font-medium text-primary",children:[e.jsx(fe,{className:"h-4 w-4"}),e.jsxs("span",{children:[s.maxCallMinutes," AI minutes/mo"]})]}),e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:["then $",m(s.overageRatePerMinute).toFixed(2),"/min overage"]})]}),e.jsxs(N,{className:"flex-grow",children:[e.jsx(es,{className:"my-4"}),e.jsx("h4",{className:"font-semibold mb-4",children:"Features"}),e.jsx("ul",{className:"space-y-2",children:(p=s.features)==null?void 0:p.map((O,Me)=>e.jsxs("li",{className:"flex items-start",children:[e.jsx($e,{className:"h-5 w-5 text-green-500 mr-2 flex-shrink-0"}),e.jsx("span",{children:O})]},Me))})]}),e.jsx(Te,{children:H?((k=t==null?void 0:t.plan)==null?void 0:k.id)===s.id?e.jsx(c,{className:"w-full",variant:"outline",disabled:!0,children:"Current Plan"}):e.jsxs(c,{className:"w-full",variant:"outline",disabled:E,onClick:()=>U(s),children:[q.isPending?e.jsx(j,{className:"mr-2 h-4 w-4 animate-spin"}):null,m(s.price)>m((w=t==null?void 0:t.plan)==null?void 0:w.price)?"Upgrade":"Downgrade"," to ",s.name]}):e.jsx(c,{className:"w-full",onClick:()=>ke(s.id),variant:x===s.id?"default":"outline",disabled:E,children:x===s.id?"Selected":"Select Plan"})})]},s.id)})}),!H&&x&&e.jsx("div",{className:"mt-8 flex justify-center",children:e.jsxs(c,{size:"lg",onClick:Se,disabled:E,children:[E&&e.jsx(j,{className:"mr-2 h-4 w-4 animate-spin"}),"Subscribe Now"]})}),D&&e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",onClick:()=>U(null),children:e.jsxs("div",{className:"bg-background rounded-lg p-6 max-w-md mx-4 shadow-xl",onClick:s=>s.stopPropagation(),children:[e.jsxs("h3",{className:"text-lg font-semibold mb-2",children:[m(D.price)>m((ce=t==null?void 0:t.plan)==null?void 0:ce.price)?"Upgrade":"Downgrade"," to ",D.name,"?"]}),e.jsx("p",{className:"text-sm text-muted-foreground mb-1",children:m(D.price)>m((de=t==null?void 0:t.plan)==null?void 0:de.price)?"You'll be charged a prorated amount for the remainder of your current billing period.":"You'll receive a prorated credit on your next invoice."}),e.jsxs("p",{className:"text-sm text-muted-foreground mb-4",children:["New price: ",e.jsxs("strong",{children:["$",m(D.price),"/",D.interval==="yearly"?"year":"month"]})]}),e.jsxs("div",{className:"flex justify-end gap-3",children:[e.jsx(c,{variant:"outline",onClick:()=>U(null),children:"Cancel"}),e.jsxs(c,{onClick:()=>q.mutate(D.id),disabled:q.isPending,children:[q.isPending&&e.jsx(j,{className:"mr-2 h-4 w-4 animate-spin"}),"Confirm Change"]})]})]})})]})}function V(r){if(r==null)return 0;const i=typeof r=="number"?r:Number(r);return Number.isFinite(i)?i:0}function xe(r){return new Date(r).toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"})}function he(r,i){return`${xe(r)} - ${xe(i)}`}function pe({status:r}){switch(r){case"paid":return e.jsx(L,{className:"bg-green-100 text-green-800 hover:bg-green-100",children:"Paid"});case"invoiced":return e.jsx(L,{className:"bg-blue-100 text-blue-800 hover:bg-blue-100",children:"Invoiced"});case"failed":return e.jsx(L,{className:"bg-red-100 text-red-800 hover:bg-red-100",children:"Failed"});case"no_overage":return e.jsx(L,{className:"bg-gray-100 text-gray-600 hover:bg-gray-100",children:"No Overage"});
1case"pending":return e.jsx(L,{className:"bg-yellow-100 text-yellow-800 hover:bg-yellow-100",children:"Pending"});default:return e.jsx(L,{variant:"outline",children:r})}}function as({businessId:r}){const{data:i,isLoading:o,error:l}=I({queryKey:["/api/subscription/overage-history",r],queryFn:async()=>{const n=await fetch(`/api/subscription/overage-history/${r}`,{credentials:"include"});if(!n.ok)throw new Error("Failed to fetch overage history");return n.json()}});if(o)return e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground py-4",children:[e.jsx(Ie,{className:"h-4 w-4 animate-spin"}),"Loading billing history..."]});if(l)return e.jsx("p",{className:"text-sm text-red-500 py-4",children:"Failed to load billing history."});const u=((i==null?void 0:i.charges)||[]).filter(n=>n.status!=="no_overage");return u.length===0?e.jsxs("div",{className:"flex flex-col items-center gap-2 py-6 text-center text-muted-foreground",children:[e.jsx(Ue,{className:"h-8 w-8"}),e.jsx("p",{className:"text-sm",children:"No overage charges yet."}),e.jsx("p",{className:"text-xs",children:"If you exceed your plan's included minutes, charges will appear here."})]}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"space-y-3 sm:hidden",children:u.map(n=>e.jsxs("div",{className:"border rounded-lg p-4 space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:he(n.periodStart,n.periodEnd)}),e.jsx(pe,{status:n.status})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-2 text-sm",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Used"}),e.jsxs("p",{className:"font-medium",children:[n.minutesUsed," min"]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Included"}),e.jsxs("p",{className:"font-medium",children:[n.minutesIncluded," min"]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Overage"}),e.jsxs("p",{className:"font-medium text-orange-600",children:[n.overageMinutes," min"]})]})]}),e.jsxs("div",{className:"flex items-center justify-between pt-2 border-t",children:[e.jsxs("span",{className:"text-sm font-semibold",children:["$",V(n.overageAmount).toFixed(2)]}),e.jsxs("span",{className:"text-xs text-muted-foreground",children:["$",V(n.overageRate).toFixed(2),"/min"]}),n.stripeInvoiceUrl&&e.jsxs("a",{href:n.stripeInvoiceUrl,target:"_blank",rel:"noopener noreferrer",className:"inline-flex items-center gap-1 text-blue-600 hover:underline text-xs",children:["Invoice ",e.jsx(ee,{className:"h-3 w-3"})]})]})]},n.id))}),e.jsx("div",{className:"hidden sm:block overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b text-muted-foreground",children:[e.jsx("th",{className:"text-left py-2 font-medium",children:"Period"}),e.jsx("th",{className:"text-right py-2 font-medium",children:"Used"}),e.jsx("th",{className:"text-right py-2 font-medium",children:"Included"}),e.jsx("th",{className:"text-right py-2 font-medium",children:"Overage"}),e.jsx("th",{className:"text-right py-2 font-medium",children:"Rate"}),e.jsx("th",{className:"text-right py-2 font-medium",children:"Amount"}),e.jsx("th",{className:"text-center py-2 font-medium",children:"Status"}),e.jsx("th",{className:"text-center py-2 font-medium",children:"Invoice"})]})}),e.jsx("tbody",{children:u.map(n=>e.jsxs("tr",{className:"border-b last:border-0",children:[e.jsx("td",{className:"py-3 text-left",children:he(n.periodStart,n.periodEnd)}),e.jsxs("td",{className:"py-3 text-right",children:[n.minutesUsed," min"]}),e.jsxs("td",{className:"py-3 text-right",children:[n.minutesIncluded," min"]}),e.jsxs("td",{className:"py-3 text-right font-medium text-orange-600",children:[n.overageMinutes," min"]}),e.jsxs("td",{className:"py-3 text-right",children:["$",V(n.overageRate).toFixed(2),"/min"]}),e.jsxs("td",{className:"py-3 text-right font-semibold",children:["$",V(n.overageAmount).toFixed(2)]}),e.jsx("td",{className:"py-3 text-center",children:e.jsx(pe,{status:n.status})}),e.jsx("td",{className:"py-3 text-center",children:n.stripeInvoiceUrl?e.jsxs("a",{href:n.stripeInvoiceUrl,target:"_blank",rel:"noopener noreferrer",className:"inline-flex items-center gap-1 text-blue-600 hover:underline text-xs",children:["View ",e.jsx(ee,{className:"h-3 w-3"})]}):e.jsx("span",{className:"text-muted-foreground text-xs",children:"â"})})]},n.id))})]})})]})}const ge={name:"",address:"",city:"",state:"",zip:"",email:"",ph
1one:"",industry:"",type:"general",locationLabel:""};function ns({business:r}){const{user:i}=te(),{toast:o}=je(),l=De(),[x,u]=g.useState(!1),[n,S]=g.useState(ge),f=r==null?void 0:r.businessGroupId,{data:b,isLoading:A}=I({queryKey:["/api/user/locations"],enabled:!!i}),d=(b==null?void 0:b.locations)??(Array.isArray(b)?b:[]),y=$({mutationFn:async a=>{if(!f)throw new Error("No business group found. Please contact support to enable multi-location.");return await(await T("POST",`/api/business-groups/${f}/add-location`,a)).json()},onSuccess:()=>{l.invalidateQueries({queryKey:["/api/user/locations"]}),l.invalidateQueries({queryKey:["/api/business"]}),u(!1),S(ge),o({title:"Location added",description:"Your new location has been created successfully."})},onError:a=>{o({title:"Failed to add location",description:a.message,variant:"destructive"})}}),h=(a,M)=>{S(G=>({...G,[a]:M}))},Q=a=>{if(a.preventDefault(),!n.name.trim()||!n.email.trim()){o({title:"Validation error",description:"Name and email are required.",variant:"destructive"});return}y.mutate(n)};return A?e.jsx(v,{children:e.jsx(N,{className:"py-10 flex items-center justify-center",children:e.jsx(j,{className:"h-6 w-6 animate-spin text-muted-foreground"})})}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold",children:"Locations"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Manage all locations in your business group."})]}),e.jsxs(Ye,{open:x,onOpenChange:u,children:[e.jsx(Oe,{asChild:!0,children:e.jsxs(c,{size:"sm",children:[e.jsx(Re,{className:"h-4 w-4 mr-2"}),"Add Location"]})}),e.jsxs(qe,{className:"sm:max-w-lg max-h-[90vh] overflow-y-auto",children:[e.jsxs(ze,{children:[e.jsx(Be,{children:"Add New Location"}),e.jsx(Ke,{children:"Create a new location under your business group. Each location gets its own dashboard, customers, and settings."})]}),e.jsxs("form",{onSubmit:Q,className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"col-span-2",children:[e.jsx(C,{htmlFor:"loc-name",children:"Business Name *"}),e.jsx(P,{id:"loc-name",placeholder:"e.g., Joe's Pizza - Downtown",value:n.name,onChange:a=>h("name",a.target.value),required:!0})]}),e.jsxs("div",{className:"col-span-2",children:[e.jsx(C,{htmlFor:"loc-label",children:"Location Label"}),e.jsx(P,{id:"loc-label",placeholder:'e.g., "Downtown", "North Side"',value:n.locationLabel,onChange:a=>h("locationLabel",a.target.value)})]}),e.jsxs("div",{className:"col-span-2",children:[e.jsx(C,{htmlFor:"loc-address",children:"Address"}),e.jsx(P,{id:"loc-address",placeholder:"123 Main St",value:n.address,onChange:a=>h("address",a.target.value)})]}),e.jsxs("div",{children:[e.jsx(C,{htmlFor:"loc-city",children:"City"}),e.jsx(P,{id:"loc-city",placeholder:"New York",value:n.city,onChange:a=>h("city",a.target.value)})]}),e.jsxs("div",{children:[e.jsx(C,{htmlFor:"loc-state",children:"State"}),e.jsx(P,{id:"loc-state",placeholder:"NY",value:n.state,onChange:a=>h("state",a.target.value)})]}),e.jsxs("div",{children:[e.jsx(C,{htmlFor:"loc-zip",children:"Zip Code"}),e.jsx(P,{id:"loc-zip",placeholder:"10001",value:n.zip,onChange:a=>h("zip",a.target.value)})]}),e.jsxs("div",{children:[e.jsx(C,{htmlFor:"loc-phone",children:"Phone"}),e.jsx(P,{id:"loc-phone",placeholder:"(555) 123-4567",value:n.phone,onChange:a=>h("phone",a.target.value)})]}),e.jsxs("div",{className:"col-span-2",children:[e.jsx(C,{htmlFor:"loc-email",children:"Email *"}),e.jsx(P,{id:"loc-email",type:"email",placeholder:"[email protected]",value:n.email,onChange:a=>h("email",a.target.value),required:!0})]}),e.jsxs("div",{children:[e.jsx(C,{htmlFor:"loc-industry",children:"Industry"}),e.jsx(P,{id:"loc-industry",placeholder:"e.g., restaurant, plumbing",value:n.industry,onChange:a=>h("industry",a.target.value)})]}),e.jsxs("div",{children:[e.jsx(C,{htmlFor:"loc-type",children:"Business Type"}),e.jsxs(_e,{value:n.type,onValueChange:a=>h("type",a),children:[e.jsx(Qe,{id:"loc-type",children:e.jsx(He,{placeholder:"Select type"})}),e.jsxs(Ve,{children:[e.jsx(F,{value:"general",children:"General"}),e.jsx(F,{value:"plumbing",children:"Plumbing"}),e.jsx(F,{value:"electrical",children:"Electrical"}),e.jsx(F,{value:"hvac",children:"HVAC"}),e.jsx(F,{value:"restaurant",children:"Restaurant"}),e.jsx(F,{value:"medical",children:"Medical"}),e.jsx(F,{value:"salon",children:"Salon"}),e.jsx(F,{value:"other",children:"Other"})]})]})]})]}),e.jsxs(Ge,{children:[e.jsx(c,{type:"button",variant:"outline",onClick:()=>u(!1),disabled:y.isPending,children:"Cancel"}),e.jsx(c,{type:"submit",disabled:y.isPending,children:y.isPending?e.jsxs(e.Fragment,{children:[e.jsx(j,{className:"h-4 w-4 mr-2 animate-spin"}),"Adding..."]}):"Add Location"})]})]})]})]})]}),d.length>=1&&e.jsx(v,{className:"border-blue-200 bg-blue-50 dark:border-blue-900 dark:bg-blue-950/30",children:e.jsxs(N,{className:"py-3 px-4 flex items-start gap-3",children:[e.jsx(ss,{className:"h-4 w-4 text-blue-600 dar
1k:text-blue-400 mt-0.5 shrink-0"}),e.jsxs("p",{className:"text-sm text-blue-700 dark:text-blue-300",children:["Multi-location pricing: businesses with 2 or more locations receive a ",e.jsx("strong",{children:"20% discount"})," on each location's subscription. The discount is applied automatically to your consolidated billing."]})]})}),e.jsx("div",{className:"grid gap-4 md:grid-cols-2",children:d.map(a=>e.jsxs(v,{className:a.id===(i==null?void 0:i.businessId)?"border-primary ring-1 ring-primary/20":"",children:[e.jsxs(B,{className:"pb-3",children:[e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(me,{className:"h-4 w-4 text-muted-foreground"}),e.jsx(K,{className:"text-base",children:a.name})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[a.id===(i==null?void 0:i.businessId)&&e.jsx(L,{variant:"secondary",children:"Current"}),e.jsx(L,{variant:a.isActive?"success":"destructive",children:a.isActive?"Active":"Inactive"})]})]}),a.locationLabel&&e.jsxs(_,{className:"flex items-center gap-1.5 mt-1",children:[e.jsx(se,{className:"h-3 w-3"}),a.locationLabel]})]}),e.jsxs(N,{className:"space-y-2 text-sm",children:[(a.address||a.city||a.state||a.zip)&&e.jsxs("div",{className:"flex items-start gap-2 text-muted-foreground",children:[e.jsx(Je,{className:"h-3.5 w-3.5 mt-0.5 shrink-0"}),e.jsx("span",{children:[a.address,[a.city,a.state].filter(Boolean).join(", "),a.zip].filter(Boolean).join(", ")})]}),a.phone&&e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground",children:[e.jsx(fe,{className:"h-3.5 w-3.5 shrink-0"}),e.jsx("span",{children:a.phone})]}),a.email&&e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground",children:[e.jsx(We,{className:"h-3.5 w-3.5 shrink-0"}),e.jsx("span",{children:a.email})]})]})]},a.id))}),d.length===0&&e.jsx(v,{children:e.jsxs(N,{className:"py-10 text-center text-muted-foreground",children:[e.jsx(me,{className:"h-10 w-10 mx-auto mb-3 opacity-40"}),e.jsx("p",{className:"text-sm",children:"No locations found. Add your first location to get started."})]})})]})}function rs({businessId:r}){const{data:i,isLoading:o}=I({queryKey:[`/api/subscription/usage/${r}`],enabled:!!r,staleTime:6e4,queryFn:async()=>{const d=await fetch(`/api/subscription/usage/${r}`,{credentials:"include"});if(!d.ok)throw new Error("Failed to fetch usage");return d.json()}}),{data:l,isLoading:x}=I({queryKey:["/api/usage/projection"],enabled:!!r,staleTime:6e4});if(o||x)return e.jsx(v,{children:e.jsx(N,{className:"flex justify-center py-8",children:e.jsx(j,{className:"h-6 w-6 animate-spin text-muted-foreground"})})});if(!i)return null;const u=i.percentUsed,n=u>80?"bg-red-500":u>50?"bg-yellow-500":"bg-green-500",S=d=>new Date(d).toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"}),f=l?`${S(l.billingPeriodStart)} - ${S(l.billingPeriodEnd)}`:null,A=(d=>!d||d==="trial"||d==="starter"?"Growth":d==="growth"||d==="professional"?"Pro":null)(i.planTier);return e.jsxs("div",{className:"space-y-4",children:[e.jsxs(v,{children:[e.jsxs(B,{className:"pb-3",children:[e.jsxs(K,{className:"flex items-center gap-2",children:[e.jsx(Ze,{className:"h-5 w-5"}),"Call Minutes"]}),f&&e.jsxs(_,{children:["Billing period: ",f]})]}),e.jsxs(N,{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-baseline gap-1",children:[e.jsx("span",{className:"text-3xl font-bold",children:i.minutesUsed}),e.jsxs("span",{className:"text-lg text-muted-foreground",children:["of ",i.minutesIncluded," minutes used"]})]}),e.jsx("div",{className:"relative h-4 w-full overflow-hidden rounded-full bg-secondary",children:e.jsx("div",{className:`h-full transition-all rounded-full ${n}`,style:{width:`${Math.min(u,100)}%`}})}),e.jsxs("div",{className:"flex items-center justify-between text-sm text-muted-foreground",children:[e.jsxs("span",{children:[i.minutesRemaining," minutes remaining"]}),l&&e.jsxs("span",{children:["~",l.averageDailyMinutes," min/day"]})]}),l&&l.daysRemainingInPeriod>0&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:[l.daysRemainingInPeriod," day",l.daysRemainingInPeriod!==1?"s":""," remaining in billing period"]}),i.overageMinutes>0&&e.jsxs("div",{className:"flex items-center gap-2 rounded-md border border-red-200 bg-red-50 dark:bg-red-950/20 dark:border-red-900 px-3 py-2 text-sm",children:[e.jsx(ue,{className:"h-4 w-4 text-red-500 flex-shrink-0"}),e.jsxs("span",{children:["Current overage: ",i.overageMinutes," minutes ($",i.overageCost.toFixed(2)," at $",i.overageRate.toFixed(2),"/min)"]})]})]})]}),l&&l.projectedOverageMinutes>0&&i.overageMinutes===0&&e.jsx(v,{className:"border-yellow-200 dark:border-yellow-900",children:e.jsx(N,{className:"pt-6",children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx(ue,{className:"h-5 w-5 text-yellow-500 flex-shrink-0 mt-0.5"}),e.jsxs("div",{className:"space-y-1",children:[e.jsxs("p",{className:"font-medium text-sm",children:["Projected overage: ",l.projectedOverageMinutes," minutes (~$",l.projectedOverageCost.toFixed(2),")"]}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["At current pace, you'll use ~",l.projectedMinutesAtPeriodEnd," minutes by end of billing period."]})]})]})})}),u>80&&A&&e.jsx(v,{className:"border-primary/30 bg-primary/5",children:e.jsx(N,{className:"pt-6",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"font-medium text-sm",children:"Running low on minutes"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Upgrade to ",A," for more included minutes and lower overage rates."]})]}),e.jsxs(c,{variant:"default",size:"sm",className:"flex-shrink-0 ml-4",children:[e.jsx(Xe,{className:"h-4 w-4 mr-1"}),"Upgrade"]})]})})})]})}function ps({activeTab:r}){const{user:i}=te(),o=i==null?void 0:i.businessId,{data:l,isLoading:x}=I({queryKey:["/api/business"],enabled:!!o});return r==="locations"?e.jsx("div",{className:"space-y-4",children:l&&e.jsx(ns,{business:l})}):e.jsxs("div",{className:"space-y-4",children:[l&&e.jsx(rs,{businessId:l.id}),e.jsxs(v,{children:[e.jsxs(B,{children:[e.jsx(K,{children:"Subscription Management"}),e.jsx(_,{children:"Manage your SmallBizAgent subscription plan"})]}),e.jsx(N,{children:x?e.jsx("div",{className:"flex justify-center py-8",children:e.jsx("div",{className:"animate-spin w-8 h-8 border-4 border-primary rounded-full border-t-transparent"})}):l&&e.jsx(ts,{businessId:l.id})})]}),l&&e.jsxs(v,{children:[e.jsxs(B,{children:[e.jsx(K,{children:"Overage Billing History"}),e.jsx(_,{children:"Minutes beyond your plan limit are billed automatically at the end of each billing period."})]}),e.jsx(N,{children:e.jsx(as,{businessId:l.id})})]})]})}export{ps 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.