1import{E as V,e as J,f as K,a2 as X,P as Z,r as l,j as e,N as E,aq as W,B as b,S as O,ax as ee,a1 as te,A as se,s as u,ag as S}from"./index-CSNO-dh3.js";import{g as D,c as re}from"./pendingCartItems-CoGLfigX.js";import{R as ae}from"./react-confetti-JvcvI06m.js";import{C as ie}from"./circle-alert-BdOe6auw.js";import{C as oe}from"./circle-check-big-BWS6unSM.js";const ne=[["path",{d:"M12 7v14",key:"1akyts"}],["path",{d:"M20 11v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2v-8",key:"1sqzm4"}],["path",{d:"M7.5 7a1 1 0 0 1 0-5A4.8 8 0 0 1 12 7a4.8 8 0 0 1 4.5-5 1 1 0 0 1 0 5",key:"kc0143"}],["rect",{x:"3",y:"7",width:"18",height:"4",rx:"1",key:"1hberx"}]],le=V("gift",ne),ce=()=>{const y=J(),{refreshSubscription:A}=K(),{clearCart:P,addToCart:B,refreshCart:k}=X(),[f]=Z(),[L,x]=l.useState(!0),[Y,w]=l.useState(null),[de,T]=l.useState(!1),[me,F]=l.useState(!1),[v,I]=l.useState(0),[q,M]=l.useState(!1),[U,R]=l.useState(!1),[z,H]=l.useState({width:window.innerWidth,height:window.innerHeight}),C=l.useRef(!1);l.useEffect(()=>{const n=()=>{H({width:window.innerWidth,height:window.innerHeight})};return window.addEventListener("resize",n),()=>window.removeEventListener("resize",n)},[]),l.useEffect(()=>{const n=s=>new Promise(t=>setTimeout(t,s)),p=async(s,t,r)=>{const a=new Date(Date.now()-18e5).toISOString();if(t==="membership"||t==="subscription"){const{data:h,error:$}=await u.from("user_memberships").select("id").eq("user_id",r).eq("status","active").limit(1);return $&&console.warn("[PaymentSuccess] Membership poll error",$),(h?.length||0)>0}let c=u.from("orders").select("id, order_type, status, payment_status, created_at").eq("user_id",r).in("order_type",["course","certification","aidp_sample"]).order("created_at",{ascending:!1}).limit(20);c=s?c.eq("stripe_session_id",s):c.gte("created_at",a);const{data:N,error:d}=await c;if(d&&console.warn("[PaymentSuccess] Order poll error",d),(N||[]).some(h=>h.status==="completed"||h.payment_status==="paid"))return!0;const[{data:o},{data:m}]=await Promise.all([u.from("user_courses").select("id, created_at").eq("user_id",r).gte("created_at",a).limit(1),u.from("user_certifications").select("id, created_at").eq("user_id",r).gte("created_at",a).limit(1)]);return(o?.length||0)>0||(m?.length||0)>0},_=async(s,t,r)=>{for(let a=1;a<=10;a++){if(await p(s,t,r))return!0;console.log(`[PaymentSuccess] Fulfillment not visible yet, polling ${a}/10`),await n(2e3)}return!1},j=async(s,t=!1)=>{T(!0),x(!1),R(!0),t&&await A();const r=D();if(t&&r.length>0)I(r.length);else try{await P(),await k(),console.log("[PaymentSuccess] Cart cleared successfully")}catch(a){console.error("[PaymentSuccess] Failed to clear cart:",a)}S({title:"Success!",description:s})};(async()=>{if(C.current){console.log("[PaymentSuccess] Already processing, skipping duplicate call");return}C.current=!0,F(!0);const s=f.get("session_id"),t=f.get("type");console.log("[PaymentSuccess] Starting payment processing",{sessionId:s,type:t});const{data:{session:r}}=await u.auth.getSession();if(!r){console.error("[PaymentSuccess] Not authenticated"),w("Not authenticated. Please log in and try again."),x(!1);return}if(!s&&(t==="bulk"||t==="subscription")){console.log("[PaymentSuccess] Processing gateway success without Stripe session",{type:t}),await _(null,t,r.user.id)?await j(t==="subscription"?"Your membership is now active.":"Your purchase has been completed and is visible in your account.",t==="subscription"):(w("Payment succeeded, but the order is still syncing. Please check your dashboard in a few minutes or contact support with your payment ID."),x(!1));return}if(!s){console.error("[PaymentSuccess] No session ID found"),w("No session ID found. Please contact support with your payment confirmation."),x(!1);return}const a=3;let c=null;for(let d=1;d<=a;d++)try{if(console.log(`[PaymentSuccess] Attempt ${d}/${a}`),console.log("[PaymentSuccess] User authenticated:",r.user.id),t==="course"||t==="certification"||t==="bulk"||!t){console.log(`[PaymentSuccess] Processing ${t} purchase with session:`,s);const{data:i,error:o}=await u.functions.invoke("create-user-purchase-from-stripe",{body:{session_id:s}});if(console.log("[PaymentSuccess] Function response:",{data:i,error:o}),o)throw console.error("[PaymentSuccess] Function error:",o),o;if(i?.requiresMembershipSync){console.log("[PaymentSuccess] Checkout requires membership sync");const{data:m,error:h}=await u.functions.invoke("create-user-membership-from-stripe",{body:{session_id:s}});if(h)throw h;if(!m||m.error)throw new Error(m?.error||"Unknown membership sync error");await j("Your membership has been activated.",!0);return}if(!i||i.error)throw new Error(i?.error||"Unknown error from function");console.log("[PaymentSuccess] Purchase processed successfully:",i),await _(s,t,r.user.id),await j("Your enrollment has been activated.");return}else{console.log("[PaymentSuccess] Processing membership purchase");const{data:i,error:o}=await u.functions.invoke("create-user-membership-from-stripe",{body:{session_id:s}});if(console.log("[PaymentSuccess] Membership function response:",{data:i,error:o}),o)throw console.error("[PaymentSuccess] Membership function error:",o),o;const m=D();m.length>0?I(m.length):await _(s,"membership",r.user.id),await j(m.length>0?"Your membership is active! Complete your remaining items now.":"Your membership has been activated.",!0);return}}catch(i){c=i instanceof Error?i:new Error(String(i)),console.error(`[PaymentSuccess] Attempt ${d} failed:`,c),d<a&&(console.log("[PaymentSuccess] Waiting 2 seconds before retry..."),await new Promise(o=>setTimeout(o,2e3)))}console.error("[PaymentSuccess] All retry attempts failed:",c);const N=c?.message||"Failed to activate purchase after multiple attempts";w(N),S({title:"Payment Processing Error",description:`${N}. Your payment was successful but enrollment failed. Please contact support with session ID: ${s}`,variant:"destructive"}),x(!1)})().finally(()=>{C.current=!1,F(!1)})},[f,A,P,k]);const G=async()=>{M(!0);try{const n=D();await P();for(const p of n)await B(p.item_id,p.item_type,p.duration,p.metadata);re(),await k(),S({title:"Cart Updated",description:"Your remaining items are ready with member pricing!"}),y("/cart")}catch(n){console.error("Failed to restore pending items:",n),S({title:"Error",description:"Failed to restore cart items. Please try again.",variant:"destructive"})}finally{M(!1)}},g=!f.get("type"),Q=f.get("type");return L?e.jsxs("div",{className:"min-h-screen bg-gradient-to-br from-background via-background to-primary/5",children:[e.jsx(E,{}),e.jsx("div",{className:"container mx-auto px-4 py-16 flex items-center justify-center min-h-[calc(100vh-80px)]",children:e.jsxs("div",{className:"relative",children:[e.jsxs("div",{className:"absolute inset-0 -m-8",children:[e.jsx("div",{className:"absolute inset-0 rounded-full border-2 border-primary/20 animate-ping",style:{animationDuration:"2s"}}),e.jsx("div",{className:"absolute inset-4 rounded-full border-2 border-primary/30 animate-ping",style:{animationDuration:"2s",animationDelay:"0.3s"}})]}),e.jsxs("div",{className:"bg-card/80 backdrop-blur-xl border border-border/50 rounded-3xl p-12 max-w-lg mx-auto text-center shadow-2xl relative",children:[e.jsx("div",{className:"absolute inset-0 rounded-3xl bg-gradient-to-br from-primary/10 to-transparent pointer-events-none"}),e.jsxs("div",{className:"relative",children:[e.jsxs("div",{className:"relative inline-block mb-8",children:[e.jsx(W,{className:"w-20 h-20 text-primary animate-spin"}),e.jsx("div",{className:"absolute inset-0 w-20 h-20 rounded-full bg-primary/20 blur-xl"})]}),e.jsx("h1",{className:"text-3xl md:text-4xl font-bold mb-4 bg-gradient-to-r from-foreground to-foreground
1/70 bg-clip-text",children:"Processing Your Purchase"}),e.jsx("p",{className:"text-lg text-muted-foreground",children:"Please wait while we activate your purchase..."}),e.jsx("div",{className:"flex justify-center gap-2 mt-8",children:[0,1,2].map(n=>e.jsx("div",{className:"w-2 h-2 rounded-full bg-primary animate-bounce",style:{animationDelay:`${n*.15}s`}},n))})]})]})]})})]}):Y?e.jsxs("div",{className:"min-h-screen bg-gradient-to-br from-background via-background to-destructive/5",children:[e.jsx(E,{}),e.jsx("div",{className:"container mx-auto px-4 py-16 flex items-center justify-center min-h-[calc(100vh-80px)]",children:e.jsxs("div",{className:"bg-card/80 backdrop-blur-xl border border-border/50 rounded-3xl p-12 max-w-lg mx-auto text-center shadow-2xl relative overflow-hidden",children:[e.jsx("div",{className:"absolute inset-0 opacity-5",children:e.jsx("div",{className:"absolute top-0 right-0 w-64 h-64 bg-destructive rounded-full blur-3xl -translate-y-1/2 translate-x-1/2"})}),e.jsxs("div",{className:"relative",children:[e.jsx("div",{className:"relative inline-block mb-6",children:e.jsx("div",{className:"w-20 h-20 rounded-full bg-destructive/10 flex items-center justify-center mx-auto",children:e.jsx(ie,{className:"w-10 h-10 text-destructive"})})}),e.jsx("h1",{className:"text-3xl md:text-4xl font-bold mb-4",children:"Activation Error"}),e.jsx("p",{className:"text-muted-foreground mb-8 leading-relaxed",children:Y}),e.jsxs("div",{className:"flex flex-col sm:flex-row gap-3 justify-center",children:[e.jsx(b,{onClick:()=>y("/dashboard"),className:"bg-foreground text-background hover:bg-foreground
1/90",children:"Go to Dashboard"}),e.jsx(b,{variant:"outline",onClick:()=>window.location.reload(),children:"Try Again"})]})]})]})})]}):e.jsxs("div",{className:"min-h-screen bg-gradient-to-br from-background via-background to-primary/5 relative overflow-hidden",children:[U&&e.jsx(ae,{width:z.width,height:z.height,recycle:!1,numberOfPieces:300,gravity:.1,colors:["#3b82f6","#8b5cf6","#ec4899","#f59e0b","#10b981"],onConfettiComplete:()=>R(!1)}),e.jsx(E,{}),e.jsxs("div",{className:"hidden md:block absolute inset-0 overflow-hidden pointer-events-none",children:[e.jsx("div",{className:"absolute top-20 left-10 w-72 h-72 bg-primary/10 rounded-full blur-3xl animate-pulse",style:{animationDuration:"4s"}}),e.jsx("div",{className:"absolute bottom-20 right-10 w-96 h-96 bg-primary/5 rounded-full blur-3xl animate-pulse",style:{animationDuration:"6s"}}),e.jsx("div",{className:"absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-[800px] h-[800px] bg-gradient-radial from-primary/5 to-transparent rounded-full"})]}),e.jsx("div",{className:"container mx-auto px-4 py-16 flex items-center justify-center min-h-[calc(100vh-80px)] relative",children:e.jsxs("div",{className:"w-full max-w-2xl",children:[e.jsxs("div",{className:"bg-card/80 backdrop-blur-xl border border-border/50 rounded-3xl p-8 md:p-12 shadow-2xl relative overflow-hidden animate-fade-in",children:[e.jsx("div",{className:"absolute inset-0 bg-gradient-to-r from-transparent via-white/5 to-transparent -translate-x-full animate-[shimmer_3s_infinite]"}),e.jsx("div",{className:"absolute top-0 left-0 right-0 h-1 bg-gradient-to-r from-primary via-primary/80 to-primary"}),e.jsxs("div",{className:"relative text-center",children:[e.jsxs("div",{className:"relative inline-block mb-8",children:[e.jsx("div",{className:"absolute inset-0 -m-4",children:e.jsx("div",{className:"absolute inset-0 rounded-full border-2 border-primary/30 animate-ping",style:{animationDuration:"2s"}})}),e.jsx(O,{className:"absolute -top-2 -right-2 w-6 h-6 text-amber-400 animate-pulse"}),e.jsx(O,{className:"absolute -bottom-1 -left-3 w-5 h-5 text-primary animate-pulse",style:{animationDelay:"0.5s"}}),e.jsx("div",{className:"relative w-24 h-24 rounded-full bg-gradient-to-br from-primary to-primary/80 flex items-center justify-center shadow-lg shadow-primary/30",children:e.jsx(oe,{className:"w-14 h-14 text-primary-foreground",strokeWidth:2.5})}),e.jsx("div",{className:"absolute inset-0 w-24 h-24 rounded-full bg-primary/30 blur-xl -z-10"})]}),e.jsx("h1",{className:"text-4xl md:text-5xl font-bold mb-4 bg-gradient-to-r from-foreground via-foreground to-foreground/80 bg-clip-text",children:"Payment Successful!"}),e.jsxs("div",{className:"flex items-center justify-center gap-2 mb-6",children:[g?e.jsx(ee,{className:"w-5 h-5 text-amber-500"}):e.jsx(le,{className:"w-5 h-5 text-primary"}),e.jsx("p",{className:"text-xl text-muted-foreground",children:g?"Welcome to ADaSci! Your membership is now active.":"You're now enrolled! Start learning today."})]}),e.jsx("p",{className:"text-muted-foreground mb-10 max-w-md mx-auto leading-relaxed",children:g?"You can manage your subscription, update payment methods, and view invoices anytime from your dashboard.":`Access your ${Q} content from your dashboard.`}),v>0&&e.jsxs("div",{className:"mb-10 bg-primary/5 border border-primary/20 rounded-2xl p-6 text-left relative overflow-hidden group hover:border-primary/40 transition-colors",children:[e.jsx("div",{className:"absolute inset-0 bg-gradient-to-br from-primary/5 to-transparent opacity-0 group-hover:opacity-100 transition-opacity"}),e.jsx("div",{className:"relative",children:e.jsxs("div",{className:"flex items-start gap-4",children:[e.jsx("div",{className:"w-12 h-12 rounded-xl bg-primary/10 flex items-center justify-center flex-shrink-0",children:e.jsx(te,{className:"w-6 h-6 text-primary"})}),e.jsxs("div",{className:"flex-1",children:[e.jsx("h3",{className:"text-lg font-semibold text-primary mb-1",children:"Complete Your Order"}),e.jsxs("p",{className:"text-sm text-muted-foreground mb-4",children:["You have ",v," item",v>1?"s":""," remaining in your cart. As a new member, you'll now get your member discount applied!"]}),e.jsx(b,{onClick:G,disabled:q,className:"bg-foreground text-background hover:bg-foreground
1/90 group",children:q?e.jsxs(e.Fragment,{children:[e.jsx(W,{className:"mr-2 h-4 w-4 animate-spin"}),"Restoring Cart..."]}):e.jsxs(e.Fragment,{children:["Continue Checkout",e.jsx(se,{className:"ml-2 h-4 w-4 group-hover:translate-x-1 transition-transform"})]})})]})]})})]}),e.jsxs("div",{className:"flex flex-col sm:flex-row gap-4 justify-center",children:[e.jsx(b,{onClick:()=>y("/dashboard"),size:"lg",className:"bg-foreground text-background hover:bg-foreground/90 px-8 py-6 text-base font-medium rounded-xl shadow-lg hover:shadow-xl transition-all",children:"View Dashboard"}),v===0&&e.jsx(b,{variant:"outline",size:"lg",onClick:()=>y(g?"/courses":"/memberships"),className:"px-8 py-6 text-base font-medium rounded-xl border-2 hover:bg-muted/50 transition-all",children:g?"Browse Courses":"Browse Memberships"})]})]})]}),e.jsx("div",{className:"mt-6 text-center",children:e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Need help? ",e.jsx("a",{href:"/contact",className:"text-primary hover:underline",children:"Contact our support team"})]})})]})})]})},be=ce;export{be 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.