1import{r as v,j as e,s as O,a as Y,i as $,n as z,b as w}from"./index-IZnvRumJ.js";import{L as H,c as _,S as Z}from"./AppLayout-cVkycFLS.js";import{u as q}from"./useSubscription-C5YM4a1B.js";import{u as K}from"./usePlans-dMbh1diU.js";import{C as S,a as T,b as D,c as L,d as R}from"./card-BWWzAZHh.js";import{B as G}from"./button-DcP8iMA9.js";import{B as P}from"./badge-CDTGuZL7.js";import{P as Q}from"./progress-C5fUdYfo.js";import{G as W}from"./gauge-CdXkSZap.js";import{Z as X}from"./zap-BJhwTE2I.js";import{U as V}from"./users-HfDtNntb.js";import{C as k}from"./crown-CGzwbtTd.js";import{C as I}from"./circle-check-CJPteXk5.js";import{C as ee}from"./circle-alert-BmsUTnOk.js";import{E as se}from"./external-link-1LE5Lgze.js";import{S as oe}from"./sparkles-C4440FS9.js";import"./input-OGCcGVWJ.js";import"./sheet-zYE83Wug.js";import"./index-MH1WVYsB.js";import"./skeleton-D7D6JDZR.js";import"./mysteryShopperBrand-BYkn7pdw.js";import"./mysterycallai-logo-dark-BFNFGS3B.js";import"./useTier-BULoQlR6.js";import"./dropdown-menu-DqOrHu2j.js";import"./index-BmRzOyoq.js";import"./index-BlZBv0Jc.js";import"./chevron-right-QiugxkLR.js";import"./check-C2pnSUS4.js";import"./chevron-down-1nMbKwc_.js";import"./chart-column-CsqJFWcn.js";import"./bot-BQdRaamy.js";import"./target-tsid7M2N.js";import"./swords-CyoFjmFq.js";import"./file-pen-line-DkW7k01H.js";import"./calculator-Pwff5vj0.js";import"./phone-call-Dm3n_iYk.js";import"./useQuery-DHnKGTyB.js";import"./circle-x-Bkg0PfRg.js";import"./triangle-alert-B8sOFBAg.js";import"./lpInvoke-Ds_Z-Vno.js";import"./shield-ebKOM8r4.js";import"./building-2-TavQYcI3.js";import"./mail-Rp6evAGI.js";import"./credit-card-Dge_IzRx.js";import"./file-text-CZEMuyyH.js";import"./search-9X0la83E.js";import"./trending-up-DZRNf4zr.js";import"./circle-plus-HJRNrV0j.js";import"./eye-BaRUKt7b.js";import"./globe-CwLKkDOx.js";import"./lightbulb-uxg2Rvra.js";import"./graduation-cap-25AkShwK.js";import"./palette-D2KBmLK0.js";import"./layout-dashboard-D4Xc7vFm.js";import"./book-open-C7ggllaI.js";import"./circle-help-LlvxE_IR.js";import"./alert-dialog-BrhGOoeP.js";import"./dialog-RI8CE1mU.js";import"./label-CoGXWevP.js";import"./select-Ds0eJjSi.js";import"./index-DyIwh2Qx.js";import"./chevron-up-CcQezlK8.js";import"./plus-GBlIdfW8.js";import"./types-C-jxggw2.js";import"./arrow-right-C8S3duny.js";import"./publicUrl-bjBORCqm.js";const re=t=>t==null||t<0?"â":String(t);function U({icon:t,label:c,used:a,cap:s,color:p="primary"}){const m=s==null||s<0,h=m?0:Math.min(100,Math.round(a/Math.max(1,s)*100)),g=!m&&h>=90,u=!m&&h>=75&&h<90;return e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs("div",{className:"flex items-center justify-between text-sm",children:[e.jsxs("span",{className:"flex items-center gap-2 text-muted-foreground",children:[e.jsx(t,{className:"h-4 w-4"}),c]}),e.jsxs("span",{className:"font-medium tabular-nums",children:[a,e.jsxs("span",{className:"text-muted-foreground",children:[" / ",re(s)]}),g&&e.jsx(P,{variant:"destructive",className:"ml-2",children:"Limit reached"}),u&&e.jsx(P,{variant:"secondary",className:"ml-2",children:"Nearing limit"})]})]}),!m&&e.jsx(Q,{value:h})]})}function te({agencyId:t}){const[c,a]=v.useState(null),[s,p]=v.useState(!0);return v.useEffect(()=>{let m=!1;return(async()=>{if(!t)return;p(!0);const{data:g,error:u}=await O.rpc("lp_get_usage",{_agency_id:t});if(!m){if(!u&&g){const N=Array.isArray(g)?g[0]:g;a(N)}p(!1)}})(),()=>{m=!0}},[t]),t?e.jsxs(S,{children:[e.jsx(T,{className:"pb-3",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(W,{className:"h-5 w-5 text-primary"}),e.jsxs("div",{children:[e.jsx(D,{className:"text-base",children:"Plan usage"}),e.jsx(L,{children:"Set from your plan in super-admin."})]})]})}),e.jsx(R,{className:"space-y-4",children:s||!c?e.jsx("div",{className:"text-sm text-muted-foreground",children:"Loading usageâ¦"}):e.jsxs(e.Fragment,{children:[e.jsx(U,{icon:X,label:"Proposal credits remaining",used:c.proposal_credits_balance??0,cap:null}),e.jsx(U,{icon:V,label:"Active customers",used:c.active_customers??0,cap:c.customers_limit}),e.jsx(U,{icon:H,label:"Package templates",used:c.template_count??0,cap:c.templates_limit})]})})]}):null}const fs=()=>{const{toast:t}=Y(),{userRole:c,agencyId:a}=$(),{subscription:s,loading:p,managingSubscription:m,manageSubscription:h}=q(a||void 0),{plans:g,loading:u}=K(s==null?void 0:s.plan_id),[N,b]=v.useState(!1),{hasLocalProposal:M}=z(),F=[...g].sort((o,l)=>{const i=(s==null?void 0:s.plan_id)===o.id,f=(s==null?void 0:s.plan_id)===l.id;return i?-1:f?1:0}),B=async o=>{var y,A,C;const l=Date.now();if(console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.log("ð [UPGRADE MONITOR] Starting upgrade process"),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.log("ð [UPGRADE] Initial State:",{planId:o,agencyId:a,currentPlanId:s==null?void 0:s.plan_id,currentPlanName:s==null?void 0:s.plan_name,subscriptionLoading:p,plansLoading:u,timestamp:new Date().toISOString()}),!a||p||u){console.warn("â ï¸ [UPGRADE] Blocked - Data not ready:",{agencyId:a,subscriptionLoading:p,plansLoading:u}),t({title:"Please wait",description:"Loading your subscription details...",variant:"default"});return}const i=`pending_upgrade_${a}`,f=localStorage.getItem(i);if(console.log("ð [UPGRADE] Checking for pending upgrade:",f),f){const n=JSON.parse(f),d=Date.now()-n.timestamp;if(console.log("â±ï¸ [UPGRADE] Found pending upgrade:",{pending:n,elapsedMs:d,elapsedMinutes:Math.round(d/6e4)}),d<3e5){console.warn("ð« [UPGRADE] Blocked - Pending upgrade within 5 minutes"),t({title:"Upgrade already in progress",description:"You have a pending upgrade. Please complete or wait 5 minutes.",variant:"destructive"});return}else console.log("â
[UPGRADE] Pending upgrade expired, proceeding")}try{b(!0);const n={agencyId:a,newPlanId:o,timestamp:Date.now(),status:"initiated"};localStorage.setItem(i,JSON.stringify(n)),console.log("ð¾ [UPGRADE] Saved initiated state to localStorage:",n),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.log("ð¡ [UPGRADE] Calling change-subscription edge function..."),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ");const d=Date.now(),{data:r,error:x}=await O.functions.invoke("change-subscription",{body:{planId:o}}),J=Date.now()-d;if(console.log("ð¥ [UPGRADE] Edge function response received:",{durationMs:J,hasError:!!x,hasData:!!r,data:r,error:x}),x){console.error("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.error("â [UPGRADE] Error object received:"),console.error("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.error("Error details:",JSON.stringify(x,null,2)),console.error("Error context:",x.context),console.error("Error message:",x.message),console.error("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),localStorage.removeItem(i),console.log("ðï¸ [UPGRADE] Removed pending upgrade from localStorage");let j=((y=x.context)==null?void 0:y.error)||x.message||"Failed to change plan";j.includes("already in progress")&&(j="A plan change is already in progress. Please wait a few minutes or contact support."),t({title:"Upgrade Failed",description:j,variant:"destructive"}),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.log("â [UPGRADE MONITOR] Upgrade failed with error"),console.log("Total duration:",Date.now()-l,"ms"),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ");return}if(r!=null&&r.error){console.error("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.error("â [UPGRADE] Error in response data:",r.error),console.error("Full response data:",JSON.stringify(r,null,2)),console.error("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),localStorage.removeItem(i),console.log("ðï¸ [UPGRADE] Removed pending upgrade from localStorage"),t({title:"Upgrade Failed",description:r.error,variant:"destructive"}),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.log("â [UPGRADE MONITOR] Upgrade failed with data error"),console.log("Total duration:",Date.now()-l,"ms"),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ");return}if(r!=null&&r.success){console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.log("â¡ [UPGRADE] INSTANT UPGRADE SUCCESSFUL!"),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.log("Response data:",JSON.stringify(r,null,2)),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),localStorage.removeItem(i),console.log("ðï¸ [UPGRADE] Removed pending upgrade from localStorage"),t({title:"Upgrade successful!",description:"Your plan has been upgraded instantly."}),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.log("â
[UPGRADE MONITOR] Instant upgrade completed successfully"),console.log("Total duration:",Date.now()-l,"ms"),console.log("Reloading page..."),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),window.location.reload();return}if(r!=null&&r.url){console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.log("ð [UPGRADE] Checkout session URL received"),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.log("Checkout URL:",r.url),console.log("Session ID:",r.sessionId),console.log("Is Lifetime:",r.isLifetime),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ");const j={sessionId:r.sessionId,agencyId:a,newPlanId:o,timestamp:Date.now(),status:"checkout_opened"};localStorage.setItem(i,JSON.stringify(j)),console.log("ð¾ [UPGRADE] Updated localStorage with checkout state:",j),console.log("ð [UPGRADE] Opening checkout in new tab..."),window.open(r.url,"_blank"),t({title:"Complete your upgrade",description:r.isLifetime?"Complete your one-time payment to upgrade":"Complete checkout to change your subscription"}),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.log("â
[UPGRADE MONITOR] Checkout opened successfully"),console.log("Total duration:",Date.now()-l,"ms"),console.log("Waiting for user to complete Stripe checkout..."),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ")}}catch(n){console.error("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.error("ð¥ [UPGRADE] EXCEPTION CAUGHT"),console.error("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.error("Exception type:",n.constructor.name),console.error("Exception message:",n.message),console.error("Exception stack:",n.stack),console.error("Full exception object:",JSON.stringify(n,Object.getOwnPropertyNames(n),2)),console.error("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),localStorage.removeItem(i),console.log("ðï¸ [UPGRADE] Removed pending upgrade from localStorage");let d="An unexpected error occurred. Please try again.";(A=n.message)!=null&&A.includes("fetch")?d="Network error. Please check your connection and try again.":(C=n.message)!=null&&C.includes("already in progress")?d="A plan change is already in progress. Please wait a few minutes or contact support.":n.message&&(d=n.message),t({title:"Upgrade Failed",description:d,variant:"destructive"}),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ"),console.log("â [UPGRADE MONITOR] Upgrade failed with exception"),console.log("Total duration:",Date.now()-l,"ms"),console.log("âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ")}
1finally{b(!1),console.log("ð [UPGRADE] Finally block executed, changingPlan set to false")}},E=o=>({monthly:"Monthly",quarterly:"Quarterly",yearly:"Yearly",lifetime:"Lifetime"})[o]||o;return p||u?e.jsx(_,{userRole:c||"agency_admin",pageTitle:"Subscription",children:e.jsx("div",{className:"flex items-center justify-center p-8",children:e.jsx(w,{className:"h-8 w-8 animate-spin text-muted-foreground"})})}):e.jsx(_,{userRole:c||"agency_admin",pageTitle:"Subscription",pageDescription:"Manage your subscription plan and billing",children:e.jsxs("div",{className:"max-w-6xl space-y-6",children:[M&&a&&e.jsx(te,{agencyId:a}),(s==null?void 0:s.subscribed)&&e.jsx(S,{children:e.jsx(R,{className:"pt-6",children:e.jsxs("div",{className:"flex flex-col md:flex-row md:items-center md:justify-between gap-4",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(D,{className:"flex items-center gap-2 text-lg",children:["Current Plan",s.plan_tier==="premium"&&e.jsx(k,{className:"h-4 w-4 text-yellow-500"})]}),e.jsx(P,{variant:s.status==="active"?"default":"secondary",className:"text-xs",children:s.status==="active"?e.jsxs(e.Fragment,{children:[e.jsx(I,{className:"h-3 w-3 mr-1"})," Active"]}):e.jsxs(e.Fragment,{children:[e.jsx(ee,{className:"h-3 w-3 mr-1"})," ",s.status]})})]}),e.jsx(Z,{orientation:"vertical",className:"h-8 hidden md:block"}),e.jsxs("div",{className:"flex gap-6",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-semibold",children:s.plan_name}),e.jsx("p",{className:"text-xs text-muted-foreground capitalize",children:s.plan_type?E(s.plan_type):"N/A"})]}),(()=>{const o=g.find(l=>l.id===s.plan_id);return o?e.jsxs("div",{children:[e.jsxs("p",{className:"text-sm font-semibold",children:["$",o.price,o.billing_interval!=="lifetime"&&`/${o.billing_interval}`]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Current Price"})]}):null})(),e.jsxs("div",{children:[e.jsxs("p",{className:"text-sm font-semibold",children:[s.reports_used||0," / ",s.reports_limit===-1?"200":s.reports_limit||0]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Reports This Month"})]})]})]}),e.jsx(G,{onClick:h,disabled:m,variant:"outline",size:"sm",children:m?e.jsxs(e.Fragment,{children:[e.jsx(w,{className:"h-4 w-4 mr-2 animate-spin"})," Loading..."]}):e.jsxs(e.Fragment,{children:[e.jsx(se,{className:"h-4 w-4 mr-2"})," Manage Billing"]})})]})})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-2xl font-bold",children:"Available Plans"}),e.jsx("p",{className:"text-muted-foreground",children:"Upgrade to unlock more features and reports"})]}),e.jsx("div",{className:"grid md:grid-cols-2 lg:grid-cols-3 gap-6",children:F.map(o=>{const l=(s==null?void 0:s.plan_id)===o.id,i=Array.isArray(o.features)?o.features:[];return e.jsxs(S,{className:l?"border-primary border-2 shadow-xl bg-primary/5":"",children:[e.jsxs(T,{children:[e.jsxs("div",{className:"flex items-center justify-between mb-2",children:[e.jsxs(D,{className:"flex items-center gap-2",children:[o.name,o.type==="premium"&&e.jsx(k,{className:"h-5 w-5 text-yellow-500"})]}),l&&e.jsxs(P,{variant:"default",className:"text-sm px-3 py-1",children:[e.jsx(I,{className:"h-3 w-3 mr-1"}),"Your Plan"]})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsxs("div",{className:"flex items-baseline gap-1",children:[e.jsxs("span",{className:"text-3xl font-bold",children:["$",o.price]}),o.billing_interval!=="lifetime"&&e.jsxs("span",{className:"text-muted-foreground",children:["/",o.billing_interval]})]}),e.jsx("p",{className:"text-sm text-muted-foreground capitalize",children:E(o.billing_interval)})]}),o.description&&e.jsx(L,{className:"mt-2",children:o.description})]}),e.jsxs(R,{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("p",{className:"text-sm font-medium",children:[o.reports_limit===-1?"Unlimited":o.reports_limit," reports"]}),i.length>0&&e.jsx("ul",{className:"space-y-2",children:i.map((f,y)=>e.jsxs("li",{className:"flex items-start gap-2 text-sm",children:[e.jsx(oe,{className:"h-4 w-4 text-primary shrink-0 mt-0.5"}),e.jsx("span",{children:f})]},y))})]}),!l&&e.jsx(G,{className:"w-full",onClick:()=>B(o.id),disabled:N||p||u||!a,children:N?e.jsxs(e.Fragment,{children:[e.jsx(w,{className:"h-4 w-4 mr-2 animate-spin"})," Processing..."]}):e.jsxs(e.Fragment,{children:["Upgrade to ",o.name]})})]})]},o.id)})})]})]})})};export{fs 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.