1import{j as e,b as W,u as E,c as M}from"./vendor-query-N4gzKaQq.js";import{r as d}from"./vendor-react-BGLQqgNE.js";import{b as S}from"./formatters-TgFxyaOf.js";import{c as F,r as C,X as T,I as Y,t as ee,m as se,s as f,n as R,B as A}from"./index-Cxf7uIkR.js";import{w as te,O,x as z,y as re,z as I,B as K,E as ae}from"./vendor-radix-CsiCqlt2.js";import{T as ne}from"./textarea-DI9uere5.js";import{I as oe}from"./input-BUUUSwA4.js";import{M as q}from"./mail-7Yj-ZZC6.js";import{P as $}from"./phone-n9wAuwYk.js";import{C as B}from"./clock-Dz-0DwIN.js";import{S as ie}from"./send-DpxfYU9J.js";import{T as ce}from"./trash-2-B0QzBVNJ.js";import{D as U}from"./download-CzWfU3vX.js";import{F as de}from"./filter-DlgZww7Y.js";import"./vendor-supabase-CYzOsYot.js";/** 2 * @license lucide-react v0.462.0 - ISC 3 * 4 * This source code is licensed under the ISC license. 5 * See the LICENSE file in the root directory of this source tree. 6 */const le=F("MessageSquare",[["path",{d:"M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z",key:"1lielz"}]]);/** 7 * @license lucide-react v0.462.0 - ISC 8 * 9 * This source code is licensed under the ISC license. 10 * See the LICENSE file in the root directory of this source tree. 11 */const me=F("Newspaper",[["path",{d:"M4 22h16a2 2 0 0 0 2-2V4a2 2 0 0 0-2-2H8a2 2 0 0 0-2 2v16a2 2 0 0 1-2 2Zm0 0a2 2 0 0 1-2-2v-9c0-1.1.9-2 2-2h2",key:"7pis2x"}],["path",{d:"M18 14h-8",key:"sponae"}],["path",{d:"M15 18h-5",key:"95g1m2"}],["path",{d:"M10 6h8v4h-8V6Z",key:"smlsk5"}]]);/** 12 * @license lucide-react v0.462.0 - ISC 13 * 14 * This source code is licensed under the ISC license. 15 * See the LICENSE file in the root directory of this source tree. 16 */const Q=F("User",[["path",{d:"M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2",key:"975kel"}],["circle",{cx:"12",cy:"7",r:"4",key:"17ys0d"}]]),ue=te,xe=ae,V=d.forwardRef(({className:s,...o},i)=>e.jsx(O,{className:C("fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",s),...o,ref:i}));V.displayName=O.displayName;const pe=Y("fixed z-50 gap-4 bg-background p-6 shadow-lg transition ease-in-out data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:duration-300 data-[state=open]:duration-500",{variants:{side:{top:"inset-x-0 top-0 border-b data-[state=closed]:slide-out-to-top data-[state=open]:slide-in-from-top",bottom:"inset-x-0 bottom-0 border-t data-[state=closed]:slide-out-to-bottom data-[state=open]:slide-in-from-bottom",left:"inset-y-0 left-0 h-full w-3/4 border-r data-[state=closed]:slide-out-to-left data-[state=open]:slide-in-from-left sm:max-w-sm",right:"inset-y-0 right-0 h-full w-3/4 border-l data-[state=closed]:slide-out-to-right data-[state=open]:slide-in-from-right sm:max-w-sm"}},defaultVariants:{side:"right"}}),H=d.forwardRef(({side:s="right",className:o,children:i,...h},m)=>e.jsxs(xe,{children:[e.jsx(V,{}),e.jsxs(z,{ref:m,className:C(pe({side:s}),o),...h,children:[i,e.jsxs(re,{className:"absolute right-4 top-4 roun
16ded-sm opacity-70 ring-offset-background transition-opacity data-[state=open]:bg-secondary hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none",children:[e.jsx(T,{className:"h-4 w-4"}),e.jsx("span",{className:"sr-only",children:"Close"})]})]})]}));H.displayName=z.displayName;const Z=({className:s,...o})=>e.jsx("div",{className:C("flex flex-col space-y-2 text-center sm:text-left",s),...o});Z.displayName="SheetHeader";const G=d.forwardRef(({className:s,...o},i)=>e.jsx(I,{ref:i,className:C("text-lg font-semibold text-foreground",s),...o}));G.displayName=I.displayName;const ge=d.forwardRef(({className:s,...o},i)=>e.jsx(K,{ref:i,className:C("text-sm text-muted-foreground",s),...o}));ge.displayName=K.displayName;function fe({lead:s,open:o,onOpenChange:i}){const{user:h}=ee(),{toast:m}=se(),g=W(),[x,b]=d.useState(""),[c,j]=d.useState("user"),[p,N]=d.useState(""),[w,u]=d.useState("");d.useEffect(()=>{s&&(s.assigned_user_id?(j("user"),u(s.assigned_user_id),N("")):s.assigned_name?(j("custom"),N(s.assigned_name),u("")):(j("user"),u(""),N("")))},[s?.id]);const{data:v=[]}=E({queryKey:["admin-user-roles-for-assign"],queryFn:async()=>{const{data:r,error:l}=await f.functions.invoke("manage-admin-users",{body:{action:"list"}});if(l)throw l;return r?.users||[]},enabled:o,staleTime:5*6e4}),{data:k=[],isLoading:t}=E({queryKey:["lead-notes",s?.id],queryFn:async()=>{if(!s)return[];const{data:r,error:l}=await f.from("lead_notes").select("*").eq("lead_id",s.id).order("created_at",{ascending:!1});if(l)throw l;return r??[]},enabled:o&&!!s?.id}),n=d.useMemo(()=>{if(!s)return null;if(s.assigned_user_id){const r=v.find(l=>l.user_id===s.assigned_user_id);return r?.display_name||r?.email||"Assigned user"}return s.assigned_name||null},[s,v]),a=M({mutationFn:async()=>{if(!s)throw new Error("No lead");const r=c==="user"&&w?w:null,l=c==="custom"&&p.trim()?p.trim():null,J={assigned_user_id:r,assigned_name:l,assigned_at:r||l?new Date().toISOString():null},{error:P}=await f.from("leads").update(J).eq("id",s.id);if(P)throw P;if(r&&r!==s.assigned_user_id)try{const{error:L}=await f.functions.invoke("notify-lead-assignment",{body:{lead_id:s.id,assignee_user_id:r,app_origin:window.location.origin}});L?console.warn("Notification failed",L):m({title:"Assignee notified",description:"Email sent to the assignee."})}catch(L){console.warn("Notification error",L)}},onSuccess:()=>{m({title:"Assignment updated"}),g.invalidateQueries({queryKey:["admin-leads"]})},onError:r=>m({title:"Error",description:r.message,variant:"destructive"})}),y=M({mutationFn:async()=>{if(!s)throw new Error("No lead");const{error:r}=await f.from("leads").update({assigned_user_id:null,assigned_name:null,assigned_at:null}).eq("id",s.id);if(r)throw r},onSuccess:()=>{u(""),N(""),m({title:"Assignment cleared"}),g.invalidateQueries({queryKey:["admin-leads"]})}}),_=M({mutationFn:async()=>{if(!s||!x.trim())throw new Error("Empty note");const{error:r}=await f.from("lead_notes").insert({lead_id:s.id,body:x.trim().slice(0,5e3),author_user_id:h?.id??null,author_email:h?.email??null});if(r)throw r},onSuccess:()=>{b(""),g.invalidateQueries({queryKey:["lead-notes",s?.id]})},onError:r=>m({title:"Error",description:r.message,variant:"destructive"})}),X=M({mutationFn:async r=>{const{error:l}=await f.from("lead_notes").delete().eq("id",r);if(l)throw l},onSuccess:()=>g.invalidateQueries({queryKey:["lead-notes",s?.id]})});return s?e.jsx(ue,{open:o,onOpenChange:i,children:e.jsxs(H,{side:"right",className:"w-full sm:max-w-xl overflow-y-auto p-0",children:[e.jsxs(Z,{className:"px-6 py-5 border-b border-border",children:[e.jsx(G,{className:"font-serif text-2xl text-foreground",children:s.full_name}),e.jsxs("div",{className:"flex flex-wrap gap-x-4 gap-y-1 text-xs text-muted-foreground font-sans pt-1",children:[e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx(q,{className:"h-3 w-3"})," ",s.email]}),s.phone&&e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx($,{className:"h-3 w-3"})," ",R(s.phone)]}),e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx(B,{className:"h-3 w-3"})," ",S(s.created_at)]})]})]}),e.jsxs("div",{className:"px-6 py-5 space-y-6",children:[e.jsxs("section",{className:"space-y-2",children:[s.message&&e.jsx("div",{className:"text-sm font-sans text-foreground
16/90 bg-muted/40 p-3 border-l-2 border-gold whitespace-pre-wrap",children:s.message}),e.jsxs("div",{className:"grid grid-cols-2 gap-x-4 gap-y-1 text-xs font-sans text-muted-foreground pt-2",children:[s.source&&e.jsxs("div",{children:[e.jsx("span",{className:"uppercase tracking-wider text-[10px]",children:"Source"}),e.jsx("div",{className:"text-foreground",children:s.source})]}),s.project_slug&&e.jsxs("div",{children:[e.jsx("span",{className:"uppercase tracking-wider text-[10px]",children:"Collection"}),e.jsx("div",{className:"text-foreground",children:s.project_slug})]}),s.preferred_contact&&e.jsxs("div",{children:[e.jsx("span",{className:"uppercase tracking-wider text-[10px]",children:"Contact"}),e.jsx("div",{className:"text-foreground",children:s.preferred_contact})]}),s.preferred_language&&e.jsxs("div",{children:[e.jsx("span",{className:"uppercase tracking-wider text-[10px]",children:"Language"}),e.jsx("div",{className:"text-foreground",children:s.preferred_language})]}),s.utm_campaign&&e.jsxs("div",{children:[e.jsx("span",{className:"uppercase tracking-wider text-[10px]",children:"UTM Campaign"}),e.jsx("div",{className:"text-foreground",children:s.utm_campaign})]}),s.page_url&&e.jsxs("div",{className:"col-span-2 truncate",children:[e.jsx("span",{className:"uppercase tracking-wider text-[10px]",children:"Page"}),e.jsx("div",{className:"text-foreground truncate",children:s.page_url})]})]})]}),e.jsxs("section",{className:"space-y-3 pt-2 border-t border-border",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("h3",{className:"text-xs uppercase tracking-widest font-sans text-muted-foreground flex items-center gap-2",children:[e.jsx(Q,{className:"h-3.5 w-3.5"})," Assigned to"]}),n&&e.jsxs("button",{onClick:()=>y.mutate(),className:"text-[10px] uppercase tracking-wider font-sans text-muted-foreground hover:text-destructive flex items-center gap-1",children:[e.jsx(T,{className:"h-3 w-3"})," Clear"]})]}),n&&e.jsxs("div",{className:"text-sm font-sans text-foreground bg-accent/5 border border-accent/20 px-3 py-2",children:[n,s.assigned_at&&e.jsxs("span",{className:"text-[10px] text-muted-foreground ml-2",children:["· since ",S(s.assigned_at)]})]}),e.jsxs("div",{className:"flex gap-2 text-[10px] uppercase tracking-wider font-sans",children:[e.jsx("button",{onClick:()=>j("user"),className:`px-2.5 py-1 border ${c==="user"?"border-gold text-foreground":"border-border text-muted-foreground"}`,children:"Admin user"}),e.jsx("button",{onClick:()=>j("custom"),className:`px-2.5 py-1 border ${c==="custom"?"border-gold text-foreground":"border-border text-muted-foreground"}`,children:"Custom name"})]}),c==="user"?e.jsxs("select",{value:w,onChange:r=>u(r.target.value),className:"w-full bg-card border border-border rounded-none px-3 py-2 text-sm font-sans text-foreground",children:[e.jsx("option",{value:"",children:"â Select user â"}),v.map(r=>e.jsxs("option",{value:r.user_id,children:[r.display_name||r.email," (",r.role,")"]},r.user_id))]}):e.jsx(oe,{value:p,onChange:r=>N(r.target.value.slice(0,100)),placeholder:"e.g. Maria â Sales",className:"rounded-none"}),e.jsx(A,{onClick:()=>a.mutate(),disabled:a.isPending||(c==="user"?!w:!p.trim()),size:"sm",className:"rounded-none text-xs uppercase tracking-widest font-sans",children:a.isPending?"Savingâ¦":"Save assignment"})]}),e.jsxs("section",{className:"space-y-3 pt-2 border-t border-border",children:[e.jsxs("h3",{className:"text-xs uppercase tracking-widest font-sans text-muted-foreground flex items-center gap-2",children:[e.jsx(le,{className:"h-3.5 w-3.5"})," Notes (",k.length,")"]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ne,{value:x,onChange:r=>b(r.target.value.slice(0,5e3)),placeholder:"Add a note about this leadâ¦",rows:3,className:"rounded-none font-sans text-sm resize-none"}),e.jsx("div",{className:"flex justify-end",children:e.jsxs(A,{onClick:()=>_.mutate(),disabled:!x.trim()||_.isPending,size:"sm",className:"rounded-none text-xs uppercase tracking-widest font-sans",children:[e.jsx(ie,{className:"h-3 w-3 mr-1.5"}),_.isPending?"Addingâ¦":"Add note"]})})]}),e.jsx("ul",{className:"space-y-2.5",children:t?e.jsx("li",{className:"text-xs text-muted-foreground font-sans animate-pulse",children:"Loading notesâ¦"}):k.length===0?e.jsx("li",{className:"text-xs text-muted-foreground font-sans py-3 text-center border border-dashed border-border",children:"No notes yet"}):k.map(r=>e.jsxs("li",{className:"bg-muted/30 border border-border p-3 group",children:[e.jsxs("div",{className:"flex items-start justify-between gap-2 mb-1.5",children:[e.jsxs("div",{className:"text-[10px] uppercase tracking-wider font-sans text-muted-foreground",children:[r.author_email||"system"," · ",S(r.created_at)]}),e.jsx("button",{onClick:()=>X.mutate(r.id),className:"opacity-0 group-hover:opacity-100 text-muted-foreground hover:text-destructive transition-opacity",title:"Delete note",children:e.jsx(ce,{className:"h-3 w-3"})})]}),e.jsx("p",{className:"text-sm font-sans text-foreground whitespace-pre-wrap",children:r.body})]},r.id))})]})]})]})}):null}function D(s,o,i){const h=[o.join(","),...i.map(b=>b.map(c=>`"${(c??"").replace(/"/g,'""')}"`).join(","))].join(` 17`),m=new Blob([h],{type:"text/csv;charset=utf-8;"}),g=URL.createObjectURL(m),x=document.createElement("a");x.href=g,x.download=s,x.click(),URL.revokeObjectURL(g)}function qe(){const[s,o]=d.useState("leads"),[i,h]=d.useState("all"),[m,g]=d.useState(null),[x,b]=d.useState(!1),{data:c=[],isLoading:j}=E({queryKey:["admin-leads"],queryFn:async()=>{const t=[];let n=0;const a=1e3;for(;;){const{data:y,error:_}=await f.from("leads").select("*").order("created_at",{ascending:!1}).range(n,n+a-1);if(_)throw _;if(!y||y.length===0||(t.push(...y),y.length<a))break;n+=a}return t}}),{data:p=[],isLoading:N}=E({queryKey:["admin-subscribers"],queryFn:async()=>{const{data:t,error:n}=await f.from("newsletter_subscribers").select("*").order("subscribed_at",{ascending:!1});if(n)throw n;return t}}),w=d.useMemo(()=>{const t=new Set;
17return c.forEach(n=>{n.project_slug&&t.add(n.project_slug)}),Array.from(t).sort()},[c]),u=d.useMemo(()=>i==="all"?c:i==="none"?c.filter(t=>!t.project_slug):c.filter(t=>t.project_slug===i),[c,i]),v=()=>{const t=["Full Name","Email","Phone","Message","Source","Collection","Preferred Contact","Language","CRM Status","Date","UTM Source","UTM Medium","UTM Campaign"],n=u.map(a=>[a.full_name,a.email,a.phone??"",a.message??"",a.source??"",a.project_slug??"",a.preferred_contact??"",a.preferred_language??"",a.crm_push_status??"pending",a.created_at,a.utm_source??"",a.utm_medium??"",a.utm_campaign??""]);D(`leads-${new Date().toISOString().slice(0,10)}.csv`,t,n)},k=()=>{const t=["Email","Subscribed At","Unsubscribed At"],n=p.map(a=>[a.email,a.subscribed_at,a.unsubscribed_at??""]);D(`newsletter-subscribers-${new Date().toISOString().slice(0,10)}.csv`,t,n)};return e.jsxs("div",{children:[e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-3 mb-6",children:[e.jsx("h1",{className:"font-serif text-2xl sm:text-4xl text-foreground",children:"Leads"}),e.jsxs("div",{className:"flex items-center gap-2",children:[s==="leads"&&u.length>0&&e.jsxs(A,{variant:"outline",size:"sm",onClick:v,className:"rounded-none text-xs uppercase tracking-widest font-sans",children:[e.jsx(U,{className:"h-4 w-4 mr-1"})," Export"]}),s==="subscribers"&&p.length>0&&e.jsxs(A,{variant:"outline",size:"sm",onClick:k,className:"rounded-none text-xs uppercase tracking-widest font-sans",children:[e.jsx(U,{className:"h-4 w-4 mr-1"})," Export"]})]})]}),e.jsxs("div",{className:"flex gap-0 border-b border-border mb-6",children:[e.jsxs("button",{onClick:()=>o("leads"),className:`px-5 py-3 text-xs uppercase tracking-widest font-sans transition-colors border-b-2 -mb-px ${s==="leads"?"border-gold text-foreground":"border-transparent text-muted-foreground hover:text-foreground"}`,children:[e.jsx(q,{className:"h-3.5 w-3.5 inline mr-2"}),"Contact Leads (",u.length,")"]}),e.jsxs("button",{onClick:()=>o("subscribers"),className:`px-5 py-3 text-xs uppercase tracking-widest font-sans transition-colors border-b-2 -mb-px ${s==="subscribers"?"border-gold text-foreground":"border-transparent text-muted-foreground hover:text-foreground"}`,children:[e.jsx(me,{className:"h-3.5 w-3.5 inline mr-2"}),"Newsletter (",p.length,")"]})]}),s==="leads"&&e.jsxs(e.Fragment,{children:[w.length>0&&e.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[e.jsx(de,{className:"h-4 w-4 text-muted-foreground"}),e.jsxs("select",{value:i,onChange:t=>h(t.target.value),className:"bg-card border border-border rounded-none px-3 py-2 text-sm font-sans text-foreground",children:[e.jsxs("option",{value:"all",children:["All Projects (",c.length,")"]}),w.map(t=>e.jsxs("option",{value:t,children:[t," (",c.filter(n=>n.project_slug===t).length,")"]},t)),e.jsxs("option",{value:"none",children:["No Project (",c.filter(t=>!t.project_slug).length,")"]})]})]}),j?e.jsx("p",{className:"text-muted-foreground font-sans animate-pulse",children:"Loadingâ¦"}):u.length===0?e.jsx("p",{className:"text-muted-foreground font-sans py-16 text-center",children:i!=="all"?"No leads match this filter.":"No leads yet. They will appear here when someone submits the contact form."}):e.jsx("div",{className:"space-y-4",children:u.map(t=>e.jsx("button",{type:"button",onClick:()=>{g(t),b(!0)},className:"w-full text-left bg-card border border-border p-6 hover:border-gold/30 transition-colors",children:e.jsxs("div",{className:"flex flex-col md:flex-row md:items-start justify-between gap-4",children:[e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-3",children:[e.jsx("h3",{className:"font-sans font-medium text-foreground text-lg",children:t.full_name}),t.project_slug&&e.jsx("span",{className:"text-[10px] uppercase tracking-[0.15em] font-sans px-2 py-0.5 bg-accent/10 text-accent border border-accent/20",children:t.project_slug}),(t.assigned_user_id||t.assigned_name)&&e.jsxs("span",{className:"text-[10px] uppercase tracking-[0.15em] font-sans px-2 py-0.5 bg-gold/10 text-gold border border-gold/30 flex items-center gap-1",children:[e.jsx(Q,{className:"h-2.5 w-2.5"}),t.assigned_name||"Assigned"]})]}),e.jsxs("div",{className:"flex flex-wrap gap-4 mt-2 text-sm text-muted-foreground font-sans",children:[e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(q,{className:"h-3 w-3"})," ",t.email]}),t.phone&&e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx($,{className:"h-3 w-3"})," ",R(t.phone)]}),e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(B,{className:"h-3 w-3"})," ",S(t.created_at)]})]}),t.message&&e.jsx("p",{className:"text-sm text-muted-foreground
17/80 font-sans mt-3 bg-muted/30 p-3 rounded line-clamp-2",children:t.message})]}),e.jsxs("div",{className:"flex flex-col gap-1 text-right text-xs font-sans text-muted-foreground",children:[t.source&&e.jsxs("span",{children:["Source: ",t.source]}),t.preferred_language&&e.jsxs("span",{children:["Lang: ",t.preferred_language]}),t.preferred_contact&&e.jsxs("span",{children:["Contact: ",t.preferred_contact]}),e.jsxs("span",{className:`uppercase tracking-wider mt-1 ${t.crm_push_status==="pushed"?"text-green-600":"text-gold"}`,children:["CRM: ",t.crm_push_status??"pending"]})]})]})},t.id))})]}),s==="subscribers"&&e.jsx(e.Fragment,{children:N?e.jsx("p",{className:"text-muted-foreground font-sans animate-pulse",children:"Loadingâ¦"}):p.length===0?e.jsx("p",{className:"text-muted-foreground font-sans py-16 text-center",children:"No subscribers yet."}):e.jsx("div",{className:"border border-border admin-table-wrap",children:e.jsxs("table",{className:"w-full text-sm font-sans",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-border bg-muted/30",children:[e.jsx("th",{className:"text-left p-3 text-xs uppercase tracking-widest text-muted-foreground",children:"Email"}),e.jsx("th",{className:"text-left p-3 text-xs uppercase tracking-widest text-muted-foreground",children:"Subscribed"}),e.jsx("th",{className:"text-left p-3 text-xs uppercase tracking-widest text-muted-foreground",children:"Status"})]})}),e.jsx("tbody",{children:p.map(t=>e.jsxs("tr",{className:"border-b border-border last:border-0 hover:bg-muted/10",children:[e.jsx("td",{className:"p-3 font-medium",children:t.email}),e.jsx("td",{className:"p-3 text-muted-foreground",children:S(t.subscribed_at)}),e.jsx("td",{className:"p-3",children:t.unsubscribed_at?e.jsx("span",{className:"text-xs uppercase tracking-wider text-destructive",children:"Unsubscribed"}):e.jsx("span",{className:"text-xs uppercase tracking-wider text-green-600",children:"Active"})})]},t.id))})]})})}),e.jsx(fe,{lead:m,open:x,onOpenChange:b})]})}export{qe 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.