1import{r as y,u as p,j as e,f as M}from"./vendor-react-CVJUvMij.js";import{C as g,a as j,c as f,b,d as w,B as q,S as I,q as W,r as B,v as O,w as t,e as H,s as i,V as _}from"./index-ClC5K_zs.js";import{u as V,A as Y}from"./AdminPageSurface-Qo3kits2.js";import{A as c}from"./AdminWorkspacePrimitives-DAALZGAm.js";import{T as $,a as K,b as v,c as d,d as z,e as o}from"./table-CiXgM3Gy.js";import{A as G,b as Q,a as J}from"./alert-fCyHpM7i.js";import{ac as U,aA as X,a3 as N,e as Z,i as S,O as x,R as ee}from"./vendor-icons-CnYTiz2w.js";import"./vendor-supabase-CBi3gvNh.js";import"./vendor-motion-DqVKfPoP.js";import"./vendor-charts-DEZ1HK4T.js";const ae={welcome:"Welcome",payment_receipt:"Payment Receipt",payment_failed:"Payment Failed",expiry_warning:"Expiry Warning",cancellation:"Cancellation"},se={welcome:"default",payment_receipt:"secondary",payment_failed:"destructive",expiry_warning:"outline",cancellation:"secondary"};function he(){const k=V({adminOnly:!0}),[n,E]=y.useState("all"),[m,h]=y.useState(!1),{data:u,isLoading:A,refetch:T}=p({queryKey:["admin","subscriber-email-log",n],queryFn:async()=>{let a=i.from("subscriber_email_log").select("*").order("sent_at",{ascending:!1}).limit(100);n!=="all"&&(a=a.eq("email_type",n));const{data:s,error:r}=await a;if(r)throw r;return s||[]}}),{data:l}=p({queryKey:["admin","subscriber-email-stats"],queryFn:async()=>{const a=new Date,s=new Date(a.getFullYear(),a.getMonth(),a.getDate()).toISOString(),r=new Date(a.getTime()-10080*60*1e3).toISOString(),R=new Date(a.getFullYear(),a.getMonth(),1).toISOString(),[P,F,D,L]=await Promise.all([i.from("subscriber_email_log").select("id",{count:"exact",head:!0}).gte("sent_at",s),i.from("subscriber_email_log").select("id",{count:"exact",head:!0}).gte("sent_at",r),i.from("subscriber_email_log").select("id",{count:"exact",head:!0}).gte("sent_at",R),i.from("subscriber_email_log").select("id",{count:"exact",head:!0}).eq("email_type","payment_failed").gte("sent_at",r)]);return{today:P.count||0,week:F.count||0,month:D.count||0,failedAlerts:L.count||0}}}),C=async()=>{h(!0);try{const{data:a,error:s}=await i.functions.invoke("subscriber-lifecycle-emails",{body:{mode:"check_expiring"},headers:{"x-agent-runner-key":"manual-admin-trigger"}});if(s)throw s;_.success(`Expiry check complete: ${a?.checked||0} checked, ${a?.sent||0} sent`),T()}catch(a){_.error("Failed to run expiry check: "+(a instanceof Error?a.message:String(a)))}finally{h(!1)}};return e.jsx(Y,{access:k,children:e.jsxs("div",{className:"space-y-6",children:[e.jsx("h1",{className:"text-2xl font-bold",children:"Subscriber Lifecycle Emails"}),e.jsx("p",{className:"text-muted-foreground",children:"Transactional email audit trail and management."}),e.jsxs(G,{variant:"default",className:"border-primary/30 bg-primary/5",children:[e.jsx(U,{className:"h-4 w-4"}),e.jsx(Q,{children:"Email Domain Not Configured"}),e.jsxs(J,{children:["Transactional email delivery requires an email domain to be set up. Currently, emails are ",e.jsx("strong",{children:"logged"})," to the audit trail but ",e.jsx("strong",{children:"not delivered"})," to recipients. To enable actual email delivery, a workspace admin needs to configure an email domain in ",e.jsx("strong",{children:"Workspace Settings â Email Domains"}),". Once configured, all lifecycle emails (welcome, dunning, expiry warnings) will be sent automatically."]})]}),e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4",children:[e.jsx(c,{label:"Sent Today",value:l?.today??0,detail:"Emails sent today",icon:X}),e.jsx(c,{label:"This Week",value:l?.week??0,detail:"Emails this week",icon:N}),e.jsx(c,{label:"This Month",value:l?.month??0,detail:"Emails this month",icon:Z}),e.jsx(c,{label:"Failed Alerts",value:l?.failedAlerts??0,detail:"Payment failed alerts this week",icon:S})]}),e.jsxs(g,{children:[e.jsxs(j,{children:[e.jsx(f,{className:"text-lg",children:"Lifecycle Email Triggers"}),e.jsx(b,{children:"Automatically sent based on Stripe webhook events and scheduled expiry checks."})]}),e.jsxs(w,{children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-3 text-sm",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(x,{className:"h-4 w-4 text-primary"})," ",e.jsx("strong",{children:"Welcome"})," â checkout.session.completed"]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(x,{className:"h-4 w-4 text-primary"})," ",e.jsx("strong",{children:"Payment Receipt"})," â invoice.payment_succeeded"]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(S,{className:"h-4 w-4 text-destructive"})," ",e.jsx("strong",{children:"Payment Failed"})," â invoice.payment_failed"]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(N,{className:"h-4 w-4 text-muted-foreground"})," ",e.jsx("strong",{children:"Expiry Warning"})," â 7-day / 1-day before expiration"]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(x,{className:"h-4 w-4 text-muted-foreground"})," ",e.jsx("strong",{children:"Cancellation"})," â customer.subscription.deleted"]})]}),e.jsx("div",{className:"mt-4",children:e.jsxs(q,{variant:"outline",size:"sm",onClick:C,disabled:m,children:[e.jsx(ee,{className:`h-4 w-4 mr-2 ${m?"animate-spin":""}`}),m?"Checking...":"Run Expiry Check Now"]})})]})]}),e.jsxs(g,{children:[e.jsxs(j,{className:"flex flex-row items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(f,{className:"text-lg",children:"Email Log"}),e.jsx(b,{children:"Recent transactional emails sent by the system."})]}),e.jsxs(I,{value:n,onValueChange:E,children:[e.jsx(W,{className:"w-[180px]",children:e.jsx(B,{placeholder:"Filter by type"})}),e.jsxs(O,{children:[e.jsx(t,{value:"all",children:"All Types"}),e.jsx(t,{value:"welcome",children:"Welcome"}),e.jsx(t,{value:"payment_receipt",children:"Payment Receipt"}),e.jsx(t,{value:"payment_failed",children:"Payment Failed"}),e.jsx(t,{value:"expiry_warning",children:"Expiry Warning"}),e.jsx(t,{value:"cancellation",children:"Cancellation"})]})]})]}),e.jsx(w,{children:A?e.jsx("p",{className:"text-muted-foreground text-sm py-8 text-center",children:"Loading email logs..."}):u?.length?e.jsx("div",{className:"overflow-x-auto",children:e.jsxs($,{children:[e.jsx(K,{children:e.jsxs(v,{children:[e.jsx(d,{children:"Type"}),e.jsx(d,{children:"Recipient"}),e.jsx(d,{children:"Product"}),e.jsx(d,{children:"Sent At"})]})}),e.jsx(z,{children:u.map(a=>e.jsxs(v,{children:[e.jsx(o,{children:e.jsx(H,{variant:se[a.email_type]||"secondary",children:ae[a.email_type]||a.email_type})}),e.jsx(o,{className:"font-mono text-xs",children:a.recipient_email}),e.jsx(o,{children:a.product_type||"â"}),e.jsx(o,{className:"text-xs text-muted-foreground",children:a.sent_at?M(new Date(a.sent_at),"MMM d, yyyy HH:mm"):"â"})]},a.id))})]})}):e.jsx("p",{className:"text-muted-foreground text-sm py-8 text-center",children:"No emails sent yet. Emails will appear here once triggered by Stripe webhooks or expiry checks."})})]})]})})}export{he 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.