1import{k as Ue,r,j as e,B as j,D as X,m as Y,n as J,o as ee,s as u,A as he,e as ie,p as fe,z as je,q as R,c as W,X as Le}from"./index-nKGv0UZe.js";import{C as T,a as q,b as H,c as Q}from"./card-dczu3mNG.js";import{I as re}from"./input-65yosF0F.js";import{B as z}from"./badge-Cp-uJuiB.js";import{C as qe}from"./checkbox-CUvbAVIH.js";import{S as se}from"./scroll-area-D_60zBFZ.js";import{T as He,a as Qe,b as ne,c as le}from"./tabs-CbwRYTNf.js";import{u as te}from"./useQuery-B1iDl6A8.js";import{E as ge}from"./EmailComposer-Cnvw-ZNj.js";import{L as Z}from"./loader-circle-B3xNK53j.js";import{C as ye}from"./clock-CX5hJUVT.js";import{U as me}from"./users-iYazoYpP.js";import{C as Ne}from"./circle-check-big-CYJCpczD.js";import{C as Be}from"./circle-x-Ds3dt3lo.js";import{E as ce}from"./eye-DZvMd1Uk.js";import{f as B}from"./format-CMz-2fmV.js";import{L as Fe}from"./LegalDocumentFrame-CYrpSUN2.js";import{S as Ve,a as We,b as Xe,c as Ye,d as pe}from"./select-H8Mchz3D.js";import{F as de}from"./file-text-CM-r5kbY.js";import{S as oe}from"./send-Mtik1pQy.js";import{T as Je}from"./textarea-DDyfX6ar.js";import{I as De}from"./inbox-DztpHnma.js";import{S as ze}from"./search-B6WXIs6m.js";import{A as es}from"./arrow-left-CK7m9J_j.js";import{M as Oe}from"./message-circle-FjnbZmPa.js";import{s as ss,g as ts,c as as}from"./email-blast-staging-BGqhcnyB.js";import{M as Ie}from"./mail-DwwZWuRk.js";import{S as ns}from"./shield-DGLgctmA.js";import"./check-F6qeKBbB.js";import"./index-DOf1kvvy.js";import"./index-BdQq_4o_.js";import"./index-MY9rRORQ.js";import"./dropdown-menu-rnlf9xhf.js";import"./chevron-right-C70UXgC_.js";import"./circle-CCMRsUou.js";import"./upload-Cj44gRLa.js";import"./chevron-down-u6f15DYh.js";import"./trash-2-D6kLtzjh.js";import"./type-CwJNZTk-.js";import"./chevron-up-m59VvkOd.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 ls=Ue("Scale",[["path",{d:"m16 16 3-8 3 8c-.87.65-1.92 1-3 1s-2.13-.35-3-1Z",key:"7g6ntu"}],["path",{d:"m2 16 3-8 3 8c-.87.65-1.92 1-3 1s-2.13-.35-3-1Z",key:"ijws7r"}],["path",{d:"M7 21h10",key:"1b0cd5"}],["path",{d:"M12 3v18",key:"108xh3"}],["path",{d:"M3 7h2c2 0 5-1 7-2 2 1 5 2 7 2h2",key:"3gwbw2"}]]),Te={all_talent:"All Talent",all_clients:"All Clients",all:"Everyone",selected:"Selected"};function is(){var k;const[n,h]=r.useState(null),{data:L=[],isLoading:N}=te({queryKey:["admin-communications-history"],queryFn:async()=>{const{data:a,error:g}=await u.from("admin_communications").select("*").order("created_at",{ascending:!1}).limit(50);if(g)throw g;return a||[]}});return N?e.jsx(T,{children:e.jsx(q,{className:"flex items-center justify-center py-12",children:e.jsx(Z,{className:"h-5 w-5 animate-spin text-muted-foreground"})})}):L.length===0?e.jsxs(T,{children:[e.jsx(H,{children:e.jsx(Q,{className:"text-lg",children:"Sent Communications"})}),e.jsx(q,{children:e.jsx("p",{className:"text-sm text-muted-foreground text-center py-8",children:"No communications sent yet."})})]}):e.jsxs(e.Fragment,{children:[e.jsxs(T,{children:[e.jsx(H,{children:e.jsxs(Q,{className:"text-lg flex items-center gap-2",children:[e.jsx(ye,{className:"h-5 w-5"}),"Sent Communications"]})}),e.jsx(q,{className:"p-0",children:e.jsx(se,{className:"max-h-[500px]",children:e.jsx("div",{className:"divide-y",children:L.map(a=>{var g;return e.jsxs("div",{className:"flex items-center gap-4 px-6 py-4 hover:bg-muted/30 transition-colors",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"font-medium text-sm truncate",children:a.subject}),e.jsxs("div",{className:"flex items-center gap-2 mt-1 flex-wrap",children:[e.jsx(z,{variant:"outline",className:"text-[10px]",children:Te[a.recipient_type]||a.recipient_type}),a.recipient_type==="selected"&&((g=a.selected_emails)!=null&&g.length)?e.jsxs("span",{className:"text-xs text-muted-foreground truncate max-w-[300px]",children:[a.selected_emails.slice(0,3).join(", "),a.selected_emails.length>3&&` +${a.selected_emails.length-3} more`]}):null,e.jsx("span",{className:"text-xs text-muted-foreground",children:B(new Date(a.created_at),"MMM d, yyyy · h:mm a")})]})]}),e.jsxs("div",{className:"flex items-center gap-3 shrink-0",children:[e.jsxs("div",{className:"flex items-center gap-1 text-xs text-muted-foreground",children:[e.jsx(me,{className:"h-3.5 w-3.5"}),a.total_recipients]}),e.jsxs("div",{className:"flex items-center gap-1 text-xs text-green-600",children:[e.jsx(Ne,{className:"h-3.5 w-3.5"}),a.sent_count]}),a.failed_count>0&&e.jsxs("div",{className:"flex items-center gap-1 text-xs text-destructive",children:[e.jsx(Be,{className:"h-3.5 w-3.5"}),a.failed_count]}),e.jsx(j,{variant:"ghost",size:"sm",onClick:()=>h(a),children:e.jsx(ce,{className:"h-4 w-4"})})]})]},a.id)})})})})]}),e.jsx(X,{open:!!n,onOpenChange:()=>h(null),children:e.jsxs(Y,{className:"max-w-2xl max-h-[80vh]",children:[e.jsx(J,{children:e.jsx(ee,{children:n==null?void 0:n.subject})}),e.jsxs("div",{className:"text-xs text-muted-foreground space-y-1 mb-2",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(z,{variant:"outline",children:Te[(n==null?void 0:n.recipient_type)||""]||(n==null?void 0:n.recipient_type)}),e.jsx("span",{children:n&&B(new Date(n.created_at),"MMM d, yyyy · h:mm a")}),e.jsxs("span",{children:[n==null?void 0:n.sent_count," sent · ",n==null?void 0:n.failed_count," failed"]})]}),(n==null?void 0:n.recipient_type)==="selected"&&((k=n==null?void 0:n.selected_emails)!=null&&k.length)?e.jsx("div",{className:"flex flex-wrap gap-1 pt-1",children:n.selected_emails.map(a=>e.jsx(z,{variant:"secondary",className:"text-[10px]",children:a},a))}):null]}),e.jsx("div",{className:"max-h-[60vh] border rounded-md overflow-hidden",children:e.jsx("iframe",{srcDoc:(n==null?void 0:n.html_content)||"",title:"Email preview",sandbox:"",className:"w-full border-0",style:{height:"60vh"}})})]})})]})}function rs(){const n=he(),[h,L]=r.useState(""),[N,k]=r.useState(""),[a,g]=r.useState("all"),[v,G]=r.useState(!1),[P,S]=r.useState(!1),[A,x]=r.useState(!1),[b,_]=r.useState(null),{data:O=[],isLoading:I}=te({queryKey:["terms-versions"],queryFn:async()=>{const{data:i,error:m}=await u.from("terms_versions").select("*").order("created_at",{ascending:!1}).limit(20);if(m)throw m;return i||[]}}),s=O.find(i=>i.is_active),{data:w}=te({queryKey:["terms-acceptance-stats",s==null?void 0:s.id],queryFn:async()=>
6{if(!s)return{accepted:0};const{count:i,error:m}=await u.from("terms_acceptances").select("*",{count:"exact",head:!0}).eq("terms_version_id",s.id);if(m)throw m;return{accepted:i||0}},enabled:!!s}),d=async()=>{x(!1),S(!0);try{const{data:{user:i}}=await u.auth.getUser();if(!i)throw new Error("Not authenticated");const{error:m}=await u.from("terms_versions").update({is_active:!1}).eq("is_active",!0);if(m)throw m;const{error:y}=await u.from("terms_versions").insert({version_label:h.trim(),html_content:N,published_by:i.id,is_active:!0,send_email_notification:v,target_audience:a});if(y){console.error("Insert failed, attempting rollback:",y);const{data:c}=await u.from("terms_versions").select("id").order("created_at",{ascending:!1}).limit(1);throw c!=null&&c[0]&&await u.from("terms_versions").update({is_active:!0}).eq("id",c[0].id),y}if(v){const{error:c}=await u.functions.invoke("send-terms-notification",{body:{versionLabel:h.trim(),targetAudience:a}});c?(console.error("Email notification failed:",c),R.warning("Terms published but email notification failed to send.")):R.success("Terms published and notification emails queued.")}else R.success("New terms & conditions published.");L(""),k(""),G(!1),n.invalidateQueries({queryKey:["terms-versions"]}),n.invalidateQueries({queryKey:["terms-acceptance-stats"]})}catch(i){R.error(i.message||"Failed to publish terms")}finally{S(!1)}},F=h.trim()&&N.trim();return e.jsxs("div",{className:"space-y-6",children:[s&&e.jsx(T,{children:e.jsx(q,{className:"py-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"h-10 w-10 rounded-full bg-primary/10 flex items-center justify-center",children:e.jsx(de,{className:"h-5 w-5 text-primary"})}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("p",{className:"font-medium text-sm",children:["Active: ",s.version_label]}),e.jsx(z,{variant:"default",className:"text-[10px]",children:"Live"})]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Published ",B(new Date(s.created_at),"MMM d, yyyy")," · ",(w==null?void 0:w.accepted)??0," users accepted"]})]})]}),e.jsxs(j,{variant:"ghost",size:"sm",onClick:()=>_(s),children:[e.jsx(ce,{className:"h-4 w-4 mr-1.5"}),"Preview"]})]})})}),e.jsxs(T,{children:[e.jsx(H,{children:e.jsxs(Q,{className:"text-lg flex items-center gap-2",children:[e.jsx(de,{className:"h-5 w-5"}),"Publish New Terms & Conditions"]})}),e.jsxs(q,{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(ie,{htmlFor:"version-label",children:"Version Label"}),e.jsx(re,{id:"version-label",placeholder:"e.g. v2.1 â March 2026",value:h,onChange:i=>L(i.target.value),maxLength:100})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ie,{children:"Target Audience"}),e.jsxs(Ve,{value:a,onValueChange:g,children:[e.jsx(We,{children:e.jsx(Xe,{})}),e.jsxs(Ye,{children:[e.jsx(pe,{value:"all",children:"All Users (Talent & Clients)"}),e.jsx(pe,{value:"talent",children:"Talent Only"}),e.jsx(pe,{value:"clients",children:"Clients Only"})]})]})]})]}),e.jsx(ge,{htmlContent:N,onContentChange:k}),e.jsxs("div",{className:"flex items-center justify-between pt-4 border-t",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(qe,{id:"send-email",checked:v,onCheckedChange:i=>G(i===!0)}),e.jsxs(ie,{htmlFor:"send-email",className:"text-sm cursor-pointer",children:["Send email notification to ",a==="all"?"all users":a]})]}),e.jsx(j,{onClick:()=>x(!0),disabled:!F||P,className:"min-w-[180px]",children:P?e.jsxs(e.Fragment,{children:[e.jsx(Z,{className:"h-4 w-4 mr-2 animate-spin"}),"Publishing..."]}):e.jsxs(e.Fragment,{children:[e.jsx(oe,{className:"h-4 w-4 mr-2"}),"Publish & Activate"]})})]}),s&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Publishing will deactivate the current version (",s.version_label,") and require all users to re-accept."]})]})]}),O.length>0&&e.jsxs(T,{children:[e.jsx(H,{children:e.jsxs(Q,{className:"text-lg flex items-center gap-2",children:[e.jsx(ye,{className:"h-5 w-5"}),"Version History"]})}),e.jsx(q,{className:"p-0",children:e.jsx(se,{className:"max-h-[300px]",children:e.jsx("div",{className:"divide-y",children:O.map(i=>e.jsxs("div",{className:"flex items-center gap-4 px-6 py-3 hover:bg-muted/30 transition-colors",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("p",{className:"text-sm font-medium",children:i.version_label}),i.is_active&&e.jsx(z,{variant:"default",className:"text-[10px]",children:"Active"}),i.send_email_notification&&e.jsx(z,{variant:"outline",className:"text-[10px]",children:"Email Sent"})]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[B(new Date(i.created_at),"MMM d, yyyy · h:mm a")," · Target: ",i.target_audience]})]}),e.jsx(j,{variant:"ghost",size:"sm",onClick:()=>_(i),children:e.jsx(ce,{className:"h-4 w-4"})})]},i.id))})})})]}),e.jsx(X,{open:A,onOpenChange:x,children:e.jsxs(Y,{children:[e.jsxs(J,{children:[e.jsx(ee,{children:"Publish New Terms & Conditions?"}),e.jsxs(fe,{children:['This will activate "',e.jsx("strong",{children:h}),'" and require all ',a==="all"?"users":a," to accept before using the platform.",v&&" An email notification will also be sent.",s&&` The current version (${s.version_label}) will be deactivated.`]})]}),e.jsxs(je,{children:[e.jsx(j,{variant:"outline",onClick:()=>x(!1),children:"Cancel"}),e.jsxs(j,{onClick:d,disabled:P,children:[P?e.jsx(Z,{className:"h-4 w-4 mr-2 animate-spin"}):e.jsx(Ne,{className:"h-4 w-4 mr-2"}),"Confirm & Publish"]})]})]})}),e.jsx(X,{open:!!b,onOpenChange:()=>_(null),children:e.jsxs(Y,{className:"max-w-2xl max-h-[80vh] flex flex-col",children:[e.jsx(J,{children:e.jsx(ee,{children:b==null?void 0:b.version_label})}),e.jsx("div",{className:"border rounded-md overflow-y-auto min-h-0 flex-1",style:{maxHeight:"60vh"},children:b&&e.jsx(Fe,{htmlContent:b.html_content,title:"Terms of Service preview"})})]})})]})}function cs(){const n=he(),[h,L]=r.useState(null),[N,k]=r.useState(""),[a,g]=r.useState(!1),[v,G]=r.useState(""),P=r.useRef(null),{data:S=[],isLoading:A}=te({queryKey:["admin-support-threads"],queryFn:async()=>{const{data:s,error:w}=await u.from("support_messages").select("*").order("created_at",{ascending:!0});if(w)throw w;const d=[...new Set((s||[]).map(c=>c.talent_id))];if(d.length===0)return[];const{data:F,error:i}=await u.from("talents").select("id, name, email, display_name").in("id",d);if(i)throw i;const m=new Map((F||[]).map(c=>[c.id,{name:c.display_name||c.name,email:c.email}])),y=new Map;for(const c of s||[]){if(!y.has(c.talent_id)){const U=m.get(c.talent_id);y.set(c.talent_id,{talent_id:c.talent_id,talent_name:(U==null?void 0:U.name)||"Unknown",talent_email:(U==null?void 0:U.email)||"",last_message:c.message,last_message_at:c.created_at,unread_count:0,messages:[]})}const $=y.get(c.talent_id);$.messages.push(c),$.last_message=c.message,$.last_message_at=c.created_at,!c.read&&c.sender_type==="talent"&&$.unread_count++}return Array.from(y.values()).sort((c,$)=>new Date($.last_message_at).getTime()-new Date(c.last_message_at).getTime())}}),x=S.find(s=>s.talent_id===h),b=S.reduce((s,w)=>s+w.unread_count,0),_=v.trim()?S.filter(s=>s.talent_name.toLowerCase().includes(v.toLowerCase())||s.talent_email.toLowerCase().includes(v.toLowerCase())):S;r.useEffect(()=>{var s;(s=P.current)==null||s.scrollIntoView({behavior:"smooth"})},[x==null?void 0:x.messages.length]),r.useEffect(()=>{if(!x||x.unread_count===0)return;(async()=>{const w=x.messages.filter(d=>!d.read&&d.sender_type==="talent").map(d=>d.id);w.length!==0&&(await u.from("support_messages").update({read:!0}).in("id",w),n.invalidateQueries({queryKey:["admin-support-threads"]}))})()},[h]),r.useEffect(()=>{const s=u.channel("admin-support-inbox").on("postgres_changes",{event:"INSERT",schema:"public",table:"support_messages"},()=>{n.invalidateQueries({queryKey:["admin-support-threads"]})}).subscribe();return()=>{u.removeChannel(s)}},[n]);const O=async()=>
6{if(!(!x||!N.trim())){g(!0);try{const{data:s,error:w}=await u.functions.invoke("send-admin-support-reply",{body:{talentId:x.talent_id,message:N.trim()}});if(w){const d=typeof s=="object"&&(s!=null&&s.error)?s.error:w.message;throw new Error(d||"Failed to send reply")}k(""),n.invalidateQueries({queryKey:["admin-support-threads"]}),R.success("Reply sent â talent notified by email")}catch(s){R.error(s.message||"Failed to send reply")}finally{g(!1)}}},I=s=>{s.key==="Enter"&&!s.shiftKey&&(s.preventDefault(),O())};return A?e.jsx(T,{children:e.jsx(q,{className:"flex items-center justify-center py-12",children:e.jsx(Z,{className:"h-5 w-5 animate-spin text-muted-foreground"})})}):e.jsx(T,{className:"overflow-hidden",children:e.jsxs("div",{className:"flex h-[600px]",children:[e.jsxs("div",{className:W("w-full md:w-80 border-r flex flex-col shrink-0",h&&"hidden md:flex"),children:[e.jsxs("div",{className:"p-3 border-b space-y-2",children:[e.jsx("div",{className:"flex items-center justify-between",children:e.jsxs("h3",{className:"font-semibold text-sm flex items-center gap-2",children:[e.jsx(De,{className:"h-4 w-4"}),"Inbox",b>0&&e.jsx(z,{variant:"destructive",className:"text-[10px] h-5 px-1.5",children:b})]})}),e.jsxs("div",{className:"relative",children:[e.jsx(ze,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-muted-foreground"}),e.jsx(re,{placeholder:"Search talent...",value:v,onChange:s=>G(s.target.value),className:"pl-8 h-8 text-xs"})]})]}),e.jsx(se,{className:"flex-1",children:_.length===0?e.jsx("p",{className:"text-sm text-muted-foreground text-center py-8",children:"No conversations yet"}):_.map(s=>e.jsx("button",{onClick:()=>L(s.talent_id),className:W("w-full text-left px-4 py-3 border-b hover:bg-muted/50 transition-colors",h===s.talent_id&&"bg-muted"),children:e.jsxs("div",{className:"flex items-start justify-between gap-2",children:[e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("p",{className:W("text-sm truncate",s.unread_count>0?"font-bold":"font-medium"),children:s.talent_name}),s.unread_count>0&&e.jsx(z,{variant:"destructive",className:"text-[9px] h-4 px-1 shrink-0",children:s.unread_count})]}),e.jsxs("p",{className:"text-xs text-muted-foreground truncate mt-0.5",children:[s.last_message.substring(0,60),s.last_message.length>60?"...":""]})]}),e.jsx("span",{className:"text-[10px] text-muted-foreground shrink-0 mt-0.5",children:B(new Date(s.last_message_at),"MMM d")})]})},s.talent_id))})]}),e.jsx("div",{className:W("flex-1 flex flex-col",!h&&"hidden md:flex"),children:x?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"px-4 py-3 border-b flex items-center gap-3",children:[e.jsx(j,{variant:"ghost",size:"icon",className:"md:hidden h-8 w-8",onClick:()=>L(null),children:e.jsx(es,{className:"h-4 w-4"})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"font-semibold text-sm",children:x.talent_name}),e.jsx("p",{className:"text-xs text-muted-foreground",children:x.talent_email})]})]}),e.jsx(se,{className:"flex-1 p-4",children:e.jsxs("div",{className:"space-y-3",children:[x.messages.map(s=>e.jsxs("div",{className:W("max-w-[80%] rounded-lg px-3 py-2",s.sender_type==="admin"?"ml-auto bg-primary text-primary-foreground":"bg-muted"),children:[e.jsx("p",{className:"text-sm whitespace-pre-wrap break-words",children:s.message}),e.jsx("p",{className:W("text-[10px] mt-1",s.sender_type==="admin"?"text-primary-foreground/60":"text-muted-foreground"),children:B(new Date(s.created_at),"MMM d, h:mm a")})]},s.id)),e.jsx("div",{ref:P})]})}),e.jsxs("div",{className:"p-3 border-t flex gap-2",children:[e.jsx(Je,{value:N,onChange:s=>k(s.target.value),onKeyDown:I,placeholder:"Type a reply... (Enter to send, Shift+Enter for new line)",className:"min-h-[40px] max-h-[120px] resize-none text-sm",disabled:a}),e.jsx(j,{onClick:O,disabled:!N.trim()||a,size:"icon",className:"shrink-0 h-10 w-10",children:a?e.jsx(Z,{className:"h-4 w-4 animate-spin"}):e.jsx(oe,{className:"h-4 w-4"})})]})]}):e.jsx("div",{className:"flex-1 flex items-center justify-center text-muted-foreground",children:e.jsxs("div",{className:"text-center",children:[e.jsx(Oe,{className:"h-10 w-10 mx-auto mb-3 opacity-30"}),e.jsx("p",{className:"text-sm",children:"Select a conversation to view messages"})]})})})]})})}function Ae({documentType:n,title:h}){const L=he(),[N,k]=r.useState(""),[a,g]=r.useState(""),[v,G]=r.useState(!1),[P,S]=r.useState(!1),[A,x]=r.useState(null),b=["legal-documents",n],{data:_=[],isLoading:O}=te({queryKey:b,queryFn:async()=>{const{data:d,error:F}=await u.from("legal_documents").select("*").eq("document_type",n).order("created_at",{ascending:!1}).limit(20);if(F)throw F;return d||[]}}),I=_.find(d=>d.is_active),s=async()=>{S(!1),G(!0);try{const{data:{user:d}}=await u.auth.getUser();if(!d)throw new Error("Not authenticated");const{error:F}=await u.from("legal_documents").update({is_active:!1}).eq("document_type",n).eq("is_active",!0);if(F)throw F;const{error:i}=await u.from("legal_documents").insert({document_type:n,version_label:N.trim(),html_content:a,published_by:d.id,is_active:!0});if(i){const{data:m}=await u.from("legal_documents").select("id").eq("document_type",n).order("created_at",{ascending:!1}).limit(1);throw m!=null&&m[0]&&await u.from("legal_documents").update({is_active:!0}).eq("id",m[0].id),i}R.success(`${h} published successfully.`),k(""),g(""),L.invalidateQueries({queryKey:b})}catch(d){R.error(d.message||`Failed to publish ${h.toLowerCase()}`)}finally{G(!1)}},w=N.trim()&&a.trim();return e.jsxs("div",{className:"space-y-6",children:[I&&e.jsx(T,{children:e.jsx(q,{className:"py-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"h-10 w-10 rounded-full bg-primary/10 flex items-center justify-center",children:e.jsx(de,{className:"h-5 w-5 text-primary"})}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("p",{className:"font-medium text-sm",children:["Active: ",I.version_label]}),e.jsx(z,{variant:"default",className:"text-[10px]",children:"Live"})]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Published ",B(new Date(I.created_at),"MMM d, yyyy")]})]})]}),e.jsxs(j,{variant:"ghost",size:"sm",onClick:()=>x(I),children:[e.jsx(ce,{className:"h-4 w-4 mr-1.5"}),"Preview"]})]})})}),e.jsxs(T,{children:[e.jsx(H,{children:e.jsxs(Q,{className:"text-lg flex items-center gap-2",children:[e.jsx(de,{className:"h-5 w-5"}),"Publish New ",h]})}),e.jsxs(q,{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(ie,{htmlFor:`version-label-${n}`,children:"Version Label"}
6),e.jsx(re,{id:`version-label-${n}`,placeholder:"e.g. v2.1 â March 2026",value:N,onChange:d=>k(d.target.value),maxLength:100})]}),e.jsx(ge,{htmlContent:a,onContentChange:g}),e.jsx("div",{className:"flex items-center justify-end pt-4 border-t",children:e.jsx(j,{onClick:()=>S(!0),disabled:!w||v,className:"min-w-[180px]",children:v?e.jsxs(e.Fragment,{children:[e.jsx(Z,{className:"h-4 w-4 mr-2 animate-spin"}),"Publishing..."]}):e.jsxs(e.Fragment,{children:[e.jsx(oe,{className:"h-4 w-4 mr-2"}),"Publish & Activate"]})})}),I&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Publishing will replace the current version (",I.version_label,")."]})]})]}),_.length>0&&e.jsxs(T,{children:[e.jsx(H,{children:e.jsxs(Q,{className:"text-lg flex items-center gap-2",children:[e.jsx(ye,{className:"h-5 w-5"}),"Version History"]})}),e.jsx(q,{className:"p-0",children:e.jsx(se,{className:"max-h-[300px]",children:e.jsx("div",{className:"divide-y",children:_.map(d=>e.jsxs("div",{className:"flex items-center gap-4 px-6 py-3 hover:bg-muted/30 transition-colors",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("p",{className:"text-sm font-medium",children:d.version_label}),d.is_active&&e.jsx(z,{variant:"default",className:"text-[10px]",children:"Active"})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:B(new Date(d.created_at),"MMM d, yyyy · h:mm a")})]}),e.jsx(j,{variant:"ghost",size:"sm",onClick:()=>x(d),children:e.jsx(ce,{className:"h-4 w-4"})})]},d.id))})})})]}),e.jsx(X,{open:P,onOpenChange:S,children:e.jsxs(Y,{children:[e.jsxs(J,{children:[e.jsxs(ee,{children:["Publish New ",h,"?"]}),e.jsxs(fe,{children:['This will activate "',e.jsx("strong",{children:N}),'" as the current ',h.toLowerCase(),".",I&&` The current version (${I.version_label}) will be replaced.`]})]}),e.jsxs(je,{children:[e.jsx(j,{variant:"outline",onClick:()=>S(!1),children:"Cancel"}),e.jsxs(j,{onClick:s,disabled:v,children:[v?e.jsx(Z,{className:"h-4 w-4 mr-2 animate-spin"}):e.jsx(Ne,{className:"h-4 w-4 mr-2"}),"Confirm & Publish"]})]})]})}),e.jsx(X,{open:!!A,onOpenChange:()=>x(null),children:e.jsxs(Y,{className:"max-w-2xl max-h-[80vh] flex flex-col",children:[e.jsx(J,{children:e.jsx(ee,{children:A==null?void 0:A.version_label})}),e.jsx("div",{className:"border rounded-md overflow-y-auto min-h-0 flex-1",style:{maxHeight:"60vh"},children:A&&e.jsx(Fe,{htmlContent:A.html_content,title:`${h} preview`})})]})})]})}function Vs(){const n=he(),[h,L]=r.useState(""),[N,k]=r.useState(""),[a,g]=r.useState(new Set),[v,G]=r.useState(""),[P,S]=r.useState(!1),[A,x]=r.useState(!1),[b,_]=r.useState(!1),[O,I]=r.useState([]),[s,w]=r.useState(0);r.useEffect(()=>{const t=()=>{const o=ts();o.length!==0&&(I(C=>{const K=new Map(C.map(D=>[D.email.toLowerCase(),D]));for(const D of o)K.set(D.email.toLowerCase(),D);return Array.from(K.values())}),g(C=>{const K=new Set(C);return o.forEach(D=>K.add(D.email)),K}),w(o.length),as())};return t(),ss(t)},[]);const{data:d=[],isLoading:F}=te({queryKey:["admin-email-recipients"],queryFn:async()=>{const t=[],o=[];let C=0;for(;;){const{data:l,error:E}=await u.from("talents").select("email, name").eq("status","approved").is("deleted_at",null).range(C*1e3,(C+1)*1e3-1);if(E)throw E;if(!l||l.length===0||(o.push(...l),l.length<1e3))break;C++}o.forEach(l=>{l.email&&t.push({email:l.email,name:l.name,type:"talent"})});const K=[];let D=0;for(;;){const{data:l,error:E}=await u.from("user_roles").select("user_id, role").range(D*1e3,(D+1)*1e3-1);if(E)throw E;if(!l||l.length===0||(K.push(...l),l.length<1e3))break;D++}const _e=new Set(K.filter(l=>l.role==="client").map(l=>l.user_id)),Se=new Set(K.filter(l=>l.role==="talent").map(l=>l.user_id)),Ce=new Set(K.filter(l=>l.role==="admin").map(l=>l.user_id)),Ge=[..._e].filter(l=>!Se.has(l)&&!Ce.has(l)),Ee=[..._e].filter(l=>Se.has(l)&&!Ce.has(l));let ke=[];if(Ee.length>0){const l=[];let E=0;for(;;){const{data:f,error:V}=await u.from("user_subscriptions").select("user_id, stripe_subscription_status, stripe_customer_id, stripe_subscription_id").in("user_id",Ee).range(E*1e3,(E+1)*1e3-1);if(V)throw V;if(!f||f.length===0||(l.push(...f),f.length<1e3))break;E++}const ae=new Set(["active","trialing"]);ke=l.filter(f=>{const V=f.stripe_subscription_status&&ae.has(f.stripe_subscription_status),$e=!!(f.stripe_subscription_id||f.stripe_customer_id);return V&&$e}).map(f=>f.user_id)}const Pe=new Set([...Ge,...ke]);if(Pe.size>0){const l=[...Pe],E=[];let ae=0;for(;;){const{data:f,error:V}=await u.from("profiles").select("id, email, full_name").in("id",l).eq("is_restricted",!1).range(ae*1e3,(ae+1)*1e3-1);if(V)throw V;if(!f||f.length===0||(E.push(...f),f.length<1e3))break;ae++}E.forEach(f=>{f.email&&t.push({email:f.email,name:f.full_name||f.email,type:"client"})})}const Me=new Set;return t.filter(l=>{if(!l.email)return!1;const E=l.email.toLowerCase();return Me.has(E)?!1:(Me.add(E),!0)})}}),i=r.useMemo(()=>{const t=new Set(d.map(o=>o.email.toLowerCase())),p=O.filter(o=>!t.has(o.email.toLowerCase()));return[...d,...p]},[d,O]),m=r.useMemo(()=>i.filter(t=>t.type==="talent").map(t=>t.email),[i]),y=r.useMemo(()=>i.filter(t=>t.type==="client").map(t=>t.email),[i]),c=r.useMemo(()=>i.filter(t=>t.type==="waitlist").map(t=>t.email),[i]),$=r.useMemo(()=>{let t=i;if(b&&(t=t.filter(o=>a.has(o.email))),!v.trim())return t;const p=v.toLowerCase();return t.filter(o=>o.name.toLowerCase().includes(p)||o.email.toLowerCase().includes(p))},[i,v,b,a]),U=t=>{g(p=>{const o=new Set(p);return o.has(t)?o.delete(t):o.add(t),o})},xe=t=>{g(p=>{const o=new Set(p);return t.forEach(C=>o.add(C)),o})},ue=t=>{g(p=>{const o=new Set(p);return t.forEach(C=>o.delete(C)),o})},M=a.size,Ke=h.trim()&&N.trim()&&M>0,Re=r.useMemo(()=>
6{if(M===0)return"selected";const t=m.length>0&&m.every(C=>a.has(C)),p=y.length>0&&y.every(C=>a.has(C)),o=M===(t?m.length:0)+(p?y.length:0);return t&&p&&o?"all":t&&!p&&M===m.length?"all_talent":p&&!t&&M===y.length?"all_clients":"selected"},[a,m,y,M]),Ze=async()=>{x(!1),S(!0);try{const t=Array.from(a),{data:p,error:o}=await u.functions.invoke("send-admin-communication",{body:{subject:h,htmlContent:N,recipientType:Re,selectedEmails:t}});if(o)throw o;R.success(`Email sent to ${p.sent} recipient${p.sent!==1?"s":""}${p.failed>0?` (${p.failed} failed)`:""}`),L(""),k(""),g(new Set),n.invalidateQueries({queryKey:["admin-communications-history"]})}catch(t){R.error(t.message||"Failed to send communication")}finally{S(!1)}},ve=m.length>0&&m.every(t=>a.has(t)),we=y.length>0&&y.every(t=>a.has(t)),be=c.length>0&&c.every(t=>a.has(t));return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold",children:"Communications"}),e.jsx("p",{className:"text-muted-foreground",children:"Send email communications and manage terms & conditions."})]}),e.jsxs(He,{defaultValue:"email",className:"w-full",children:[e.jsxs(Qe,{children:[e.jsxs(ne,{value:"email",className:"gap-1.5",children:[e.jsx(Ie,{className:"h-4 w-4"}),"Email Blast"]}),e.jsxs(ne,{value:"support",className:"gap-1.5",children:[e.jsx(Oe,{className:"h-4 w-4"}),"Talent Support"]}),e.jsxs(ne,{value:"terms",className:"gap-1.5",children:[e.jsx(de,{className:"h-4 w-4"}),"Terms & Conditions"]}),e.jsxs(ne,{value:"privacy",className:"gap-1.5",children:[e.jsx(ns,{className:"h-4 w-4"}),"Privacy Policy"]}),e.jsxs(ne,{value:"license",className:"gap-1.5",children:[e.jsx(ls,{className:"h-4 w-4"}),"Commercial License"]})]}),e.jsxs(le,{value:"email",className:"mt-6 space-y-6",children:[s>0&&e.jsxs("div",{className:"flex items-center justify-between gap-3 rounded-lg border border-primary/30 bg-primary/5 px-4 py-3",children:[e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(De,{className:"h-4 w-4 text-primary"}),e.jsxs("span",{children:["Imported ",e.jsx("strong",{children:s})," recipient",s!==1?"s":""," from another list â they're pre-selected below."]})]}),e.jsx(j,{variant:"ghost",size:"sm",onClick:()=>w(0),children:e.jsx(Le,{className:"h-4 w-4"})})]}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-3 gap-6",children:[e.jsx("div",{className:"lg:col-span-2 space-y-4",children:e.jsxs(T,{children:[e.jsx(H,{children:e.jsxs(Q,{className:"text-lg flex items-center gap-2",children:[e.jsx(Ie,{className:"h-5 w-5"}),"Compose Email"]})}),e.jsxs(q,{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(ie,{htmlFor:"subject",children:"Subject Line"}),e.jsx(re,{id:"subject",placeholder:"e.g. Important Update from Trueth",value:h,onChange:t=>L(t.target.value)})]}),e.jsx(ge,{htmlContent:N,onContentChange:k}),e.jsxs("div",{className:"flex items-center justify-between pt-4 border-t",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:[M," recipient",M!==1?"s":""]}),e.jsx(j,{onClick:()=>x(!0),disabled:!Ke||P,className:"min-w-[140px]",children:P?e.jsxs(e.Fragment,{children:[e.jsx(Z,{className:"h-4 w-4 mr-2 animate-spin"}),"Sending..."]}):e.jsxs(e.Fragment,{children:[e.jsx(oe,{className:"h-4 w-4 mr-2"}),"Send Email"]})})]})]})]})}),e.jsx("div",{className:"space-y-4",children:e.jsxs(T,{children:[e.jsxs(H,{
6className:"pb-3",children:[e.jsx(Q,{className:"text-lg",children:"Recipients"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Quick-select a group, then uncheck anyone you want to exclude."})]}),e.jsxs(q,{className:"space-y-3",children:[e.jsxs("div",{className:"flex flex-wrap gap-2",children:[e.jsxs(j,{variant:ve?"default":"outline",size:"sm",className:"text-xs h-8",onClick:()=>{ve?ue(m):(xe(m),_(!0))},children:[e.jsx(me,{className:"h-3.5 w-3.5 mr-1.5"}),"All Talent (",m.length,")"]}),e.jsxs(j,{variant:we?"default":"outline",size:"sm",className:"text-xs h-8",onClick:()=>{we?ue(y):(xe(y),_(!0))},children:[e.jsx(me,{className:"h-3.5 w-3.5 mr-1.5"}),"All Clients (",y.length,")"]}),c.length>0&&e.jsxs(j,{variant:be?"default":"outline",size:"sm",className:"text-xs h-8",onClick:()=>{be?ue(c):(xe(c),_(!0))},children:[e.jsx(me,{className:"h-3.5 w-3.5 mr-1.5"}),"All Waitlist (",c.length,")"]}),M>0&&e.jsxs(j,{variant:"ghost",size:"sm",className:"text-xs h-8 text-destructive",onClick:()=>{g(new Set),_(!1)},children:[e.jsx(Le,{className:"h-3.5 w-3.5 mr-1"}),"Clear"]})]}),M>0&&e.jsxs("div",{className:"flex items-center justify-between text-xs border-b pb-2 gap-2",children:[e.jsxs("span",{className:"text-muted-foreground",children:[M," selected â ",m.filter(t=>a.has(t)).length," talent, ",y.filter(t=>a.has(t)).length," clients"]}),e.jsx("button",{type:"button",onClick:()=>_(t=>!t),className:"text-primary hover:underline shrink-0",children:b?"Show all":"Show selected only"})]}),e.jsxs("div",{className:"relative",children:[e.jsx(ze,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(re,{placeholder:"Search by name or email...",value:v,onChange:t=>G(t.target.value),className:"pl-9"})]}),e.jsx(se,{className:"h-[400px]",children:F?e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(Z,{className:"h-5 w-5 animate-spin text-muted-foreground"})}):e.jsxs("div",{className:"space-y-1",children:[$.map(t=>e.jsxs("label",{className:"flex items-center gap-3 p-2 rounded-md hover:bg-muted/50 cursor-pointer",children:[e.jsx(qe,{checked:a.has(t.email),onCheckedChange:()=>U(t.email)}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-sm font-medium truncate",children:t.name}),e.jsx("p",{className:"text-xs text-muted-foreground truncate",children:t.email})]}),e.jsx(z,{variant:"outline",className:"text-[10px] shrink-0",children:t.type})]},t.email)),$.length===0&&e.jsx("p",{className:"text-sm text-muted-foreground text-center py-4",children:"No recipients found"})]})})]})]})})]}),e.jsx(is,{}),e.jsx(X,{open:A,onOpenChange:x,children:e.jsxs(Y,{children:[e.jsxs(J,{children:[e.jsx(ee,{children:"Confirm Send"}),e.jsxs(fe,{children:['You are about to send "',e.jsx("strong",{children:h}),'" to ',e.jsx("strong",{children:M})," recipient",M!==1?"s":"",". This action cannot be undone."]})]}),e.jsxs(je,{children:[e.jsx(j,{variant:"outline",onClick:()=>x(!1),children:"Cancel"}),e.jsxs(j,{onClick:Ze,disabled:P,children:[P?e.jsx(Z,{className:"h-4 w-4 mr-2 animate-spin"}):e.jsx(oe,{className:"h-4 w-4 mr-2"}),"Send to ",M," people"]})]})]})})]}),e.jsx(le,{value:"support",className:"mt-6",children:e.jsx(cs,{})}),e.jsx(le,{value:"terms",className:"mt-6",children:e.jsx(rs,{})}),e.jsx(le,{value:"privacy",className:"mt-6",children:e.jsx(Ae,{documentType:"privacy_policy",title:"Privacy Policy"})}),e.jsx(le,{value:"license",className:"mt-6",children:e.jsx(Ae,{documentType:"commercial_license",title:"Commercial License"})})]})]})}export{Vs 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.