1import{r as n,g as ae,j as e,C as V,y as L,z as B,I as G,a as O,B as N,a5 as re,L as D,N as K,s as m,b as le,u as ce,K as de,a2 as me,ad as X,M as ue,P as $,a7 as xe,aV as Z,h as pe,ae as he}from"./index-DO0ZNUzu.js";import{T as te}from"./textarea-CcV1k9Cd.js";import{S as ge,a as fe,b as je,c as ye,d as A}from"./select-Tn3h6yZZ.js";import{T as ve,a as we,b as ee,c as T,d as Ne,e as U}from"./table-CnjIDQIw.js";import{U as be}from"./upload-5tqj8CNI.js";import{H as Se}from"./house-CUUNLlcb.js";import{M as se}from"./mail-rAjwM4rG.js";import{S as ke}from"./search-DfJrq0Pb.js";import{T as Ee}from"./trash-2-VtFXnL4x.js";import"./check-Cyw8qn0D.js";const Ce=()=>{const[b,F]=n.useState(null);n.useState("30");const[g,P]=n.useState(""),[l,k]=n.useState(!1),[E,f]=n.useState([]),{toast:C}=ae(),R=()=>{const a=`email,days,reason 2[email protected],30,Beta tester reward 3[email protected],90,Special promotion`,c=new Blob([a],{type:"text/csv"}),t=window.URL.createObjectURL(c),u=document.createElement("a");u.href=t,u.download="bulk_premium_access_template.csv",u.click(),window.URL.revokeObjectURL(t)},W=a=>{const c=a.split(` 4`).filter(x=>x.trim()),t=c[0].split(",").map(x=>x.trim().toLowerCase()),u=t.indexOf("email"),i=t.indexOf("days"),j=t.indexOf("reason");if(u===-1)throw new Error("CSV must have 'email' column");return c.slice(1).map(x=>{const p=x.split(",").map(h=>h.trim());return{email:p[u],days:i!==-1&&parseInt(p[i])||30,reason:j!==-1?p[j]:""}})},H=async()=>{if(!b){C({title:"No file selected",description:"Please select a CSV file to upload",variant:"destructive"});return}k(!0),f([]);try{const a=await b.text(),c=W(a),t=[];for(const i of c)try{const{data:j,error:x}=await m.auth.admin.listUsers();if(x)throw x;const p=j.users.find(_=>_.email===i.email);if(!p){t.push({email:i.email,success:!1,message:"User not found"});continue}const h=new Date;h.setDate(h.getDate()+i.days);const{data:{user:y}}=await m.auth.getUser();if(!y){t.push({email:i.email,success:!1,message:"Not authenticated"});continue}const{error:I}=await m.from("temporary_premium_access").insert({user_id:p.id,granted_by:y.id,expires_at:h.toISOString(),reason:i.reason||g||"Bulk premium access grant"});if(I){t.push({email:i.email,success:!1,message:I.message});continue}try{await m.functions.invoke("send-premium-access-email",{body:{userId:p.id,expiresAt:h.toISOString(),reason:i.reason||g||"Bulk premium access grant"}})}catch(_){console.error("Email notification failed:",_)}t.push({email:i.email,success:!0,message:`Access granted for ${i.days} days`})}catch(j){t.push({email:i.email,success:!1,message:j.message||"Unknown error"})}f(t);const u=t.filter(i=>i.success).length;C({title:"Bulk Access Processing Complete",description:`Successfully granted access to ${u} out of ${t.length} users`})}catch(a){console.error("Error processing CSV:",a),C({title:"Error",description:a.message||"Failed to process CSV file",variant:"destructive"})}finally{k(!1)}};return e.jsxs(V,{children:[e.jsxs(L,{children:[e.jsx(B,{children:"Bulk Premium Access Management"}),e.jsx(G,{children:"Grant premium access to multiple users via CSV upload"})]}),e.jsxs(O,{className:"space-y-6",children:[e.jsx("div",{className:"flex items-center gap-4",children:e.jsxs(N,{variant:"outline",onClick:R,className:"gap-2",children:[e.jsx(re,{className:"h-4 w-4"}),"Download CSV Template"]})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(D,{htmlFor:"csv-file",children:"Upload CSV File"}),e.jsx(K,{id:"csv-file",type:"file",accept:".csv",onChange:a=>{var c;return F(((c=a.target.files)==null?void 0:c[0])||null)}}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"CSV should contain columns: email (required), days (optional, default 30), reason (optional)"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(D,{htmlFor:"default-reason",children:"Default Reason (Optional)"}),e.jsx(te,{id:"default-reason",placeholder:"Used if reason is not specified in CSV",value:g,onChange:a=>P(a.target.value),rows:2})]}),e.jsxs(N,{onClick:H,disabled:!b||l,className:"w-full gap-2",children:[e.jsx(be,{className:"h-4 w-4"}),l?"Processing...":"Upload and Grant Access"]})]}),E.length>0&&e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium",children:"Results"}),e.jsx("div",{className:"max-h-60 overflow-y-auto space-y-1 rounded-md border p-3",children:E.map((a,c)=>e.jsxs("div",{className:`text-sm p-2 rounded ${a.success?"bg-green-50 dark:bg-green-950 text-green-900 dark:text-green-100":"bg-red-50 dark:bg-red-950 text-red-900 dark:text-red-100"}`,children:[e.jsx("span",{className:"font-medium",children:a.email}),": ",a.message]},c))})]})]})]})},Le=()=>{const b=le(),{user:F}=ce(),{isAdmin:g,loading:P}=de(),{toast:l}=ae(),[k,E]=n.useState(""),[f,C]=n.useState(null),[R,W]=n.useState("7"),[H,a]=n.useState(""),[c,t]=n.useState(!1),[u,i]=n.useState([]),[j,x]=n.useState(!0),[p,h]=n.useState([]),[y,I]=n.useState(""),[_,Y]=n.useState(!1);n.useEffect(()=>{!P&&!g&&(b("/admin/dashboard"),l({title:"Access Denied",description:"You must be an admin to access this page",variant:"destructive"}))},[g,P,b]),n.useEffect(()=>{g&&q()},[g]);const q=async()=>{x(!0);const{data:s,error:r}=await m.from("temporary_premium_access").select("*").order("expires_at",{ascending:!1});if(!r&&s){const v=s.map(o=>
4o.user_id),{data:d}=await m.from("profiles").select("user_id, display_name").in("user_id",v),M=s.map(o=>({...o,profiles:(d==null?void 0:d.find(w=>w.user_id===o.user_id))||{display_name:null}}));i(M)}x(!1)},J=async()=>{if(!k.trim())return;const{data:s,error:r}=await m.from("profiles").select("user_id, display_name").ilike("display_name",`%${k}%`).limit(10);!r&&s&&h(s)},ie=async()=>{if(!f||!R){l({title:"Error",description:"Please select a user and duration",variant:"destructive"});return}t(!0);const s=new Date;s.setDate(s.getDate()+parseInt(R));try{const{data:r}=await m.from("profiles").select("user_id, display_name").eq("user_id",f).single();let v=(r==null?void 0:r.display_name)||null;const{error:d}=await m.from("temporary_premium_access").insert({user_id:f,granted_by:F==null?void 0:F.id,expires_at:s.toISOString(),reason:H||null});if(d)throw d;l({title:"Success",description:"Temporary premium access granted"}),C(null),E(""),a(""),h([]),q()}catch(r){console.error("Error granting access:",r),l({title:"Error",description:r.message||"Failed to grant temporary access",variant:"destructive"})}finally{t(!1)}},ne=async s=>{const{error:r}=await m.from("temporary_premium_access").delete().eq("id",s);r?l({title:"Error",description:"Failed to revoke access",variant:"destructive"}):(l({title:"Success",description:"Temporary access revoked"}),q())},Q=async()=>{var r,v,d,M;if(!y.trim()){l({title:"Error",description:"Please enter an email address",variant:"destructive"});return}if(!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(y)){l({title:"Error",description:"Please enter a valid email address",variant:"destructive"});return}Y(!0);try{const{data:o,error:w}=await m.auth.getSession();if(w||!o.session){console.error("No valid session found:",w),l({title:"Authentication Error",description:"Please log out and log back in, then try again.",variant:"destructive"});return}console.log("Calling export-ebook-email function with valid session");const{data:S,error:z}=await m.functions.invoke("export-ebook-email",{body:{email:y}});if(z)throw console.error("Function invocation error:",z),z;console.log("Function response:",S),l({title:"â
Email Sent Successfully!",description:e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{children:"E-book and workbook have been sent to:"}),e.jsx("p",{className:"font-semibold",children:y}),e.jsx("p",{className:"text-xs text-muted-foreground mt-2",children:"Check your inbox for the HTML files. Open them in a browser and print to PDF."})]})}),I("")}catch(o){console.error("Error exporting e-book:",o);let w="â Email Send Failed",S="Failed to send the e-book export. Please try again.";(r=o.message)!=null&&r.includes("Admin or owner role required")?S="Access denied. You need admin or owner role to export content.":(v=o.message)!=null&&v.includes("Unauthorized")||(d=o.message)!=null&&d.includes("Auth")?(w="ð Authentication Error",S="Please log out and log back in, then try again."):(M=o.message)!=null&&M.includes("email")?(w="ð§ Invalid Email",S="Please check the email address and try again."):o.message&&(S=o.message),l({title:w,description:e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{children:S}),e.jsxs("p",{className:"text-xs text-muted-foreground mt-2",children:["Error details: ",o.message||"Unknown error"]})]}),variant:"destructive"})}finally{Y(!1)}};if(P||j)return e.jsx(me,{});if(!g)return null;const oe=[{name:"Unlimited Affirmations",icon:$,description:"No daily limits on affirmation mirror"},{name:"Unlimited Voice Notes",icon:$,description:"Record as many voice notes as you want"},{name:"Full Sisterhood Circles Access",icon:he,description:"Join all circles without subscription"},{name:"Unlimited Vision Board Images",icon:$,description:"Add up to 999 images to vision boards"},{name:"Vision Board Sharing",icon:Z,description:"Download and share vision boards"},{name:"Grant Temporary Access",icon:X,description:"Give users premium features for limited time"}];return e.jsx("div",{className:"min-h-screen bg-background",children:e.jsxs("div",{className:"container mx-auto px-4 py-8 max-w-6xl",children:[e.jsxs("div",{className:"flex items-center justify-between mb-6",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(X,{className:"h-8 w-8 text-primary"}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold text-foreground",children:"Admin Settings"}),e.jsx("p",{className:"text-muted-foreground",children:"Manage your admin privileges and grant access"})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ue,{}),e.jsx(N,{variant:"ghost",onClick:()=>
4b("/admin/dashboard"),className:"rounded-full",children:e.jsx(Se,{className:"h-5 w-5"})})]})]}),e.jsxs(V,{className:"mb-6",children:[e.jsxs(L,{children:[e.jsxs(B,{className:"flex items-center gap-2",children:[e.jsx($,{className:"h-5 w-5 text-primary"}),"Your Admin Features"]}),e.jsx(G,{children:"As an admin, you have full access to all premium features at no cost"})]}),e.jsx(O,{children:e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:oe.map(s=>e.jsxs("div",{className:"flex items-start gap-3 p-3 rounded-lg border bg-card",children:[e.jsx(s.icon,{className:"h-5 w-5 text-primary mt-1"}),e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-sm",children:s.name}),e.jsx("p",{className:"text-xs text-muted-foreground",children:s.description})]})]},s.name))})})]}),e.jsxs(V,{className:"mb-6 border-primary/20 bg-gradient-to-br from-primary/5 to-secondary/5",children:[e.jsxs(L,{children:[e.jsxs(B,{className:"flex items-center gap-2",children:[e.jsx(se,{className:"h-5 w-5 text-primary"}),"Export E-book & Workbook"]}),e.jsx(G,{children:"Send the HERPower E-book and Workbook to any email address (HTML format - convert to PDF by opening in browser and printing)"})]}),e.jsxs(O,{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center gap-2 p-3 bg-primary/10 rounded-lg border border-primary/20",children:[e.jsx(xe,{className:"h-5 w-5 text-primary flex-shrink-0"}),e.jsxs("div",{className:"text-sm",children:[e.jsx("p",{className:"font-medium",children:"What you'll receive:"}),e.jsxs("ul",{className:"list-disc list-inside text-muted-foreground mt-1",children:[e.jsx("li",{children:"HERPower-Ebook.html (All 13 chapters)"}),e.jsx("li",{children:"HERPower-Workbook.html (Companion workbook with exercises)"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(D,{htmlFor:"export-email",children:"Email Address"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(K,{id:"export-email",type:"email",placeholder:"[email protected]",value:y,onChange:s=>I(s.target.value),onKeyDown:s=>s.key==="Enter"&&Q(),className:"flex-1"}),e.jsx(N,{onClick:Q,disabled:_,className:"min-w-[140px]",children:_?e.jsxs(e.Fragment,{children:[e.jsx(re,{className:"h-4 w-4 mr-2 animate-bounce"}),"Sending..."]}):e.jsxs(e.Fragment,{children:[e.jsx(se,{className:"h-4 w-4 mr-2"}),"Send Export"]})})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"ð¡ Tip: Open the HTML files in a browser and use Print â Save as PDF to create PDF versions"})]})]})]}),e.jsxs(V,{className:"mb-6",children:[e.jsxs(L,{children:[e.jsxs(B,{className:"flex items-center gap-2",children:[e.jsx(Z,{className:"h-5 w-5 text-primary"}),"Grant Temporary Premium Access"]}),e.jsx(G,{children:"Give users premium features for a limited time period"})]}),e.jsxs(O,{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(D,{children:"Search User by Name"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(K,{placeholder:"Enter user name...",value:k,onChange:s=>E(s.target.value),onKeyDown:s=>s.key==="Enter"&&J()}),e.jsx(N,{onClick:J,variant:"secondary",children:e.jsx(ke,{className:"h-4 w-4"})})]})]}),p.length>0&&e.jsx("div",{className:"border rounded-lg p-2 space-y-1",children:p.map(s=>e.jsx(N,{variant:f===s.user_id?"default":"ghost",className:"w-full justify-start",onClick:()=>{C(s.user_id),E(s.display_name||""),h([])},children:s.display_name||"Unnamed User"},s.user_id))}),f&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(D,{children:"Duration"}),e.jsxs(ge,{value:R,onValueChange:W,children:[e.jsx(fe,{children:e.jsx(je,{})}),e.jsxs(ye,{children:[e.jsx(A,{value:"1",children:"1 day"}),e.jsx(A,{value:"3",children:"3 days"}),e.jsx(A,{value:"7",children:"7 days"}),e.jsx(A,{value:"14",children:"14 days"}),e.jsx(A,{value:"30",children:"30 days"}),e.jsx(A,{value:"90",children:"90 days"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(D,{children:"Reason (Optional)"}),e.jsx(te,{placeholder:"Why are you granting this access?",value:H,onChange:s=>a(s.target.value),rows:2})]}
4),e.jsx(N,{onClick:ie,disabled:c,className:"w-full",children:c?"Granting...":"Grant Access"})]})]})]}),e.jsxs(V,{children:[e.jsxs(L,{children:[e.jsx(B,{children:"Active Temporary Access"}),e.jsx(G,{children:"Users who currently have temporary premium access"})]}),e.jsx(O,{children:u.length===0?e.jsx("p",{className:"text-center text-muted-foreground py-8",children:"No active temporary access grants"}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(ve,{children:[e.jsx(we,{children:e.jsxs(ee,{children:[e.jsx(T,{children:"User"}),e.jsx(T,{children:"Granted"}),e.jsx(T,{children:"Expires"}),e.jsx(T,{children:"Status"}),e.jsx(T,{children:"Reason"}),e.jsx(T,{className:"text-right",children:"Actions"})]})}),e.jsx(Ne,{children:u.map(s=>{var d;const r=new Date(s.expires_at)<new Date,v=new Date(s.expires_at).getTime()-Date.now()<24*60*60*1e3;return e.jsxs(ee,{children:[e.jsx(U,{className:"font-medium",children:((d=s.profiles)==null?void 0:d.display_name)||"Unknown"}),e.jsx(U,{children:new Date(s.granted_at).toLocaleDateString()}),e.jsx(U,{children:new Date(s.expires_at).toLocaleDateString()}),e.jsx(U,{children:e.jsx(pe,{variant:r?"destructive":v?"secondary":"default",children:r?"Expired":v?"Expiring Soon":"Active"})}),e.jsx(U,{className:"max-w-xs truncate",children:s.reason||"â"}),e.jsx(U,{className:"text-right",children:!r&&e.jsx(N,{variant:"ghost",size:"sm",onClick:()=>ne(s.id),children:e.jsx(Ee,{className:"h-4 w-4"})})})]},s.id)})})]})})})]}),e.jsx(Ce,{})]})})};export{Le 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.