PageSourceSearch

https://brightengine-production.up.railway.app/_next/static/chunks/712.8c71719023a48735.js

js brightengine-production.up.railway.app collected 2026-10-03 13:13:24 UTC 164,095 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[712],{32660:function(e,t,a){a.d(t,{Z:function(){return r}});let r=(0,a(39763).Z)("ArrowLeft",[["path",{d:"m12 19-7-7 7-7",key:"1l729n"}],["path",{d:"M19 12H5",key:"x3x0zl"}]])},10407:function(e,t,a){a.d(t,{Z:function(){return r}});let r=(0,a(39763).Z)("ChevronRight",[["path",{d:"m9 18 6-6-6-6",key:"mthhwq"}]])},91723:function(e,t,a){a.d(t,{Z:function(){return r}});let r=(0,a(39763).Z)("Clock",[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["polyline",{points:"12 6 12 12 16 14",key:"68esgv"}]])},92735:function(e,t,a){a.d(t,{Z:function(){return r}});let r=(0,a(39763).Z)("Download",[["path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4",key:"ih7n3h"}],["polyline",{points:"7 10 12 15 17 10",key:"2ggqvy"}],["line",{x1:"12",x2:"12",y1:"15",y2:"3",key:"1vk2je"}]])},33245:function(e,t,a){a.d(t,{Z:function(){return r}});let r=(0,a(39763).Z)("Info",[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["path",{d:"M12 16v-4",key:"1dtifu"}],["path",{d:"M12 8h.01",key:"e9boi3"}]])},17684:function(e,t,a){a.d(t,{Z:function(){return r}});let r=(0,a(39763).Z)("MoreVertical",[["circle",{cx:"12",cy:"12",r:"1",key:"41hilf"}],["circle",{cx:"12",cy:"5",r:"1",key:"gxeob9"}],["circle",{cx:"12",cy:"19",r:"1",key:"lyex9k"}]])},18930:function(e,t,a){a.d(t,{Z:function(){return r}});let r=(0,a(39763).Z)("Trash2",[["path",{d:"M3 6h18",key:"d0wm0j"}],["path",{d:"M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6",key:"4alrt4"}],["path",{d:"M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2",key:"v07s0e"}],["line",{x1:"10",x2:"10",y1:"11",y2:"17",key:"1uufr5"}],["line",{x1:"14",x2:"14",y1:"11",y2:"17",key:"xtxkd"}]])},32489:function(e,t,a){a.d(t,{Z:function(){return r}});let r=(0,a(39763).Z)("X",[["path",{d:"M18 6 6 18",key:"1bl5f8"}],["path",{d:"m6 6 12 12",key:"d8bk6v"}]])},60712:function(e,t,a){a.r(t),a.d(t,{default:function(){return U}});var r=a(57437),s=a(2265),l=a(15125),n=a(48311),d=a(40875),i=a(33245),o=a(10407),c=a(32489),x=a(82431),g=a(99397),m=a(44743),u=a(17684),y=a(32660),p=a(33276),b=a(89227),h=a(37806),f=a(45675),k=a(92735),v=a(18930),j=a(91723),N=a(63639);let w={30003:{label:"Unreachable destination",explanation:"The handset was unreachable — powered off, out of coverage, or no longer in service."},30005:{label:"Unknown destination / dead number",explanation:"The number doesn't exist or can no longer receive messages."},30006:{label:"Landline or unreachable carrier",explanation:"The number is a landline or on a carrier that can't receive SMS."},30007:{label:"Carrier violation / filtered",explanation:"A carrier blocked the message as spam — usually content-related (payment link, price, or multiple URLs)."},30008:{label:"Unknown delivery error",explanation:"The carrier reported a generic delivery failure; sometimes transient."},21610:{label:"Recipient opted out (STOP)",explanation:"The recipient previously texted STOP, so the carrier blocks any further sends."},21211:{label:"Invalid number",explanation:"The destination isn't a valid phone number (formatting error or typo)."},none:{label:"No code reported",explanation:"The send failed but the carrier returned no specific code."}};var C=a(80225);function S(e){let{channel:t}=e,[a,l]=(0,s.useState)(null),[n,i]=(0,s.useState)(!0),[c,x]=(0,s.useState)(!1);if((0,s.useEffect)(()=>{let e=!1,t=()=>{fetch("/api/reps/queue-summary").then(e=>e.ok?e.json():null).then(t=>{!e&&t&&l(t)}).catch(()=>{}).finally(()=>{e||i(!1)})};t();let a=setInterval(()=>{e||t()},6e4);return()=>{e=!0,clearInterval(a)}},[]),n||!a||0===a.byRep.length)return null;let g="email"===t?a.totals.email:a.totals.sms,m="email"===t?"text-blue-600 dark:text-blue-400":"text-green-600 dark:text-green-400";return(0,r.jsxs)("div",{className:"rounded-lg border px-4 py-2.5 mb-4 ".concat("email"===t?"bg-blue-50 dark:bg-blue-950/20 border-blue-200 dark:border-blue-800":"bg-green-50 dark:bg-green-950/20 border-green-200 dark:border-green-800"),children:[(0,r.jsxs)("button",{onClick:()=>x(!c),className:"flex items-center justify-between w-full text-left",children:[(0,r.jsxs)("div",{className:"flex items-center gap-3 flex-wrap",children:[(0,r.jsxs)("span",{className:"text-xs font-semibold uppercase tracking-wider ".concat(m),children:["email"===t?"Email":"SMS"," Queue"]}),(0,r.jsxs)("span",{className:"text-sm font-bold ".concat(m),children:[g.toLocaleStr
1ing()," leads"]}),c&&a.byRep.map(e=>{let a="email"===t?e.email:e.sms;return 0===a?null:(0,r.jsxs)("span",{className:"text-xs text-gray-500 dark:text-gray-400",children:[e.name,": ",(0,r.jsx)("span",{className:"font-semibold text-gray-700 dark:text-gray-300",children:a})]},e.id)})]}),c?(0,r.jsx)(o.Z,{size:14,className:"text-gray-400 flex-shrink-0"}):(0,r.jsx)(d.Z,{size:14,className:"text-gray-400 flex-shrink-0"})]}),!c&&(0,r.jsx)("div",{className:"flex items-center gap-4 mt-2 flex-wrap",children:a.byRep.map(e=>{let a="email"===t?e.email:e.sms;return(0,r.jsxs)("div",{className:"flex items-center gap-1.5",children:[(0,r.jsx)("span",{className:"text-xs text-gray-600 dark:text-gray-400",children:e.name}),(0,r.jsx)("span",{className:"text-sm font-bold ".concat(m),children:a}),e.queueTarget>0&&(0,r.jsxs)("span",{className:"text-xs text-gray-400 dark:text-gray-500",children:["/ ",e.queueTarget]})]},e.id)})})]})}var T=a(84292);let D=["mon","tue","wed","thu","fri","sat","sun"],E={mon:"Mon",tue:"Tue",wed:"Wed",thu:"Thu",fri:"Fri",sat:"Sat",sun:"Sun"},F=["{companyName}","{city}","{state}","{previewUrl}"];function R(e){let{value:t,onChange:a,showErrors:s=!1,smartWeeklyEnabled:l=!1,onSmartWeeklyChange:n}=e,d=T.$D.filter(e=>e.active),i=new Map(t.map((e,t)=>[e.tfn,t])),o=e=>{let r=i.get(e.tfn);if(void 0===r)a([...t,e]);else{let s=[...t];s[r]=e,a(s)}},x=e=>{a(t.filter(t=>t.tfn!==e))},m=t.reduce((e,t)=>e+(Number.isFinite(t.throttlePerMinute)?t.throttlePerMinute:0),0),u=t.reduce((e,t)=>e+(Number.isFinite(t.dailyCap)?t.dailyCap:0),0);return(0,r.jsxs)("div",{className:"space-y-4",children:[(0,r.jsxs)("div",{children:[(0,r.jsxs)("h3",{className:"text-sm font-semibold text-gray-900 dark:text-white",children:["Pool: ",t.length," ",1===t.length?"TFN":"TFNs"]}),(0,r.jsx)("p",{className:"text-xs text-gray-500 dark:text-gray-400",children:"Each TFN sends with its own copy and schedule. Empty templates fall back to the campaign-level template below."})]}),n&&(0,r.jsx)("div",{className:"border border-gray-200 dark:border-slate-700 rounded-lg px-4 py-3",children:(0,r.jsxs)("div",{className:"flex items-center justify-between gap-3",children:[(0,r.jsxs)("div",{children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white block",children:"Smart Weekly"}),(0,r.jsx)("p",{className:"text-xs text-gray-500 dark:text-gray-400 mt-0.5",children:"Derives a different daily shape per TFN from its weekly target; the daily total across the pool stays flat. Paired TFNs (1st+2nd, 3rd+4th in pool order) should share the same weekly target for a perfectly flat system total — pool order defines the pairing. Note: with a Mon–Fri send window, actual weekly volume is the target \xd7 5⁄7 (the weights cover all 7 days)."})]}),(0,r.jsx)("button",{type:"button",onClick:()=>n(!l),className:"relative inline-flex h-6 w-11 flex-shrink-0 items-center rounded-full transition-colors ".concat(l?"bg-blue-600":"bg-gray-300 dark:bg-gray-600"),"aria-label":"Toggle Smart Weekly",children:(0,r.jsx)("span",{className:"inline-block h-4 w-4 transform rounded-full bg-white transition-transform ".concat(l?"translate-x-6":"translate-x-1")})})]})}),d.map(e=>{var a,n,d,m,u;let y=i.get(e.number),p=void 0!==y,b=p?t[y]:null,h=b?function(e){let t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return{throttle:!Number.isInteger(e.throttlePerMinute)||e.throttlePerMinute<1||e.throttlePerMinute>30,dailyCap:!Number.isInteger(e.dailyCap)||e.dailyCap<1,window:e.sendWindow.endHour<=e.sendWindow.startHour,days:!Array.isArray(e.sendWindow.days)||0===e.sendWindow.days.length,weeklyTarget:t?!Number.isInteger(e.weeklyTarget)||e.weeklyTarget<1:void 0!==e.weeklyTarget&&(!Number.isInteger(e.weeklyTarget)||e.weeklyTarget<1),emergencyCap:void 0!==e.emergencyCap&&(!Number.isInteger(e.emergencyCap)||e.emergencyCap<1)}}(b,l):null;return(0,r.jsxs)("div",{className:"border rounded-lg ".concat(p?"border-blue-200 dark:border-blue-800 bg-blue-50/30 dark:bg-blue-950/10":"border-gray-200 dark:border-slate-700"),children:[(0,r.jsxs)("label",{className:"flex items-center gap-3 px-4 py-3 cursor-pointer",children:[(0,r.jsx)("input",{type:"checkbox",checked:p,onChange:t=>{t.target.checked?o(function(e){let t=T.$D.find(t=>t.number===e);return t?{tfn:e,throttlePerMinute:t.throttlePerMinute,dailyCap:t.dailyCap,sendWindow:{...t.sendWindow},templates:["","",""]}:{tfn:e,throttlePerMinute:11,dailyCap:2e3,sendWindow:{startHour:10,endHour:13,days:["mon","tue","wed","thu","fri"]},templates:["","",""]}}(e.number)):x(e.number)},className:"w-4 h-4 rounded border-gray-300 dark:border-slate-600"}),(0,r.jsxs)("div",{className:"flex-1",children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white",children:e.label}),(0,r.jsx)("span",{className:"text-xs text-gray-500 dark:text-gray-400 ml-2",children:e.number})]})]}),p&&b&&(0,r.jsxs)("div",{className:"px-4 pb-4 pt-1 space-y-3 border-t border-gray-200 dark:border-slate-700",children:[(0,r.jsxs)("div",{className:"grid grid-cols-2 gap-3",children:[(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1",children:"Throttle (per minute)"}),(0,r.jsx)("input",{type:"number",min:1,max:30,value:b.throttlePerMinute,onChange:e=>o({...b,throttlePerMinute:parseInt(e.target.value||"0",10)}
1),className:"border rounded px-2 py-1 w-full bg-white dark:bg-gray-900 text-sm ".concat(s&&(null==h?void 0:h.throttle)?"border-red-500":"border-gray-300 dark:border-slate-600")}),s&&(null==h?void 0:h.throttle)&&(0,r.jsx)("p",{className:"text-xs text-red-600 mt-0.5",children:"1–30 per minute"})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1",children:"Daily cap"}),(0,r.jsx)("input",{type:"number",min:1,value:b.dailyCap,disabled:l,onChange:e=>o({...b,dailyCap:parseInt(e.target.value||"0",10)}),className:"border rounded px-2 py-1 w-full text-sm ".concat(l?"bg-gray-100 dark:bg-gray-800 text-gray-400 cursor-not-allowed":"bg-white dark:bg-gray-900"," ").concat(s&&(null==h?void 0:h.dailyCap)?"border-red-500":"border-gray-300 dark:border-slate-600")}),l?(0,r.jsx)("p",{className:"text-xs text-gray-400 mt-0.5",children:"Overridden by Smart Weekly (derived from weekly target)"}):s&&(null==h?void 0:h.dailyCap)?(0,r.jsx)("p",{className:"text-xs text-red-600 mt-0.5",children:"Must be ≥ 1"}):null]})]}),(0,r.jsxs)("div",{className:"grid grid-cols-2 gap-3",children:[(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1",children:"Weekly target"}),(0,r.jsx)("input",{type:"number",min:1,placeholder:"—",value:null!==(a=b.weeklyTarget)&&void 0!==a?a:"",onChange:e=>{let t=parseInt(e.target.value,10);if(Number.isInteger(t))o({...b,weeklyTarget:t});else{let{weeklyTarget:e,...t}=b;o(t)}},className:"border rounded px-2 py-1 w-full bg-white dark:bg-gray-900 text-sm ".concat(s&&(null==h?void 0:h.weeklyTarget)?"border-red-500":"border-gray-300 dark:border-slate-600")}),s&&(null==h?void 0:h.weeklyTarget)&&(0,r.jsx)("p",{className:"text-xs text-red-600 mt-0.5",children:l?"Required while Smart Weekly is on (positive integer)":"Must be a positive integer"})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1",children:"Emergency cap"}),(0,r.jsx)("input",{type:"number",min:1,placeholder:"—",value:null!==(n=b.emergencyCap)&&void 0!==n?n:"",onChange:e=>{let t=parseInt(e.target.value,10);if(Number.isInteger(t))o({...b,emergencyCap:t});else{let{emergencyCap:e,...t}=b;o(t)}},className:"border rounded px-2 py-1 w-full bg-white dark:bg-gray-900 text-sm ".concat(s&&(null==h?void 0:h.emergencyCap)?"border-red-500":"border-gray-300 dark:border-slate-600")}),(0,r.jsx)("p",{className:"text-xs text-gray-400 mt-0.5",children:"Hard per-day ceiling (incident throttle / re-entry ramp). Lost volume is never redistributed."}),s&&(null==h?void 0:h.emergencyCap)&&(0,r.jsx)("p",{className:"text-xs text-red-600 mt-0.5",children:"Must be a positive integer"})]})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1",children:"Send window"}),(0,r.jsxs)("div",{className:"flex items-center gap-2 mb-2",children:[(0,r.jsx)("input",{type:"number",min:0,max:23,value:b.sendWindow.startHour,onChange:e=>o({...b,sendWindow:{...b.sendWindow,startHour:parseInt(e.target.value||"0",10)}}),className:"border rounded px-2 py-1 w-16 bg-white dark:bg-gray-900 text-sm ".concat(s&&(null==h?void 0:h.window)?"border-red-500":"border-gray-300 dark:border-slate-600")}),(0,r.jsx)("span",{className:"text-xs text-gray-500",children:"to"}),(0,r.jsx)("input",{type:"number",min:0,max:23,value:b.sendWindow.endHour,onChange:e=>o({...b,sendWindow:{...b.sendWindow,endHour:parseInt(e.target.value||"0",10)}}),className:"border rounded px-2 py-1 w-16 bg-white dark:bg-gray-900 text-sm ".concat(s&&(null==h?void 0:h.window)?"border-red-500":"border-gray-300 dark:border-slate-600")}),(0,r.jsx)("span",{className:"text-xs text-gray-500",children:"(0–23 hr, lead local time)"})]}),s&&(null==h?void 0:h.window)&&(0,r.jsx)("p",{className:"text-xs text-red-600 mb-1",children:"End must be after start"}),(0,r.jsx)("div",{className:"flex gap-1 flex-wrap",children:D.map(e=>{let t=b.sendWindow.days.includes(e);return(0,r.jsx)("button",{type:"button",onClick:()=>{let a=t?b.sendWindow.days.filter(t=>t!==e):[...b.sendWindow.days,e];o({...b,sendWindow:{...b.sendWindow,days:a}})},className:"px-2 py-1 rounded text-xs border ".concat(t?"bg-blue-600 text-white border-blue-700":"bg-white dark:bg-gray-900 text-gray-700 dark:text-gray-300 border-gray-300 dark:border-slate-600"),children:E[e]},e)})}),s&&(null==h?void 0:h.days)&&(0,r.jsx)("p",{className:"text-xs text-red-600 mt-1",children:"Pick at least one day"})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1",children:"Pitch preference"}),(0,r.jsxs)("select",{value:null!==(d=b.pitchPreference)&&void 0!==d?d:"",onCh
1ange:e=>{let t=e.target.value;if("low_reviews"===t||"high_reviews"===t)o({...b,pitchPreference:t});else{let{pitchPreference:e,...t}=b;o(t)}},className:"w-full px-2 py-1 text-sm rounded border border-gray-300 dark:border-slate-600 bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100",children:[(0,r.jsx)("option",{value:"",children:"No preference"}),(0,r.jsx)("option",{value:"low_reviews",children:"Small businesses (≤5 reviews)"}),(0,r.jsx)("option",{value:"high_reviews",children:"Established businesses (6+ reviews)"})]}),(0,r.jsx)("p",{className:"text-xs text-gray-500 dark:text-gray-400 mt-1",children:"Preferred lead segment for this TFN. Send queue sorts preferred matches to the front; overflow leads still send with this TFN's normal templates."})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1",children:"Templates"}),(0,r.jsxs)("p",{className:"text-xs text-gray-500 dark:text-gray-400 mb-2",children:["Variables: ",F.join(" ")]}),(0,r.jsx)("div",{className:"space-y-2",children:(null!==(m=b.templates)&&void 0!==m?m:[]).map((e,t)=>{var a;return(0,r.jsxs)("div",{className:"flex gap-2 items-start",children:[(0,r.jsx)("textarea",{value:e,rows:2,onChange:e=>{var a;let r=[...null!==(a=b.templates)&&void 0!==a?a:[]];r[t]=e.target.value,o({...b,templates:r})},placeholder:"Template ".concat(t+1),className:"border border-gray-300 dark:border-slate-600 rounded px-2 py-1 w-full bg-white dark:bg-gray-900 text-sm font-mono"}),(0,r.jsx)("button",{type:"button",disabled:(null!==(a=b.templates)&&void 0!==a?a:[]).length<=1,onClick:()=>{var e;let a=(null!==(e=b.templates)&&void 0!==e?e:[]).filter((e,a)=>a!==t);o({...b,templates:a})},className:"text-gray-400 hover:text-red-600 disabled:opacity-30 disabled:cursor-not-allowed mt-1","aria-label":"Remove template ".concat(t+1),children:(0,r.jsx)(c.Z,{size:16})})]},t)})}),(0,r.jsxs)("button",{type:"button",onClick:()=>{var e;return o({...b,templates:[...null!==(e=b.templates)&&void 0!==e?e:[],""]})},className:"mt-2 text-xs text-blue-600 hover:text-blue-700 flex items-center gap-1",children:[(0,r.jsx)(g.Z,{size:12})," Add another template"]}),(null!==(u=b.templates)&&void 0!==u?u:[]).every(e=>!e||!e.trim())&&(0,r.jsx)("p",{className:"text-xs text-amber-600 dark:text-amber-400 mt-2",children:"ⓘ All templates blank — will use campaign-level fallback"})]})]})]},e.number)}),t.length>0&&(0,r.jsx)("div",{className:"text-xs text-gray-500 dark:text-gray-400 border-t border-gray-200 dark:border-slate-700 pt-3",children:l?(0,r.jsxs)(r.Fragment,{children:["Pool: ",t.length," TFNs \xb7 ~",m,"/min combined max \xb7 ~",Math.round(t.reduce((e,t)=>e+(Number.isFinite(t.weeklyTarget)?t.weeklyTarget:0),0)/7).toLocaleString(),"/day system average (Smart Weekly)"]}):(0,r.jsxs)(r.Fragment,{children:["Pool: ",t.length," TFNs \xb7 ~",m,"/min combined max \xb7 ~",u.toLocaleString()," daily cap"]})})]})}let L=["{companyName}","{firstName}","{city}","{state}","{industry}","{previewUrl}"];function A(e){let{campaignId:t}=e,[a,l]=(0,s.useState)([]),[n,d]=(0,s.useState)(!0),[i,o]=(0,s.useState)(!0),[c,x]=(0,s.useState)(!1),[g,m]=(0,s.useState)(null),[u,y]=(0,s.useState)({}),p=(0,s.useCallback)(async()=>{d(!0),o(!0);try{let e=await fetch("/api/campaigns/".concat(t,"/per-tfn-variant-stats"));if(e.ok){let t=await e.json();l(t.tfns||[]);let a={};for(let e of t.tfns||[])a[e.tfn]=[...e.templates||[]];y(a)}}catch(e){console.error("[PerTfnTemplateVariantsSection] fetch failed:",e)}finally{d(!1),o(!1)}},[t]);(0,s.useEffect)(()=>{p()},[p]);let b=(e,t,a)=>{y(r=>{let s={...r},l=[...s[e]||[]];return l[t]=a,s[e]=l,s})},h=e=>{y(t=>{let a={...t};return a[e]=[...a[e]||[],""],a})},f=(e,t)=>{y(a=>{let r={...a};return r[e]=(r[e]||[]).filter((e,a)=>a!==t),r})},k=(e,t,a)=>{let r=document.activeElement;if((null==r?void 0:r.tagName)==="TEXTAREA"&&r.dataset.tfn===e&&r.dataset.variantIdx===String(t)){var s,l,n;let d=null!==(l=r.selectionStart)&&void 0!==l?l:r.value.length,i=(null===(s=u[e])||void 0===s?void 0:s[t])||"";b(e,t,i.slice(0,d)+a+i.slice(null!==(n=r.selectionEnd)&&void 0!==n?n:d)),setTimeout(()=>{r.focus(),r.selectionStart=r.selectionEnd=d+a.length},0)}},v=async()=>{x(!0),m(null);try{var e;let a=await fetch("/api/campaigns/".concat(t));if(!a.ok){m("Failed to read current campaign");return}let{campaign:r}=await a.json(),s=(null==r?void 0:null===(e=r.settings)||void 0===e?void 0:e.tfnPool)||[];if(0===s.length){m("Campaign has no TFN pool");return}let l=s.map(e=>{let t=(u[e.tfn]||[]).filter(e=>e&&e.trim().length>0);return{...e,templates:t.length>0?t:void 0}});if(l.flatMap(e=>(e.templates||[]).map(t=>({tfn:e.tfn,text:t}))).find(e=>e.text.includes("{firstName}"))){m("Variable {firstName} not allowed. Scraper does not capture first names."),x(!1),setTimeout(()=>m(null),3e3);return}let n=await fetch("/api/campaigns/".concat(t),{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({tfnPool:l})});if(n.ok)m("Saved"),await p();else{let e=await n.json().catch(()=>({}));m(e.error||"Save failed")}setTimeout(()=>m(null),3e3)}catch(e){console.error("[PerTfnTemplateVariantsSection] save failed:",e),m("Network error")}finally{x(!1)}};return n?(0,r.jsx)("div",{className:"border border-gray-100 dark:border-gray-700 rounded-lg p-4",children:(0,r.jsx)("div",{className:"flex items-center justify-center py-6",children:(0,r.jsx)("div",{className:"animate-spin rounded-full h-5 w-5 border-b-2 border-blue-600"})})}):0===a.length?(0,r.jsx)("div",{className:"border border-gray-100 dark:border-gray-700 rounded-lg p-4",children:(0,r.jsx)("p",{className:"text-sm text-gray-500 dark:text-gray-400",children:"This campaign is not multi-TFN. Use the Template Variants section above."})}):(0,r.jsxs)("div",{className:"space-y-4",children:[(0,r.jsx)("div",{className:"border border-blue-200 dark:border-blue-800 bg-blue-50/30 dark:bg-blue-950/10 rounded-lg p-3",children:(0,r.jsxs)("p",{className:"text-xs text-blue-700 dark:text-blue-300",children:[(0,r.jsx)("strong",{children:"Multi-TFN campaign."})," Each TFN has its own template set. Edit them below — saves persist to the campaign's TFN pool config."]})}),a.map(e=>{let t=u[e.tfn]||[],a=e.variants.filter(e=>null!==e.variantIndex&&e.delivered>0),s=a.length>0?a.reduce((e,t)=>
1e+parseFloat(t.clickRate||"0"),0)/a.length:0,l=a.length>0?a.reduce((e,t)=>e+parseFloat(t.replyRate||"0"),0)/a.length:0,n=a.length>0?a.reduce((e,t)=>e+parseFloat(t.optOutRate||"0"),0)/a.length:0;return(0,r.jsxs)("div",{className:"border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:[(0,r.jsx)("div",{className:"flex items-center justify-between mb-3",children:(0,r.jsxs)("div",{children:[(0,r.jsxs)("span",{className:"text-sm font-semibold text-gray-900 dark:text-white",children:[e.label,(()=>{let t=T.$D.find(t=>t.number===e.tfn);return(null==t?void 0:t.personaName)?" — ".concat(t.personaName):""})()]}),(0,r.jsx)("span",{className:"text-xs text-gray-500 dark:text-gray-400 ml-2",children:e.tfn})]})}),(0,r.jsx)("p",{className:"text-xs text-gray-400 mb-3",children:"Each variant is a complete SMS template. One is picked at random per send for this TFN. If empty, falls back to the campaign-level template."}),(0,r.jsx)("div",{className:"flex flex-wrap gap-1.5 mb-3",children:L.map(t=>(0,r.jsx)("button",{type:"button",onClick:()=>{let a=document.activeElement;if((null==a?void 0:a.tagName)==="TEXTAREA"&&a.dataset.tfn===e.tfn){let r=parseInt(a.dataset.variantIdx||"0",10);k(e.tfn,r,t)}},className:"text-xs bg-blue-50 dark:bg-blue-900/30 text-blue-600 dark:text-blue-400 hover:bg-blue-100 dark:hover:bg-blue-900/50 border border-blue-200 dark:border-blue-800 px-2 py-0.5 rounded",children:t},t))}),(0,r.jsx)("div",{className:"space-y-3",children:t.map((t,a)=>{let d=t.length,o=Math.ceil(d/160)||0,c=e.variants.find(e=>e.variantIndex===a);return(0,r.jsxs)("div",{children:[(0,r.jsx)("textarea",{rows:3,"data-tfn":e.tfn,"data-variant-idx":a,value:t,onChange:t=>b(e.tfn,a,t.target.value),className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white font-mono text-sm",placeholder:"Hey, free preview for {companyName} in {city}: {previewUrl} — ".concat(e.label)}),(0,r.jsxs)("div",{className:"flex items-center justify-between mt-1",children:[(0,r.jsxs)("span",{className:"text-xs text-gray-400",children:[d," chars \xb7 ",o," segment",1!==o?"s":""]}),(0,r.jsx)("button",{onClick:()=>f(e.tfn,a),className:"text-red-400 hover:text-red-600 text-xs",children:"Remove"})]}),c&&c.sent>0&&(0,r.jsx)("div",{className:"mt-2 flex flex-wrap gap-x-4 gap-y-1 bg-gray-50 dark:bg-gray-900/50 rounded-lg px-3 py-2",children:[{label:"Sent",val:c.sent.toLocaleString()},{label:"Del.",val:c.delivered.toLocaleString()},{label:"Del. Rate",val:"".concat(c.deliveryRate,"%")},{label:"Clicked",val:c.clicked.toLocaleString()},{label:"Click Rate",val:"".concat(c.clickRate,"%"),color:parseFloat(c.clickRate)>=s?"text-emerald-600":"text-red-500"},{label:"Opt-Out",val:c.optedOut.toLocaleString()},{label:"Opt-Out %",val:"".concat(c.optOutRate,"%"),color:parseFloat(c.optOutRate)<=n?"text-emerald-600":"text-red-500"},{label:"Replied",val:c.replied.toLocaleString()},{label:"Reply %",val:"".concat(c.replyRate,"%"),color:parseFloat(c.replyRate)>=l?"text-emerald-600":"text-red-500"}].map(e=>(0,r.jsxs)("div",{className:"text-center min-w-[48px]",children:[(0,r.jsx)("p",{className:"text-xs font-semibold tabular-nums ".concat(e.color||"text-gray-900 dark:text-white"),children:e.val}),(0,r.jsx)("p",{className:"text-[9px] text-gray-400 leading-tight",children:e.label})]},e.label))}),i&&!c&&(0,r.jsx)("div",{className:"mt-1 text-[10px] text-gray-400 animate-pulse",children:"Loading stats..."})]},a)})}),(0,r.jsxs)("div",{className:"flex items-center justify-between mt-3",children:[(0,r.jsx)("button",{onClick:()=>h(e.tfn),className:"px-3 py-1.5 bg-blue-600 text-white rounded-lg text-xs font-medium hover:bg-blue-700",children:"Add Variant"}),(0,r.jsxs)("span",{className:"text-xs text-gray-400",children:[t.filter(e=>e.trim()).length," variant",1!==t.filter(e=>e.trim()).length?"s":""]})]})]},e.tfn)}),(0,r.jsxs)("div",{className:"flex items-center justify-end gap-3 pt-2",children:[g&&(0,r.jsx)("span",{className:"text-xs ".concat("Saved"===g?"text-emerald-600":"text-red-500"),children:g}
1),(0,r.jsx)("button",{onClick:v,disabled:c,className:"px-4 py-1.5 bg-blue-600 text-white rounded-lg text-sm font-medium hover:bg-blue-700 disabled:opacity-50",children:c?"Saving...":"Save All TFN Templates"})]})]})}var P=a(73875);let I={DRAFT:{bg:"bg-gray-100 dark:bg-gray-700",text:"text-gray-700 dark:text-gray-300"},SENDING:{bg:"bg-blue-100 dark:bg-blue-900/40",text:"text-blue-700 dark:text-blue-300"},PAUSED:{bg:"bg-yellow-100 dark:bg-yellow-900/40",text:"text-yellow-700 dark:text-yellow-300"},COMPLETED:{bg:"bg-green-100 dark:bg-green-900/40",text:"text-green-700 dark:text-green-300"},ARCHIVED:{bg:"bg-gray-100 dark:bg-gray-700",text:"text-gray-500 dark:text-gray-400"}},_=[{key:"QUEUED",label:"Queued",color:"bg-gray-400",bgColor:"bg-gray-100 dark:bg-gray-700",textColor:"text-gray-700 dark:text-gray-300"},{key:"TEXTED",label:"Texted",color:"bg-blue-500",bgColor:"bg-blue-100 dark:bg-blue-900/40",textColor:"text-blue-700 dark:text-blue-300"},{key:"CLICKED",label:"Clicked",color:"bg-orange-500",bgColor:"bg-orange-100 dark:bg-orange-900/40",textColor:"text-orange-700 dark:text-orange-300"},{key:"REP_CALLED",label:"Rep Called",color:"bg-purple-500",bgColor:"bg-purple-100 dark:bg-purple-900/40",textColor:"text-purple-700 dark:text-purple-300"},{key:"OPTED_IN",label:"Opted In",color:"bg-green-500",bgColor:"bg-green-100 dark:bg-green-900/40",textColor:"text-green-700 dark:text-green-300"},{key:"HOT",label:"Hot",color:"bg-red-500",bgColor:"bg-red-100 dark:bg-red-900/40",textColor:"text-red-700 dark:text-red-300"},{key:"SET",label:"Set",color:"bg-indigo-500",bgColor:"bg-indigo-100 dark:bg-indigo-900/40",textColor:"text-indigo-700 dark:text-indigo-300"},{key:"CLOSED",label:"Closed",color:"bg-emerald-500",bgColor:"bg-emerald-100 dark:bg-emerald-900/40",textColor:"text-emerald-700 dark:text-emerald-300"},{key:"OPTED_OUT",label:"Opted Out",color:"bg-slate-400",bgColor:"bg-slate-100 dark:bg-slate-700",textColor:"text-slate-600 dark:text-slate-300"},{key:"ARCHIVED",label:"Archived",color:"bg-gray-300",bgColor:"bg-gray-100 dark:bg-gray-700",textColor:"text-gray-500 dark:text-gray-400"}],O=["{companyName}","{city}","{state}","{industry}","{previewUrl}"],M={COLD_TEXT_SENT:"\uD83D\uDCE4",DELIVERED:"✅",PREVIEW_CLICKED:"\uD83D\uDC41️",REP_CALLED:"\uD83D\uDCDE",OPTED_IN:"✅",DRIP_SENT:"\uD83D\uDCE4",LEAD_REPLIED:"\uD83D\uDCAC",MARKED_HOT:"\uD83D\uDD25",CLOSED:"\uD83D\uDCB0",OPTED_OUT:"\uD83D\uDED1"};function Z(e,t){return t?(e/t*100).toFixed(1):"0.0"}let z=e=>null===e?"text-gray-400":e>=70?"text-green-600 dark:text-green-400":e>=50?"text-amber-600 dark:text-amber-400":"text-red-600 dark:text-red-400",W=e=>null===e?"bg-gray-300":e>=70?"bg-green-500":e>=50?"bg-amber-500":"bg-red-500";function U(){let[e,t]=(0,s.useState)("list"),[a,n]=(0,s.useState)([]),[d,i]=(0,s.useState)(null),[o,c]=(0,s.useState)(!0),x=(0,s.useCallback)(async()=>{try{let e=await fetch("/api/campaigns");if(e.ok){let t=await e.json();n(t.campaigns||[])}}catch(e){console.error("Failed to fetch campaigns:",e)}finally{c(!1)}},[]);(0,s.useEffect)(()=>{x()},[x]);let g=(0,s.useCallback)(async()=>{try{let e=await fetch("/api/campaigns/live-stats");if(!e.ok)return;let t=await e.json(),a=null==t?void 0:t.stats;if(!a)return;n(e=>e.map(e=>{let t=a[e.id];return t?{...e,sentCount:t.texted,deliveredCount:t.delivered,clickedCount:t.clicked,optedInCount:t.optedIn}:e}))}catch(e){}},[]);(0,l.C)(g,1e4,"list"===e);let m=(0,s.useCallback)(async e=>{try{let t=await fetch("/api/campaigns/".concat(e.id));if(t.ok){let a=await t.json();i(a.campaign||e)}else i(e)}catch(t){i(e)}t("detail")},[]),u=(0,s.useCallback)(()=>{t("list"),i(null),x()},[x]);return(0,r.jsxs)("div",{className:"space-y-6",children:[(0,r.jsx)("div",{className:"flex items-center gap-2",children:(0,r.jsx)("button",{onClick:()=>t("list"),className:"px-4 py-2 rounded-lg text-sm font-medium transition-colors ".concat("list"===e?"bg-blue-600 text-white":"bg-white dark:bg-gray-800 text-gray-700 dark:text-gray-300 border border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700"),children:"Campaigns"})}),"list"===e&&(0,r.jsx)(Y,{campaigns:a,loading:o,onSelect:m,onRefresh:x}),"detail"===e&&d&&(0,r.jsx)(ee,{campaign:d,onBack:u,onRefresh:async()=>{try{let e=await fetch("/api/campaigns/".concat(d.id));if(e.ok){let t=await e.json();i(t.campaign)}}catch(e){console.error("Failed to refresh campaign:",e)}}})]})}function H(e){let{label:t,value:a}=e;return(0,r.jsxs)("div",{className:"text-center",children:[(0,r.jsx)("p",{className:"text-lg font-semibold tabular-nums ".concat(0===a?"text-gray-500 dark:text-gray-400":"text-red-600 dark:text-red-400"),children:a.toLocaleStr
1ing()}),(0,r.jsx)("p",{className:"text-[10px] text-gray-500 dark:text-gray-400",children:t})]})}let q=e=>null===e?"text-gray-400":e<=1?"text-green-600 dark:text-green-400":e<=3?"text-amber-600 dark:text-amber-400":"text-red-600 dark:text-red-400",B=(e,t)=>{if(null===t||0===t)return"bg-gray-300";let a=e/t*100;return a>=100?"bg-red-500":a>=80?"bg-amber-500":"bg-green-500"};function V(e){var t,a,l,n,o,c,x,g,m,u,y,p,b,h,f,k;let{campaignId:v,startDate:j,endDate:N}=e,[C,S]=(0,s.useState)(null),[T,D]=(0,s.useState)(!0),[E,F]=(0,s.useState)(!1),[R,L]=(0,s.useState)(null),[A,P]=(0,s.useState)(null),[I,_]=(0,s.useState)(null),[O,M]=(0,s.useState)(!1),[Z,W]=(0,s.useState)(null),[U,H]=(0,s.useState)("500"),[V,K]=(0,s.useState)(!1),[Q,J]=(0,s.useState)(!1),[Y,G]=(0,s.useState)(null),[X,$]=(0,s.useState)(0);(0,s.useEffect)(()=>{let e=!1;D(!0);let t=new URLSearchParams;j&&t.set("startDate",j),N&&t.set("endDate",N);let a=t.toString();return fetch("/api/campaigns/".concat(v,"/tfn-breakdown").concat(a?"?"+a:"")).then(e=>e.ok?e.json():null).then(t=>{!e&&t&&S(t)}).catch(()=>{}).finally(()=>{e||D(!1)}),()=>{e=!0}},[v,j,N,X]);let ee=(0,s.useCallback)(function(){let e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];fetch("/api/campaigns/".concat(v,"/recold-available").concat(e?"?force=1":"")).then(e=>e.ok?e.json():null).then(e=>{e&&P(e)}).catch(()=>{})},[v]);(0,s.useEffect)(()=>{ee(!1)},[ee]);let et=(0,s.useCallback)(function(){let e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];fetch("/api/campaigns/".concat(v,"/feeder-stats").concat(e?"?force=1":"")).then(e=>e.ok?e.json():null).then(e=>{e&&_(e)}).catch(()=>{})},[v]);(0,s.useEffect)(()=>{et(!1)},[et]);let ea=async()=>{if(window.confirm("Run the daily feed now for this campaign? Enrols up to 3,000 backfill + 3,000 fresh into the pool. Safe to repeat — already-enrolled leads are skipped.")){M(!0),W(null);try{let t=await fetch("/api/campaigns/".concat(v,"/feed-now"),{method:"POST"}),a=await t.json().catch(()=>null);if(t.ok)W("Enrolled ".concat(a.enrolled.backfill.toLocaleString()," backfill + ").concat(a.enrolled.fresh.toLocaleString()," fresh"));else{var e;W(null!==(e=null==a?void 0:a.error)&&void 0!==e?e:"Feed failed")}}catch(e){W("Feed failed")}finally{M(!1),et(!0),$(e=>e+1)}}},er=async()=>{if(A&&!Q){J(!0),G(null);try{let e=await fetch("/api/campaigns/".concat(v,"/recold-mode"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({manual:!A.manualMode})});if(e.ok){let t=await e.json();P(e=>e?{...e,manualMode:t.manualMode}:e)}else G("Could not change mode")}catch(e){G("Could not change mode")}finally{J(!1)}}},es=async()=>{if(V)return;let e=parseInt(U,10);if(!Number.isFinite(e)||e<=0){G("Enter a positive number");return}K(!0),G(null);try{let a=await fetch("/api/campaigns/".concat(v,"/recold-fill"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({count:e})}),r=await a.json().catch(()=>({}));if(a.ok){var t;P(e=>{var t,a;return e?{...e,available:null!==(t=r.availableRemaining)&&void 0!==t?t:e.available,availableCapped:null!==(a=r.availableCapped)&&void 0!==a?a:e.availableCapped}:e}),G("Loaded ".concat(Number(null!==(t=r.filled)&&void 0!==t?t:0).toLocaleString()," lead").concat(1===r.filled?"":"s"," into the pool")),$(e=>e+1)}else G(r.error||"Fill failed")}catch(e){G("Fill failed")}finally{K(!1)}};if(T&&!C)return(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow border border-gray-200 dark:border-gray-700 p-4",children:[(0,r.jsx)("div",{className:"h-5 w-32 mb-3 bg-gray-200 dark:bg-gray-700 rounded animate-pulse"}),(0,r.jsx)("div",{className:"h-32 bg-gray-100 dark:bg-gray-700/50 rounded animate-pulse"})]});if(!C)return null;let{tfns:el,combined:en}=C,ed=!0===C.pooledRouting,ei=el.filter(e=>"pool"!==e.number).length,eo=null!==(x=null===(t=el.find(e=>"pool"===e.number))||void 0===t?void 0:t.queued)&&void 0!==x?x:0,ec=en.dailyCapCampaign.cap,ex=en.dailyCapCampaign.sentToday,eg=e=>null===e?(0,r.jsx)("span",{className:"text-gray-400",children:"--"}):(0,r.jsxs)("span",{className:z(e),children:[e,"%"]}),em=e=>null===e?(0,r.jsx)("span",{className:"text-gray-400",children:"--"}):(0,r.jsxs)("span",{className:q(e),children:[e,"%"]}),eu=(e,t)=>
1(0,r.jsxs)("div",{className:"text-right",children:[(0,r.jsxs)("div",{className:"tabular-nums text-sm text-gray-700 dark:text-gray-300",children:[e.toLocaleString(),null!==t?(0,r.jsxs)("span",{className:"text-gray-400 dark:text-gray-500",children:[" / ",t.toLocaleString()]}):null]}),null!==t&&(0,r.jsx)("div",{className:"h-1 rounded-full bg-gray-200 dark:bg-gray-700 mt-1 overflow-hidden",children:(0,r.jsx)("div",{className:"h-full rounded-full ".concat(B(e,t)),style:{width:"".concat(Math.min(100,e/t*100),"%")}})})]});return(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow border border-gray-200 dark:border-gray-700",children:[(0,r.jsx)("div",{className:"px-4 py-3 border-b border-gray-200 dark:border-gray-700",children:(0,r.jsx)("h3",{className:"text-sm font-semibold text-gray-700 dark:text-gray-300",children:"TFN Breakdown"})}),ed&&(0,r.jsxs)("div",{className:"px-4 py-3 border-b border-gray-100 dark:border-gray-700/50 bg-gray-50/60 dark:bg-gray-800/40 flex flex-wrap items-center gap-x-8 gap-y-2",children:[(0,r.jsxs)("div",{className:"min-w-[220px] flex-1 max-w-sm",children:[(0,r.jsxs)("div",{className:"flex items-baseline justify-between",children:[(0,r.jsx)("span",{className:"text-[11px] uppercase tracking-wider text-gray-400 dark:text-gray-500",children:"Total daily cap"}),(0,r.jsxs)("span",{className:"tabular-nums text-sm font-semibold text-gray-700 dark:text-gray-200",children:[ex.toLocaleString(),null!==ec&&(0,r.jsxs)("span",{className:"font-normal text-gray-400 dark:text-gray-500",children:[" / ",ec.toLocaleString()]})]})]}),null!==ec&&ec>0&&(0,r.jsx)("div",{className:"h-1.5 rounded-full bg-gray-200 dark:bg-gray-700 mt-1.5 overflow-hidden",children:(0,r.jsx)("div",{className:"h-full rounded-full ".concat(B(ex,ec)),style:{width:"".concat(Math.min(100,ex/ec*100),"%")}})}),(0,r.jsxs)("div",{className:"text-[10px] text-gray-400 dark:text-gray-500 mt-1",children:["Sent today across all TFNs \xb7 sum of the ",ei," lane caps below"]})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("div",{className:"text-[11px] uppercase tracking-wider text-gray-400 dark:text-gray-500",children:"Pool queue"}),(0,r.jsx)("div",{className:"tabular-nums text-sm font-semibold text-gray-700 dark:text-gray-200",children:eo.toLocaleString()}),(0,r.jsxs)("div",{className:"text-[10px] text-gray-400 dark:text-gray-500 mt-0.5",children:["unbound \xb7 splits evenly across ",ei," TFNs at send time"]})]}),A&&(0,r.jsxs)("div",{className:"min-w-[300px] flex-1 max-w-md border-l border-gray-200 dark:border-gray-700 pl-6",children:[(0,r.jsxs)("div",{className:"flex items-center justify-between gap-2",children:[(0,r.jsx)("div",{className:"text-[11px] uppercase tracking-wider text-gray-400 dark:text-gray-500",children:"Recalled leads available"}),(0,r.jsx)("button",{type:"button",onClick:er,disabled:Q,title:A.manualMode?"Manual: the automatic 60+ day backfill is OFF for this campaign — you fill the queue by hand.":"Auto: the system backfills recalled leads automatically when fresh supply runs short.",className:"text-[10px] px-2 py-0.5 rounded-full border transition-colors ".concat(A.manualMode?"bg-indigo-50 dark:bg-indigo-900/30 border-indigo-300 dark:border-indigo-700 text-indigo-700 dark:text-indigo-300":"bg-gray-100 dark:bg-gray-700 border-gray-300 dark:border-gray-600 text-gray-500 dark:text-gray-400"," ").concat(Q?"opacity-50 cursor-wait":"hover:opacity-80"),children:A.manualMode?"● Manual fill":"○ Auto fill"})]}),(0,r.jsxs)("div",{className:"tabular-nums text-sm font-semibold text-gray-700 dark:text-gray-200 mt-0.5",children:[A.available.toLocaleString(),A.availableCapped?"+":"",(0,r.jsx)("span",{className:"font-normal text-[10px] text-gray-400 dark:text-gray-500",children:" \xb7 60+ days cold, updates daily"})]}),(0,r.jsxs)("div",{className:"flex items-center gap-2 mt-1.5",children:[(0,r.jsx)("input",{type:"number",min:1,value:U,onChange:e=>H(e.target.value),disabled:V,className:"w-24 px-2 py-1 text-sm rounded border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-900 text-gray-700 dark:text-gray-200 tabular-nums disabled:opacity-50",placeholder:"Count"}),(0,r.jsx)("button",{type:"button",onClick:es,disabled:V||0===A.available,className:"px-3 py-1 text-sm rounded bg-indigo-600 hover:bg-indigo-700 text-white font-medium disabled:opacity-50 disabled:cursor-not-allowed transition-colors",children:V?"Filling…":"Fill Queue"})]}),Y&&(0,r.jsx)("div",{className:"text-[10px] text-gray-500 dark:text-gray-400 mt-1",children:Y}),!A.recoldEnabledGlobal&&(0,r.jsx)("div",{className:"text-[10px] text-amber-600 dar
1k:text-amber-400 mt-1",children:"Note: the global ReCold switch is off — manual fills still queue leads, but the auto backfill is disabled platform-wide."})]}),(null==I?void 0:I.isFeedTarget)&&(0,r.jsxs)("div",{className:"min-w-[300px] flex-1 max-w-md border-l border-gray-200 dark:border-gray-700 pl-6",children:[(0,r.jsxs)("div",{className:"flex items-center justify-between gap-2",children:[(0,r.jsx)("div",{className:"text-[11px] uppercase tracking-wider text-gray-400 dark:text-gray-500",children:"Daily feed"}),(0,r.jsxs)("span",{title:"The switch lives in smart_search_constants (campaign_feed_enabled). This pill is display only.",className:"text-[10px] px-2 py-0.5 rounded-full border ".concat(I.enabled?"bg-green-50 dark:bg-green-900/30 border-green-300 dark:border-green-700 text-green-700 dark:text-green-300":"bg-gray-100 dark:bg-gray-700 border-gray-300 dark:border-gray-600 text-gray-500 dark:text-gray-400"),children:[I.enabled?"ON":"OFF"," \xb7 ",I.scheduleLabel]})]}),(0,r.jsxs)("div",{className:"tabular-nums text-sm font-semibold text-gray-700 dark:text-gray-200 mt-0.5",children:[(null!==(g=I.addedToday)&&void 0!==g?g:0).toLocaleString()," today",(0,r.jsxs)("span",{className:"font-normal text-[10px] text-gray-400 dark:text-gray-500",children:[" \xb7 ",(null!==(m=I.addedAllTime)&&void 0!==m?m:0).toLocaleString()," all-time"]})]}),(0,r.jsxs)("div",{className:"text-[10px] text-gray-500 dark:text-gray-400 mt-0.5",children:["Eligible left: ",(null!==(u=null===(a=I.eligibleRemaining)||void 0===a?void 0:a.total)&&void 0!==u?u:0).toLocaleString()," ","(",(null!==(y=null===(l=I.eligibleRemaining)||void 0===l?void 0:l.backfill)&&void 0!==y?y:0).toLocaleString()," backfill \xb7 ",(null!==(p=null===(n=I.eligibleRemaining)||void 0===n?void 0:n.fresh)&&void 0!==p?p:0).toLocaleString()," fresh)"]}),(0,r.jsxs)("div",{className:"text-[10px] text-gray-500 dark:text-gray-400",children:["Last feed ",I.lastFeed?new Date(I.lastFeed).toLocaleString("en-US",{timeZone:"America/New_York",month:"short",day:"numeric",hour:"numeric",minute:"2-digit"})+" ET":"never"]}),I.lastRun&&(0,r.jsxs)("div",{className:"text-[10px] ".concat("ok"===I.lastRun.status?"text-gray-500 dark:text-gray-400":"text-red-600 dark:text-red-400"),children:["Last run: ",I.lastRun.trigger," \xb7"," ",I.lastRun.backfill.toLocaleString(),"/",I.lastRun.fresh.toLocaleString()," \xb7"," ",I.lastRun.status]}),(0,r.jsxs)("div",{className:"text-[10px] ".concat((null!==(b=I.harvestPending)&&void 0!==b?b:0)>0?"text-amber-600 dark:text-amber-400":"text-gray-500 dark:text-gray-400"),children:["Preview refresh pending: ",(null!==(h=I.harvestPending)&&void 0!==h?h:0).toLocaleString()]}),(0,r.jsx)("div",{className:"flex items-center gap-2 mt-1.5",children:(0,r.jsx)("button",{type:"button",onClick:ea,disabled:O,className:"px-3 py-1 text-sm rounded bg-indigo-600 hover:bg-indigo-700 text-white font-medium disabled:opacity-50 disabled:cursor-not-allowed transition-colors",children:O?"Feeding…":"Feed now"})}),Z&&(0,r.jsx)("div",{className:"text-[10px] text-gray-500 dark:text-gray-400 mt-1",children:Z})]})]}),(0,r.jsx)("div",{className:"px-4 pb-4 pt-3 overflow-x-auto",children:(0,r.jsxs)("table",{className:"w-full text-sm",children:[(0,r.jsx)("thead",{children:(0,r.jsxs)("tr",{className:"text-left",children:[(0,r.jsx)("th",{className:"font-normal text-[11px] uppercase tracking-wider text-gray-400 dark:text-gray-500 pb-2 pr-4",children:"Metric"}),el.map(e=>(0,r.jsxs)("th",{className:"font-normal pb-2 px-3 text-right align-bottom",children:[(0,r.jsxs)("div",{className:"text-xs font-semibold text-gray-700 dark:text-gray-200",children:[e.label,e.personaName?" \xb7 ".concat(e.personaName):""]}),(0,r.jsx)("div",{className:"text-[11px] font-mono text-gray-500 dark:text-gray-400",children:e.number}),(0,r.jsx)("div",{className:"text-[10px] leading-tight text-gray-400 dark:text-gray-500 max-w-[200px] ml-auto mt-0.5 whitespace-normal",children:(null==C?void 0:C.pooledRouting)?"Pooled — all states":e.catchAll?e.states.length?"".concat(e.states.join(", ")," + catch-all"):"catch-all":e.states.length?e.states.join(", "):"—"})]},e.number)),(0,r.jsxs)("th",{className:"font-normal pb-2 px-3 text-right border-l border-gray-200 dark:border-gray-700",children:[(0,r.jsx)("div",{className:"text-xs font-semibold text-gray-700 dark:text-gray-200",children:"Combined"}),(0,r.jsx)("div",{className:"text-[11px] text-gray-400 dark:text-gray-500",children:"all TFNs"})]})]})}),(0,r.jsxs)("tbody",{className:"text-gray-700 dark:text-gray-300",children:[(0,r.jsxs)("tr",{className:"border-t border-gray-100 dark:border-gray-700/50",children:[(0,r.jsx)("td",{className:"py-2 pr-4 text-xs text-gray-500 dark:text-gray-400",children:"Sent"}),el.map(e=>(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums",children:e.sent.toLocaleStr
1ing()},e.number)),(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums font-semibold border-l border-gray-200 dark:border-gray-700",children:en.sent.toLocaleString()})]}),(0,r.jsxs)("tr",{className:"border-t border-gray-100 dark:border-gray-700/50",children:[(0,r.jsx)("td",{className:"py-2 pr-4 text-xs text-gray-500 dark:text-gray-400",children:"Delivered"}),el.map(e=>(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums",children:e.delivered.toLocaleString()},e.number)),(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums font-semibold border-l border-gray-200 dark:border-gray-700",children:en.delivered.toLocaleString()})]}),(0,r.jsxs)("tr",{className:"border-t border-gray-100 dark:border-gray-700/50",children:[(0,r.jsx)("td",{className:"py-2 pr-4 text-xs text-gray-500 dark:text-gray-400",children:"Delivery Rate"}),el.map(e=>(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums",children:eg(e.deliveryRate)},e.number)),(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums font-semibold border-l border-gray-200 dark:border-gray-700",children:eg(en.deliveryRate)})]}),(0,r.jsxs)("tr",{className:"border-t border-gray-100 dark:border-gray-700/50",children:[(0,r.jsx)("td",{className:"py-2 pr-4 text-xs text-gray-500 dark:text-gray-400",children:en.failed>0?(0,r.jsxs)("button",{type:"button",onClick:()=>F(e=>!e),className:"inline-flex items-center gap-1 hover:text-gray-700 dark:hover:text-gray-200",title:"Show why these failed (carrier delivery codes)",children:[(0,r.jsx)(d.Z,{size:12,className:"text-gray-400 transition-transform ".concat(E?"":"-rotate-90")}),"Failed"]}):"Failed"}),el.map(e=>(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums",children:e.failed.toLocaleString()},e.number)),(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums font-semibold border-l border-gray-200 dark:border-gray-700",children:en.failed.toLocaleString()})]}),E&&en.failed>0&&(()=>{let e=new Map(en.failureCodes.map(e=>[e.code,e.count])),t=Array.from(new Set(el.flatMap(e=>e.failureCodes.map(e=>e.code)))).sort((t,a)=>{var r,s;return(null!==(r=e.get(a))&&void 0!==r?r:0)-(null!==(s=e.get(t))&&void 0!==s?s:0)});return 0===t.length?(0,r.jsx)("tr",{className:"bg-gray-50/60 dark:bg-gray-800/30",children:(0,r.jsx)("td",{colSpan:el.length+2,className:"py-1.5 pl-6 pr-4 text-[11px] italic text-gray-400",children:"No failures in this window"})}):t.map(t=>{var a;let s=function(e){var t;let a=(e||"").trim()||"none";return null!==(t=w[a])&&void 0!==t?t:{label:"Carrier delivery failure (code ".concat(a,")"),explanation:"The carrier returned delivery failure code ".concat(a,".")}}(t),l="none"===t?s.label:"".concat(t," — ").concat(s.label);return(0,r.jsxs)("tr",{className:"bg-gray-50/60 dark:bg-gray-800/30 text-[11px]",children:[(0,r.jsxs)("td",{className:"py-1.5 pl-6 pr-4 text-gray-500 dark:text-gray-400 align-top",children:[(0,r.jsxs)("span",{className:"inline-flex items-center gap-1",children:[(0,r.jsx)("span",{children:l}),(0,r.jsx)("button",{type:"button",title:s.explanation,onClick:()=>L(e=>e===t?null:t),className:"text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 leading-none","aria-label":"What does ".concat(l," mean?"),children:(0,r.jsx)(i.Z,{size:12})})]}),R===t&&(0,r.jsx)("div",{className:"mt-0.5 text-[10px] leading-snug text-gray-400 max-w-xs",children:s.explanation})]}),el.map(e=>{var a,s;let l=null!==(s=null===(a=e.failureCodes.find(e=>e.code===t))||void 0===a?void 0:a.count)&&void 0!==s?s:0;return(0,r.jsx)("td",{className:"py-1.5 px-3 text-right tabular-nums text-gray-500 dark:text-gray-400",children:l>0?l.toLocaleString():"—"},e.number)}),(0,r.jsx)("td",{className:"py-1.5 px-3 text-right tabular-nums text-gray-500 dark:text-gray-400 border-l border-gray-200 dark:border-gray-700",children:(null!==(a=e.get(t))&&void 0!==a?a:0).toLocaleString()})]},"fc-".concat(t))})})(),(0,r.jsxs)("tr",{className:"border-t border-gray-100 dark:border-gray-700/50",children:[(0,r.jsx)("td",{className:"py-2 pr-4 text-xs text-gray-500 dark:text-gray-400",children:"Inbound"}),el.map(e=>(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums",children:e.inbound.toLocaleStr
1ing()},e.number)),(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums font-semibold border-l border-gray-200 dark:border-gray-700",children:en.inbound.toLocaleString()})]}),(0,r.jsxs)("tr",{className:"border-t border-gray-100 dark:border-gray-700/50",children:[(0,r.jsx)("td",{className:"py-2 pr-4 text-xs text-gray-500 dark:text-gray-400",title:"Distinct leads with an SMS_CLICKED preview-link event, bucketed to the TFN that sent them.",children:"Clicks"}),el.map(e=>(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums",children:e.clicks.toLocaleString()},e.number)),(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums font-semibold border-l border-gray-200 dark:border-gray-700",children:en.clicks.toLocaleString()})]}),(0,r.jsxs)("tr",{className:"border-t border-gray-100 dark:border-gray-700/50",children:[(0,r.jsx)("td",{className:"py-2 pr-4 text-xs text-gray-500 dark:text-gray-400",children:"Opt-outs (STOP)"}),el.map(e=>(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums",children:e.stopOptouts.toLocaleString()},e.number)),(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums font-semibold border-l border-gray-200 dark:border-gray-700",children:en.stopOptouts.toLocaleString()})]}),(0,r.jsxs)("tr",{className:"border-t border-gray-100 dark:border-gray-700/50",children:[(0,r.jsx)("td",{className:"py-2 pr-4 text-xs text-gray-500 dark:text-gray-400",children:"Opt-out Rate (STOP only)"}),el.map(e=>(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums",children:em(e.stopOptOutRate)},e.number)),(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums font-semibold border-l border-gray-200 dark:border-gray-700",children:em(en.stopOptOutRate)})]}),(0,r.jsxs)("tr",{className:"border-t border-gray-100 dark:border-gray-700/50",children:[(0,r.jsx)("td",{className:"py-2 pr-4 text-xs text-gray-500 dark:text-gray-400",title:ed?"Today's sends per lane vs that lane's cap. The Combined cell is the campaign's total daily cap (same as the strip above).":"Today's sends from this TFN for this campaign only.",children:ed?"Daily Cap (per TFN)":"Daily Cap (this campaign)"}),el.map(e=>(0,r.jsx)("td",{className:"py-2 px-3",children:eu(e.dailyCapCampaign.sentToday,e.dailyCapCampaign.cap)},e.number)),(0,r.jsx)("td",{className:"py-2 px-3 border-l border-gray-200 dark:border-gray-700",children:eu(en.dailyCapCampaign.sentToday,en.dailyCapCampaign.cap)})]}),!ed&&(0,r.jsxs)("tr",{className:"border-t border-gray-100 dark:border-gray-700/50",children:[(0,r.jsx)("td",{className:"py-2 pr-4 text-xs text-gray-500 dark:text-gray-400",title:"Today's sends from this TFN across ALL campaigns. This is what the worker counts against the cap.",children:"Daily Cap (TFN total)"}),el.map(e=>(0,r.jsx)("td",{className:"py-2 px-3",children:eu(e.dailyCapTotal.sentToday,e.dailyCapTotal.cap)},e.number)),(0,r.jsx)("td",{className:"py-2 px-3 text-right text-gray-400 border-l border-gray-200 dark:border-gray-700",children:"—"})]}),(0,r.jsxs)("tr",{className:"border-t border-gray-100 dark:border-gray-700/50",children:[(0,r.jsx)("td",{className:"py-2 pr-4 text-xs text-gray-500 dark:text-gray-400",title:ed?"Almost all queued leads sit unbound in the Pool column and are dealt evenly across lanes at send time. A per-TFN count here means the lead already has send history on that lane (sticky).":"Queued leads (funnel_stage='QUEUED') that will route to this TFN when the worker next sends.",children:ed?"Queued (pool + sticky)":"Queued (next to send)"}),el.map(e=>(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums",children:e.queued.toLocaleString()},e.number)),(0,r.jsx)("td",{className:"py-2 px-3 text-right tabular-nums font-semibold border-l border-gray-200 dark:border-gray-700",children:en.queued.toLocaleString()})]})]})]})}),(0,r.jsx)("div",{className:"px-4 pb-4 pt-1 border-t border-gray-100 dark:border-gray-700/50",children:(0,r.jsxs)("div",{className:"flex flex-wrap items-center gap-x-8 gap-y-2 pt-3",children:[(0,r.jsxs)("div",{children:[(0,r.jsx)("div",{className:"text-[11px] uppercase tracking-wider text-gray-400 dark:text-gray-500",children:"Litigator-blocked"}),(0,r.jsx)("div",{className:"tabular-nums text-sm font-semibold text-red-600 dark:text-red-400",children:(null!==(f=null===(o=C.litigatorScrub)||void 0===o?void 0:o.blocked)&&void 0!==f?f:0).toLocaleStr
1ing()}),(0,r.jsx)("div",{className:"text-[10px] text-gray-400 dark:text-gray-500 mt-0.5",children:"never sent \xb7 permanently suppressed"})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("div",{className:"text-[11px] uppercase tracking-wider text-gray-400 dark:text-gray-500",children:"Flagged but sent"}),(0,r.jsx)("div",{className:"tabular-nums text-sm font-semibold text-amber-600 dark:text-amber-400",children:(null!==(k=null===(c=C.litigatorScrub)||void 0===c?void 0:c.flaggedNotBlocked)&&void 0!==k?k:0).toLocaleString()}),(0,r.jsx)("div",{className:"text-[10px] text-gray-400 dark:text-gray-500 mt-0.5",children:"record-only flags (DNC lists)"})]}),C.litigatorScrub&&Object.keys(C.litigatorScrub.byFlag).length>0&&(0,r.jsxs)("div",{className:"min-w-[200px]",children:[(0,r.jsx)("div",{className:"text-[11px] uppercase tracking-wider text-gray-400 dark:text-gray-500 mb-1",children:"By flag"}),(0,r.jsx)("div",{className:"flex flex-wrap gap-1",children:Object.entries(C.litigatorScrub.byFlag).sort((e,t)=>t[1]-e[1]).map(e=>{let[t,a]=e;return(0,r.jsxs)("span",{className:"rounded bg-gray-100 dark:bg-gray-700 px-1.5 py-0.5 text-[10px] font-medium text-gray-600 dark:text-gray-300",children:[t,": ",a.toLocaleString()]},t)})})]}),(0,r.jsx)("a",{href:"/admin/litigator-exposure",className:"text-xs text-indigo-600 dark:text-indigo-400 hover:underline ml-auto",children:"Exposure list →"})]})})]})}function K(){let[e,t]=(0,s.useState)([]),[a,l]=(0,s.useState)(null),[i,c]=(0,s.useState)(null),[x,g]=(0,s.useState)(!0),[m,u]=(0,s.useState)(!1);return((0,s.useEffect)(()=>{fetch("/api/admin/tfn-health").then(e=>e.ok?e.json():null).then(e=>
1{e&&(t(e.tfns||[]),l(e.summary||null),c(e.crossTfnSafety||null))}).catch(()=>{}).finally(()=>g(!1))},[]),x||!a)?null:(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow border border-gray-200 dark:border-gray-700",children:[(0,r.jsxs)("button",{onClick:()=>u(!m),className:"flex items-center justify-between w-full text-left px-4 py-3 border-b border-gray-200 dark:border-gray-700",children:[(0,r.jsxs)("div",{className:"flex items-center gap-2",children:[(0,r.jsx)("h3",{className:"text-sm font-semibold text-gray-900 dark:text-white",children:"TFN Health"}),(0,r.jsxs)("span",{className:"text-xs text-gray-500 dark:text-gray-400 bg-gray-100 dark:bg-gray-700 px-2 py-0.5 rounded-md",children:[a.totalActive," active"]})]}),(0,r.jsxs)("div",{className:"flex items-center gap-3",children:[(0,r.jsxs)("div",{className:"text-right",children:[(0,r.jsx)("span",{className:"text-xl font-semibold text-gray-900 dark:text-white",children:a.totalSentToday.toLocaleString()}),(0,r.jsx)("span",{className:"text-xs text-gray-500 dark:text-gray-400 ml-1",children:"sent today"}),null!==a.overallDeliveryRateToday&&(0,r.jsxs)("span",{className:"text-xs ml-2 ".concat(z(a.overallDeliveryRateToday)),children:["\xb7 ",a.overallDeliveryRateToday,"% delivery"]})]}),m?(0,r.jsx)(o.Z,{size:16,className:"text-gray-400"}):(0,r.jsx)(d.Z,{size:16,className:"text-gray-400"})]})]}),!m&&(0,r.jsx)("div",{className:"px-4 pb-4 pt-3",children:0===e.length?(0,r.jsx)("div",{className:"py-3 text-xs text-gray-400 dark:text-gray-500",children:"No active TFNs configured"}):(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)("div",{className:"flex items-center gap-2 mb-2 text-[11px] text-gray-400 dark:text-gray-500 uppercase tracking-wider",children:[(0,r.jsx)("div",{className:"flex-1 min-w-[180px]",children:"TFN"}),(0,r.jsx)("div",{className:"w-[64px] text-center",children:"Status"}),(0,r.jsx)("div",{className:"w-[80px] text-center",children:"Reg"}),(0,r.jsx)("div",{className:"w-[72px] text-center",children:"Today"}),(0,r.jsx)("div",{className:"w-[72px] text-center",children:"7 day"}),(0,r.jsx)("div",{className:"flex-1 min-w-[140px]",children:"All time"}),(0,r.jsx)("div",{className:"w-[72px] text-right",children:"Stickied"}),(0,r.jsx)("div",{className:"w-[72px] text-right",children:"Opt-outs"}),(0,r.jsx)("div",{className:"w-[120px] text-right",children:"Last out"}),(0,r.jsx)("div",{className:"w-[120px] text-right",children:"Last in"})]}),(0,r.jsx)("div",{className:"space-y-1",children:e.map(e=>{var t,a,s,l;return(0,r.jsxs)("div",{className:"flex items-center gap-2 py-1.5",children:[(0,r.jsxs)("div",{className:"flex items-center gap-2 flex-1 min-w-[180px]",children:[(0,r.jsx)("span",{className:"w-2 h-2 rounded-full flex-shrink-0",style:{background:e.active?e.primaryColor||"#10b981":"#ef4444"}}),(0,r.jsxs)("div",{children:[(0,r.jsxs)("div",{className:"text-xs font-semibold text-gray-700 dark:text-gray-200",children:[e.label,e.personaName?" — ".concat(e.personaName):""]}),(0,r.jsxs)("div",{className:"text-[11px] font-mono text-gray-500 dark:text-gray-400",children:[e.number,e.dailyCap&&e.today.sent>0&&(0,r.jsxs)("span",{className:"ml-2 text-gray-400",children:["\xb7 ",e.today.sent.toLocaleString()," / ",e.dailyCap.toLocaleString()]})]})]})]}),(0,r.jsx)("div",{className:"w-[64px] text-center",children:(0,r.jsx)("span",{className:"text-[11px] font-medium px-2 py-0.5 rounded-md whitespace-nowrap ".concat(e.active?"bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-300":"bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-300"),children:e.active?"Active":"Inactive"})}),(0,r.jsx)("div",{className:"w-[80px] text-center",children:(0,r.jsx)("span",{className:"text-[11px] font-medium px-2 py-0.5 rounded-md whitespace-nowrap ".concat(e.registered?"bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-300":"bg-amber-100 dark:bg-amber-900/30 text-amber-700 dark:text-amber-300"),children:e.registered?"Verified":"Not verified"})}),(0,r.jsx)("div",{className:"w-[72px] text-center",children:e.today.sent>0?(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)("div",{className:"text-sm font-medium ".concat(z(e.today.deliveryRate)),children:[null!==(t=e.today.deliveryRate)&&void 0!==t?t:0,"%"]}),(0,r.jsxs)("div",{className:"text-[11px] text-gray-400 dark:text-gray-500",children:[e.today.delivered,"/",e.today.sent]})]}):(0,r.jsx)("div",{className:"text-sm text-gray-300 dark:text-gray-600",children:"--"})}),(0,r.jsx)("div",{className:"w-[72px] text-center",children:e.week.sent>0?(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)("div",{className:"text-sm font-medium ".concat(z(e.week.deliveryRate)),children:[null!==(a=e.week.deliveryRate)&&void 0!==a?a:0,"%"]}),(0,r.jsxs)("div",{className:"text-[11px] text-gray-400 dark:text-gray-500",children:[e.week.delivered,"/",e.week.sent]})]}):(0,r.jsx)("div",{className:"text-sm text-gray-300 dark:text-gray-600",children:"--"})}),(0,r.jsx)("div",{className:"flex-1 min-w-[140px]",children:e.allTime.sent>0?(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)("div",{className:"h-1.5 rounded-full bg-gray-100 dark:bg-gray-700 overflow-hidden mb-1",children:(0,r.jsx)("div",{className:"h-full rounded-full ".concat(W(e.allTime.deliveryRate)),style:{width:"".concat(null!==(s=e.allTime.deliveryRate)&&void 0!==s?s:0,"%")}})}),(0,r.jsxs)("div",{className:"text-xs text-gray-500 dark:text-gray-400 whitespace-nowrap",children:[null!==(l=e.allTime.deliveryRate)&&void 0!==l?l:0,"% \xb7 ",e.allTime.sent.toLocaleStr
1ing()]})]}):(0,r.jsx)("div",{className:"text-sm text-gray-300 dark:text-gray-600",children:"--"})}),(0,r.jsx)("div",{className:"w-[72px] text-right text-xs text-gray-700 dark:text-gray-300",children:e.stickiedLeads.toLocaleString()}),(0,r.jsx)("div",{className:"w-[72px] text-right text-xs ".concat(e.lifetimeOptOuts>0?"text-amber-600 dark:text-amber-400":"text-gray-700 dark:text-gray-300"),children:e.lifetimeOptOuts.toLocaleString()}),(0,r.jsx)("div",{className:"w-[120px] text-right text-[11px] text-gray-500 dark:text-gray-400 whitespace-nowrap",children:e.lastOutboundAt?(0,n.o0)(e.lastOutboundAt):"—"}),(0,r.jsx)("div",{className:"w-[120px] text-right text-[11px] text-gray-500 dark:text-gray-400 whitespace-nowrap",children:e.lastInboundAt?(0,n.o0)(e.lastInboundAt):"—"})]},e.number)})}),i&&(0,r.jsxs)("div",{className:"mt-4 border-t border-gray-200 dark:border-gray-700 pt-3",children:[(0,r.jsx)("p",{className:"text-xs font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Cross-TFN Safety"}),(0,r.jsxs)("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-3",children:[(0,r.jsx)(H,{label:"Blocked sends (today)",value:i.crossTfnBlocksToday}),(0,r.jsx)(H,{label:"Blocked sends (7d)",value:i.crossTfnBlocks7d}),(0,r.jsx)(H,{label:"Pool-mismatch archives (today)",value:i.poolMismatchArchivesToday}),(0,r.jsx)(H,{label:"Pool-mismatch archives (7d)",value:i.poolMismatchArchives7d})]}),(0,r.jsx)("p",{className:"text-[10px] text-gray-400 mt-2",children:"Steady state: all zero. Non-zero = investigate."})]})]})})]})}function Q(){let[e,t]=(0,s.useState)([]),[a,l]=(0,s.useState)([]),[n,i]=(0,s.useState)(null),[c,x]=(0,s.useState)(!0),[g,m]=(0,s.useState)(!1);if((0,s.useEffect)(()=>{fetch("/api/admin/domain-health").then(e=>e.ok?e.json():null).then(e=>{e&&(t(e.activeDomains||[]),l(e.quarantinedDomains||[]),i(e.summary||null))}).catch(()=>{}).finally(()=>x(!1))},[]),c||!n||0===n.totalActive&&0===n.totalQuarantined)return null;let u=e=>e.replace(/^(www\.|preview\.|p\.)/,"");return(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow border border-gray-200 dark:border-gray-700",children:[(0,r.jsxs)("button",{onClick:()=>m(!g),className:"flex items-center justify-between w-full text-left px-4 py-3 border-b border-gray-200 dark:border-gray-700",children:[(0,r.jsxs)("div",{className:"flex items-center gap-2",children:[(0,r.jsx)("h3",{className:"text-sm font-semibold text-gray-900 dark:text-white",children:"Domain health"}),(0,r.jsxs)("span",{className:"text-xs text-gray-500 dark:text-gray-400 bg-gray-100 dark:bg-gray-700 px-2 py-0.5 rounded-md",children:[n.totalActive," active \xb7 ",n.totalQuarantined," quarantined"]})]}),(0,r.jsxs)("div",{className:"flex items-center gap-3",children:[(0,r.jsxs)("div",{className:"text-right",children:[(0,r.jsx)("span",{className:"text-xl font-semibold text-gray-900 dark:text-white",children:n.totalSendsToday.toLocaleString()}),(0,r.jsx)("span",{className:"text-xs text-gray-500 dark:text-gray-400 ml-1",children:"sends today"})]}),g?(0,r.jsx)(o.Z,{size:16,className:"text-gray-400"}):(0,r.jsx)(d.Z,{size:16,className:"text-gray-400"})]})]}),!g&&(0,r.jsxs)("div",{className:"px-4 pb-4 pt-3",children:[(0,r.jsxs)("div",{className:"flex items-center gap-2 mb-2 text-[11px] text-gray-400 dark:text-gray-500 uppercase tracking-wider",children:[(0,r.jsx)("div",{className:"flex-1 min-w-[180px]",children:"Domain"}),(0,r.jsx)("div",{className:"w-[72px] text-center",children:"Today"}),(0,r.jsx)("div",{className:"w-[72px] text-center",children:"7 day"}),(0,r.jsx)("div",{className:"flex-1",children:"All time"}),(0,r.jsx)("div",{className:"w-[64px] text-right",children:"Status"})]}),(0,r.jsx)("div",{className:"space-y-1",children:e.map(e=>{var t,a,s,l,n,d,i,o,c,x,g,m,y,p,b,h,f,k,v,j,N,w,C,S,T,D,E,F,R,L;let A=null!==(k=null===(t=e.week)||void 0===t?void 0:t.deliveryRate)&&void 0!==k?k:null,P=(null!==(v=null===(a=e.week)||void 0===a?void 0:a.sent)&&void 0!==v?v:0)<10?"Low data":null!==A&&A>=70?"Healthy":null!==A&&A>=50?"Warning":"Flagged",I="Healthy"===P?"bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-300":"Warning"===P?"bg-yellow-100 dark:bg-yellow-900/30 text-yellow-700 dark:text-yellow-300":"Flagged"===P?"bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-300":"bg-gray-100 dark:bg-gray-700 text-gray-500 dark:text-gray-400";return(0,r.jsxs)("div",{className:"flex items-center gap-2 py-1.5",children:[(0,r.jsxs)("div",{className:"flex items-center gap-2 flex-1 min-w-[180px]",children:[(0,r.jsx)("span",{className:"w-2 h-2 rounded-full bg-green-500 flex-shrink-0"}),(0,r.jsx)("span",{className:"text-xs font-mono text-gray-700 dark:text-gray-300 truncate",children:u(e.domain)})]}),(0,r.jsx)("div",{className:"w-[72px] text-center",children:(null!==(j=null===(s=e.today)||void 0===s?void 0:s.sent)&&void 0!==j?j:0)>0?(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)("div",{className:"text-sm font-medium ".concat(z(null!==(N=null===(l=e.today)||void 0===l?void 0:l.deliveryRate)&&void 0!==N?N:null)),children:[null!==(w=null===(n=e.today)||void 0===n?void 0:n.deliveryRate)&&void 0!==w?w:0,"%"]}),(0,r.jsxs)("div",{className:"text-[11px] text-gray-400 dark:text-gray-500",children:[null===(d=e.today)||void 0===d?void 0:d.delivered,"/",null===(i=e.today)||void 0===i?void 0:i.sent]})]}):(0,r.jsx)("div",{className:"text-sm text-gray-300 dark:text-gray-600",children:"--"})}),(0,r.jsx)("div",{className:"w-[72px] text-center",children:(null!==(C=null===(o=e.week)||void 0===o?void 0:o.sent)&&void 0!==C?C:0)>0?(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)("div",{className:"text-sm font-medium ".concat(z(null!==(S=null===(c=e.week)||void 0===c?void 0:c.deliveryRate)&&void 0!==S?S:null)),children:[null!==(T=null===(x=e.week)||void 0===x?void 0:x.deliveryRate)&&void 0!==T?T:0,"%"]}),(0,r.jsxs)("div",{className:"text-[11px] text-gray-400 dark:text-gray-500",children:[null===(g=e.week)||void 0===g?void 0:g.delivered,"/",null===(m=e.week)||void 0===m?void 0:m.sent]})]}):(0,r.jsx)("div",{className:"text-sm text-gray-300 dark:text-gray-600",children:"--"})}),(0,r.jsx)("div",{className:"flex-1",children:(null!==(D=null===(y=e.allTime)||void 0===y?void 0:y.sent)&&void 0!==D?D:0)>0?(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)("div",{className:"h-1.5 rounded-full bg-gray-100 dark:bg-gray-700 overflow-hidden mb-1",children:(0,r.jsx)("div",{className:"h-full rounded-full ".concat(W(null!==(E=null===(p=e.allTime)||void 0===p?void 0:p.deliveryRate)&&void 0!==E?E:null)),style:{width:"".concat(null!==(F=null===(b=e.allTime)||void 0===b?void 0:b.deliveryRate)&&void 0!==F?F:0,"%")}})}),(0,r.jsxs)("div",{className:"text-xs text-gray-500 dark:text-gray-400 whitespace-nowrap",children:[null!==(R=null===(h=e.allTime)||void 0===h?void 0:h.deliveryRate)&&void 0!==R?R:0,"% \xb7 ",(null!==(L=null===(f=e.allTime)||void 0===f?void 0:f.sent)&&void 0!==L?L:0).toLocaleStr
1ing()]})]}):(0,r.jsx)("div",{className:"text-sm text-gray-300 dark:text-gray-600",children:"--"})}),(0,r.jsx)("div",{className:"w-[64px] text-right",children:(0,r.jsx)("span",{className:"text-[11px] font-medium px-2 py-0.5 rounded-md whitespace-nowrap ".concat(I),children:P})})]},e.domain)})}),a.length>0&&(0,r.jsxs)("div",{className:"border-t border-gray-100 dark:border-gray-700 mt-4 pt-3",children:[(0,r.jsxs)("div",{className:"flex items-center gap-2 mb-2",children:[(0,r.jsx)("span",{className:"text-xs font-medium text-gray-500 dark:text-gray-400",children:"Quarantined"}),(0,r.jsx)("span",{className:"text-[11px] text-gray-400 dark:text-gray-500",children:"auto-removed from rotation due to low delivery"})]}),(0,r.jsx)("div",{className:"space-y-1.5",children:a.map(e=>(0,r.jsxs)("div",{className:"flex items-center gap-2 bg-gray-50 dark:bg-gray-800/50 rounded-lg px-3 py-2",children:[(0,r.jsx)("span",{className:"w-2 h-2 rounded-full bg-red-500 flex-shrink-0"}),(0,r.jsx)("span",{className:"text-xs font-mono text-gray-500 dark:text-gray-400 flex-1 truncate",children:u(e.domain)}),(0,r.jsxs)("span",{className:"text-[11px] text-gray-400 dark:text-gray-500",children:["offense #",e.offenseCount]}),(0,r.jsxs)("span",{className:"text-[11px] font-medium px-2 py-0.5 rounded-md bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-300",children:[e.daysRemaining,"d left"]})]},e.domain))})]})]})]})}function J(){let[e,t]=(0,s.useState)(!1),[a,l]=(0,s.useState)(""),[n,i]=(0,s.useState)([]),[x,g]=(0,s.useState)(""),[m,u]=(0,s.useState)(!1),[y,p]=(0,s.use
1State)(!1),[b,h]=(0,s.useState)(null),f=(0,s.useCallback)(async()=>{u(!0);try{let a=await fetch("/api/campaigns/settings");if(a.ok){var e,t;let r=await a.json();l((null===(e=r.settings)||void 0===e?void 0:e.smsFromNumber)||""),i((null===(t=r.settings)||void 0===t?void 0:t.previewDomainPool)||[])}}catch(e){}finally{u(!1)}},[]);(0,s.useEffect)(()=>{e&&f()},[e,f]);let k=()=>{let e=x.trim().replace(/^https?:\/\//,"").replace(/\/+$/,"");e&&!n.includes(e)&&(i([...n,e]),g(""))},v=e=>i(n.filter(t=>t!==e)),j=async()=>{p(!0),h(null);try{let e=await fetch("/api/campaigns/settings",{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({overrides:{smsFromNumber:a},previewDomainPool:n})});h(e.ok?"Saved":"Save failed"),setTimeout(()=>h(null),2e3)}catch(e){h("Network error")}finally{p(!1)}};return(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow",children:[(0,r.jsx)("button",{onClick:()=>t(!e),className:"w-full px-5 py-3 flex items-center justify-between text-left",children:(0,r.jsxs)("div",{className:"flex items-center gap-2",children:[e?(0,r.jsx)(d.Z,{size:14,className:"text-gray-400"}):(0,r.jsx)(o.Z,{size:14,className:"text-gray-400"}),(0,r.jsx)("span",{className:"text-sm font-semibold text-gray-700 dark:text-gray-300",children:"System Settings"}),(0,r.jsx)("span",{className:"text-xs text-gray-400",children:"From number \xb7 Domain pool \xb7 Domain health"})]})}),e&&(0,r.jsx)("div",{className:"px-5 pb-4 space-y-4",children:m?(0,r.jsx)("div",{className:"flex items-center justify-center py-4",children:(0,r.jsx)("div",{className:"animate-spin rounded-full h-4 w-4 border-b-2 border-blue-600"})}):(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)("div",{className:"border border-gray-100 dark:border-gray-700 rounded-lg p-4",children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white mb-2 block",children:"From Number (TFN)"}),(0,r.jsx)("input",{type:"text",value:a,onChange:e=>l(e.target.value),placeholder:"+18001234567",className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white text-sm"})]}),(0,r.jsxs)("div",{className:"border border-gray-100 dark:border-gray-700 rounded-lg p-4",children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white mb-2 block",children:"Preview Domain Pool"}),(0,r.jsx)("p",{className:"text-xs text-gray-400 mb-3",children:"Domains used for SMS preview site links. First domain is primary."}),(0,r.jsx)("div",{className:"space-y-1.5 mb-3",children:n.map((e,t)=>(0,r.jsxs)("div",{className:"flex items-center justify-between bg-gray-50 dark:bg-gray-900 rounded-lg px-3 py-2",children:[(0,r.jsxs)("div",{className:"flex items-center gap-2",children:[(0,r.jsx)("span",{className:"text-xs font-mono text-gray-700 dark:text-gray-300",children:e}),0===t&&(0,r.jsx)("span",{className:"text-[9px] bg-blue-100 dark:bg-blue-900/40 text-blue-700 dark:text-blue-300 px-1.5 py-0.5 rounded font-semibold",children:"PRIMARY"})]}),(0,r.jsx)("button",{onClick:()=>v(e),className:"text-red-400 hover:text-red-600 transition-colors",children:(0,r.jsx)(c.Z,{size:12})})]},e))}),(0,r.jsxs)("div",{className:"flex gap-2",children:[(0,r.jsx)("input",{type:"text",value:x,onChange:e=>g(e.target.value),onKeyDown:e=>"Enter"===e.key&&k(),placeholder:"newdomain.com",className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-1.5 flex-1 bg-white dark:bg-gray-900 text-sm text-gray-900 dark:text-white"}),(0,r.jsx)("button",{onClick:k,className:"px-3 py-1.5 bg-blue-600 text-white rounded-lg text-xs font-medium hover:bg-blue-700",children:"Add"})]}),(0,r.jsx)("p",{className:"text-xs text-amber-500 mt-2",children:"Each domain must have DNS pointing to Vercel + Railway. Adding a non-configured domain will break preview links."})]}),(0,r.jsxs)("div",{className:"flex items-center justify-end gap-3",children:[b&&(0,r.jsx)("span",{className:"text-xs ".concat("Saved"===b?"text-green-600":"text-red-600"),children:b}),(0,r.jsx)("button",{onClick:j,disabled:y,className:"bg-blue-600 text-white px-4 py-2 rounded-lg text-sm font-medium hover:bg-blue-700 disabled:opacity-50",children:y?"Saving...":"Save System Settings"})]})]})})]})}function Y(e){let{campaigns:t,loading:a,onSelect:l,onRefresh:n}
1=e,[d,i]=(0,s.useState)(!1),[o,c]=(0,s.useState)(null);return(0,s.useEffect)(()=>{let e=!1,t=async()=>{try{let t=await fetch("/api/admin/twilio-balance");if(!t.ok)return;let a=await t.json();e||c(a)}catch(e){}};t();let a=setInterval(t,6e4);return()=>{e=!0,clearInterval(a)}},[]),(0,r.jsxs)("div",{className:"space-y-4",children:[o&&(0,r.jsxs)("div",{className:"flex items-center justify-between px-4 py-3 rounded-lg border ".concat(null===o.balance?"bg-gray-50 border-gray-200 dark:bg-gray-900/30 dark:border-gray-700":o.balance<10?"bg-red-50 border-red-300 dark:bg-red-900/20 dark:border-red-800":o.balance<50?"bg-yellow-50 border-yellow-300 dark:bg-yellow-900/20 dark:border-yellow-800":"bg-green-50 border-green-200 dark:bg-green-900/20 dark:border-green-800"),children:[(0,r.jsxs)("div",{className:"flex items-center gap-3",children:[(0,r.jsx)("div",{className:"w-2 h-2 rounded-full ".concat(null===o.balance?"bg-gray-400":o.balance<10?"bg-red-500 animate-pulse":o.balance<50?"bg-yellow-500":"bg-green-500")}),(0,r.jsxs)("div",{children:[(0,r.jsx)("div",{className:"text-xs uppercase tracking-wide font-semibold text-gray-500 dark:text-gray-400",children:"Twilio Balance"}),(0,r.jsx)("div",{className:"text-lg font-bold ".concat(null===o.balance?"text-gray-400":o.balance<10?"text-red-700 dark:text-red-400":o.balance<50?"text-yellow-800 dark:text-yellow-400":"text-green-800 dark:text-green-400"),children:o.configured?null===o.balance?"—":"$".concat(o.balance.toFixed(2)," ").concat(o.currency):"Not configured"})]})]}),null!==o.balance&&o.balance<10&&(0,r.jsx)("a",{href:"https://console.twilio.com/billing",target:"_blank",rel:"noopener noreferrer",className:"text-xs font-medium px-3 py-1.5 rounded-md bg-red-600 hover:bg-red-700 text-white",children:"Reload Twilio →"}),null!==o.balance&&o.balance>=10&&o.balance<50&&(0,r.jsx)("a",{href:"https://console.twilio.com/billing",target:"_blank",rel:"noopener noreferrer",className:"text-xs font-medium text-yellow-800 hover:underline dark:text-yellow-300",children:"Reload Twilio →"})]}),(0,r.jsx)(J,{}),(0,r.jsx)(P.Z,{}),(0,r.jsx)(K,{}),(0,r.jsx)(Q,{}),(0,r.jsx)(S,{channel:"sms"}),(0,r.jsxs)("div",{className:"flex items-center justify-between",children:[(0,r.jsxs)("p",{className:"text-sm text-gray-500 dark:text-gray-400",children:[t.length," campaign",1!==t.length?"s":""]}),(0,r.jsxs)("div",{className:"flex items-center gap-2",children:[(0,r.jsx)("button",{onClick:n,className:"p-2 rounded-lg border border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors",title:"Refresh",children:(0,r.jsx)(x.Z,{size:16,className:"text-gray-500 dark:text-gray-400"})}),(0,r.jsxs)("button",{onClick:()=>i(!0),className:"bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition-colors text-sm font-medium flex items-center gap-2",children:[(0,r.jsx)(g.Z,{size:16}),"New Campaign"]})]})]}),a?(0,r.jsx)("div",{className:"flex items-center justify-center py-20",children:(0,r.jsx)("div",{className:"animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"})}):0===t.length?(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow p-12 text-center",children:[(0,r.jsx)(m.Z,{size:48,className:"mx-auto text-gray-300 dark:text-gray-600 mb-4"}),(0,r.jsx)("h3",{className:"text-lg font-semibold text-gray-700 dark:text-gray-300 mb-2",children:"No campaigns yet"}),(0,r.jsx)("p",{className:"text-sm text-gray-500 dark:text-gray-400 mb-6",children:"Create your first SMS campaign to start reaching leads"}),(0,r.jsx)("button",{onClick:()=>i(!0),className:"bg-blue-600 text-white px-6 py-2 rounded-lg hover:bg-blue-700 transition-colors text-sm font-medium",children:"Create Campaign"})]}):(0,r.jsx)("div",{className:"grid gap-4 md:grid-cols-2 lg:grid-cols-3",children:t.map(e=>(0,r.jsx)(G,{campaign:e,onClick:()=>l(e),onRefresh:n},e.id))}),d&&(0,r.jsx)($,{onClose:()=>i(!1),onCreated:()=>{i(!1),n()}})]})}function G(e){let{campaign:t,onClick:a,onRefresh:l}=e,d=t.totalLeads>0?t.sentCount/t.totalLeads*100:0,i=I[t.status]||I.DRAFT,[o,c]=(0,s.useState)(!1),x=async e=>{e.stopPropagation(),c(!1);try{(await fetch("/api/campaigns/".concat(t.id,"/duplicate"),{method:"POST"})).ok?l():alert("Failed to duplicate campaign")}catch(e){alert("Failed to duplicate campaign")}},g=async e=>{if(e.stopPropagation(),c(!1),confirm('Delete "'.concat(t.name,'"? This cannot be undone.')))try{(await fetch("/api/campaigns/".concat(t.id),{method:"DELETE"})).ok?l():alert("Failed to delete campaign")}catch(e){alert("Failed to delete campaign")}};return(0,r.jsxs)("div",{onClick:a,className:"bg-white dark:bg-gray-800 rounded-lg shadow p-6 text-left hover:shadow-md transition-shadow w-full border border-gray-200 dark:border-gray-700 cursor-pointer relative",children:[(0,r.jsxs)("div",{className:"flex items-start justify-between mb-3",children:[(0,r.jsx)("h3",{className:"font-semibold text-gray-900 dark:text-white truncate pr-2",children:t.name}),(0,r.jsxs)("div",{className:"flex items-center gap-2",children:[(0,r.jsx)("span",{className:"px-2 py-1 rounded-full text-xs font-medium whitespace-nowrap ".concat(i.bg," ").concat(i.text),children:t.status}),(0,r.jsx)("button",{onClick:e=>{e.stopPropagation(),c(!o)},className:"p-1 hover:bg-gray-100 dark:hover:bg-gray-700 rounded",children:(0,r.jsx)(u.Z,{size:14,className:"text-gray-400"})})]})]}),o&&(0,r.jsxs)("div",{className:"absolute right-4 top-14 z-20 bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-lg shadow-lg py-1 min-w-[140px]",children:[(0,r.jsx)("button",{onClick:x,className:"w-full text-left px-3 py-2 text-sm hover:bg-gray-50 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-300",children:"Duplicate"}),"SENDING"!==t.status&&(0,r.jsx)("button",{onClick:g,className:"w-full text-left px-3 py-2 text-sm hover:bg-red-50 dark:hover:bg-red-900/20 text-red-600 dark:text-red-400",children:"Delete"})]}),(0,r.jsxs)("div",{className:"mb-3",children:[(0,r.jsxs)("div",{className:"flex justify-between text-xs text-gray-500 dark:text-gray-400 mb-1",children:[(0,r.jsxs)("span",{children:[t.sentCount," / ",t.totalLeads," sent"]}),(0,r.jsxs)("span",{children:[d.toFixed(0),"%"]})]}),(0,r.jsx)("div",{className:"w-full bg-gray-200 dark:bg-gray-700 rounded-full h-2",children:(0,r.jsx)("div",{className:"bg-blue-600 rounded-full h-2 transition-all",style:{width:"".concat(Math.min(d,100),"%")}})})]}),(0,r.jsxs)("div",{className:"flex items-center gap-4 text-xs text-gray-500 dark:text-gray-400",children:[(0,r.jsxs)("span",{title:"Delivered",children:[Z(t.deliveredCount,t.sentCount),"% del"]}),(0,r.jsxs)("span",{title:"Clicked",children:[Z(t.clickedCount,t.deliveredCount),"% CTR"]}
1),(0,r.jsxs)("span",{title:"Opted In",children:[Z(t.optedInCount,t.deliveredCount),"% opt-in"]})]}),(0,r.jsxs)("p",{className:"text-xs text-gray-400 dark:text-gray-500 mt-3",children:["Created ",(0,n.p6)(t.createdAt)]})]})}function X(e){let{value:t,onChange:a,allowSystemDefault:s}=e,l=T.$D.filter(e=>e.active),n=l.some(e=>e.number===t),d=!s&&!n&&""!==t;return(0,r.jsxs)("select",{value:t,onChange:e=>a(e.target.value),className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none",children:[s&&(0,r.jsx)("option",{value:"",children:"(use system default)"}),d&&(0,r.jsxs)("option",{value:t,disabled:!0,children:[t," (legacy — pick a current TFN)"]}),l.map(e=>(0,r.jsxs)("option",{value:e.number,children:[e.label," \xb7 ",e.number]},e.number))]})}function $(e){let{onClose:t,onCreated:a}=e,[l,n]=(0,s.useState)(""),[d,i]=(0,s.useState)(""),[o,x]=(0,s.useState)(""),[m,u]=(0,s.useState)([]),[y,p]=(0,s.use
1State)(""),[b,h]=(0,s.useState)(""),[f,k]=(0,s.useState)(!1),[v,j]=(0,s.useState)(""),[N,w]=(0,s.useState)(!1),[C,S]=(0,s.useState)([]),[T,D]=(0,s.useState)(!1),[E,F]=(0,s.useState)(!1),[L,A]=(0,s.useState)(null),P=(0,s.useRef)(null);(0,s.useEffect)(()=>{fetch("/api/folders").then(e=>e.json()).then(e=>u(e.folders||[])).catch(()=>{}),fetch("/api/campaigns/settings").then(e=>e.json()).then(e=>{var t;(null===(t=e.settings)||void 0===t?void 0:t.smsFromNumber)&&x(e.settings.smsFromNumber)}).catch(()=>{})},[]),(0,s.useEffect)(()=>{if(!y){A(null);return}fetch("/api/campaigns/phone-stats?folderId=".concat(y)).then(e=>e.json()).then(e=>{void 0!==e.total&&A(e)}).catch(()=>A(null))},[y]);let I=e=>{let t=P.current;if(!t)return;let a=t.selectionStart,r=t.selectionEnd;i(d.slice(0,a)+e+d.slice(r)),setTimeout(()=>{t.focus(),t.selectionStart=t.selectionEnd=a+e.length},0)},_=d.length,M=_<=160?1:Math.ceil(_/153),Z=async()=>{if(!l.trim()){j("Campaign name is required");return}if(!d.trim()){j(N?"Fallback template is required (campaign templateBody is NOT NULL)":"Template body is required");return}if(N){if(0===C.length){j("Pick at least one TFN for the pool"),F(!0);return}for(let e of C){if(!Number.isInteger(e.throttlePerMinute)||e.throttlePerMinute<1||e.throttlePerMinute>30){j("TFN ".concat(e.tfn,": throttle must be 1–30/min")),F(!0);return}if(!Number.isInteger(e.dailyCap)||e.dailyCap<1){j("TFN ".concat(e.tfn,": daily cap must be ≥ 1")),F(!0);return}if(e.sendWindow.endHour<=e.sendWindow.startHour){j("TFN ".concat(e.tfn,": send window end must be after start")),F(!0);return}if(!Array.isArray(e.sendWindow.days)||0===e.sendWindow.days.length){j("TFN ".concat(e.tfn,": pick at least one send day")),F(!0);return}if(T&&(!Number.isInteger(e.weeklyTarget)||e.weeklyTarget<1)){j("TFN ".concat(e.tfn,": weekly target required while Smart Weekly is on")),F(!0);return}if(!T&&void 0!==e.weeklyTarget&&(!Number.isInteger(e.weeklyTarget)||e.weeklyTarget<1)){j("TFN ".concat(e.tfn,": weekly target must be a positive integer")),F(!0);return}if(void 0!==e.emergencyCap&&(!Number.isInteger(e.emergencyCap)||e.emergencyCap<1)){j("TFN ".concat(e.tfn,": emergency cap must be a positive integer")),F(!0);return}}}k(!0),j("");try{let e={name:l.trim(),templateBody:d.trim()};N?(e.fromNumber=C[0].tfn,e.tfnPool=C.map(e=>{var t;let a=(null!==(t=e.templates)&&void 0!==t?t:[]).filter(e=>"string"==typeof e&&e.trim().length>0);if(0===a.length){let{templates:t,...a}=e;return a}return{...e,templates:a}}),e.smartWeeklyEnabled=T):e.fromNumber=o.trim()||void 0,y&&(e.folderId=y),b.trim()&&(e.leadIds=b.split(",").map(e=>e.trim()).filter(Boolean));let t=await fetch("/api/campaigns",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!t.ok){let e=await t.json().catch(()=>({}));throw Error(e.error||"Failed to create campaign")}a()}catch(e){j(e.message||"Failed to create campaign")}finally{k(!1)}};return(0,r.jsx)("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/50",onClick:t,children:(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-xl shadow-xl max-w-2xl w-full mx-4 max-h-[90vh] overflow-y-auto",onClick:e=>e.stopPropagation(),children:[(0,r.jsxs)("div",{className:"flex items-center justify-between p-6 border-b border-gray-200 dark:border-gray-700",children:[(0,r.jsx)("h2",{className:"text-lg font-bold text-gray-900 dark:text-white",children:"New Campaign"}),(0,r.jsx)("button",{onClick:t,className:"text-gray-400 hover:text-gray-600 dark:hover:text-gray-300",children:(0,r.jsx)(c.Z,{size:20})})]}),(0,r.jsxs)("div",{className:"p-6 space-y-5",children:[v&&(0,r.jsx)("div",{className:"bg-red-50 dark:bg-red-950/30 border border-red-200 dark:border-red-800 text-red-700 dark:text-red-400 px-4 py-3 rounded-lg text-sm",children:v}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1",children:"Campaign Name"}),(0,r.jsx)("input",{type:"text",value:l,onChange:e=>n(e.target.value),className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none",placeholder:"e.g. March Plumbers Blast"})]}),(0,r.jsxs)("div",{className:"flex items-start justify-between border border-blue-200 dark:border-blue-800 bg-blue-50/50 dark:bg-blue-950/20 rounded-lg px-4 py-3",children:[(0,r.jsxs)("div",{className:"pr-3",children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white",children:"Multi-TFN mode (Phase 2)"}),(0,r.jsx)("p",{className:"text-xs text-gray-500 dark:text-gray-400 mt-0.5",children:"Send from multiple TFNs in one campaign. Each TFN gets its own copy and schedule."})]}),(0,r.jsx)("input",{type:"checkbox",checked:N,onChange:e=>w(e.target.checked),className:"w-4 h-4 rounded border-gray-300 dark:border-slate-600 mt-1"})]}),!N&&(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1",children:"Cold Text Template"}),(0,r.jsx)("textarea",{ref:P,value:d,onChange:e=>i(e.target.value),rows:4,className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none font-mono text-sm",placeholder:"Hey, free preview for {companyName} in {city}: {previewUrl}"}),(0,r.jsxs)("div",{className:"flex items-center justify-between mt-1",children:[(0,r.jsx)("div",{className:"flex flex-wrap gap-1",children:O.map(e=>(0,r.jsx)("button",{type:"button",onClick:()=>I(e),className:"px-2 py-0.5 rounded text-xs bg-blue-50 dark:bg-blue-900/30 text-blue-600 dar
1k:text-blue-400 hover:bg-blue-100 dark:hover:bg-blue-900/50 border border-blue-200 dark:border-blue-800 transition-colors",children:e},e))}),(0,r.jsxs)("span",{className:"text-xs text-gray-400 dark:text-gray-500 whitespace-nowrap ml-2",children:[_," chars \xb7 ",M," segment",1!==M?"s":""]})]})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1",children:"From Number"}),(0,r.jsx)(X,{value:o,onChange:x,allowSystemDefault:!0})]})]}),N&&(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1",children:"Fallback Template"}),(0,r.jsx)("p",{className:"text-xs text-gray-500 dark:text-gray-400 mb-1",children:"Used only when a TFN has no per-TFN templates. Required by the schema."}),(0,r.jsx)("textarea",{value:d,onChange:e=>i(e.target.value),rows:3,className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white font-mono text-sm",placeholder:"Hey, free preview for {companyName} in {city}: {previewUrl}"})]}),(0,r.jsx)(R,{value:C,onChange:S,showErrors:E,smartWeeklyEnabled:T,onSmartWeeklyChange:D})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1",children:"Lead Selection"}),(0,r.jsx)("p",{className:"text-xs text-gray-500 dark:text-gray-400 mb-2",children:"Optional. Leave both empty to create an empty DRAFT campaign — you can add leads later from the campaign detail page."}),(0,r.jsxs)("div",{className:"space-y-3",children:[(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"text-xs text-gray-500 dark:text-gray-400 mb-1 block",children:"Select a Folder"}),(0,r.jsxs)("select",{value:y,onChange:e=>p(e.target.value),className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none",children:[(0,r.jsx)("option",{value:"",children:"-- Choose folder --"}),m.map(e=>{var t,a,s;return(0,r.jsxs)("option",{value:e.id,children:[e.name," (",null!==(s=null!==(a=null===(t=e._count)||void 0===t?void 0:t.leads)&&void 0!==a?a:e.leadCount)&&void 0!==s?s:"?"," leads)"]},e.id)})]}),L&&(0,r.jsxs)("div",{className:"mt-2 space-y-1",children:[(0,r.jsxs)("p",{className:"text-xs text-gray-500 dark:text-gray-400",children:[L.mobile," sendable",L.landline+L.voip+L.unknown>0&&" \xb7 ".concat(L.landline+L.voip+L.unknown," will be skipped"),L.untyped>0&&" \xb7 ".concat(L.untyped," not yet verified")]}),L.untyped>0&&(0,r.jsxs)("p",{className:"text-xs text-amber-600 dark:text-amber-400",children:[L.untyped," leads not yet phone-verified. Lookup will run at send time and unverifiable numbers will be skipped."]})]})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"text-xs text-gray-500 dark:text-gray-400 mb-1 block",children:"Or paste comma-separated Lead IDs"}),(0,r.jsx)("input",{type:"text",value:b,onChange:e=>h(e.target.value),className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none text-sm",placeholder:"lead_abc123, lead_def456, ..."}),b.trim()&&(0,r.jsxs)("p",{className:"text-xs text-gray-400 mt-1",children:[b.split(",").filter(e=>e.trim()).length," lead ID(s) entered"]})]})]})]})]}),(0,r.jsxs)("div",{className:"flex items-center justify-end gap-3 p-6 border-t border-gray-200 dark:border-gray-700",children:[(0,r.jsx)("button",{onClick:t,className:"px-4 py-2 rounded-lg text-sm font-medium text-gray-700 dark:text-gray-300 border border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors",children:"Cancel"}),(0,r.jsx)("button",{onClick:Z,disabled:f,className:"bg-blue-600 text-white px-6 py-2 rounded-lg hover:bg-blue-700 transition-colors text-sm font-medium disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-2",children:f?(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)("div",{className:"animate-spin rounded-full h-4 w-4 border-b-2 border-white"}),"Creating..."]}):(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(g.Z,{size:16}),"Create Campaign"]})})]})]})})}function ee(e){var t,a;let{campaign:d,onBack:i,onRefresh:o}
1=e,[g,m]=(0,s.useState)([]),[u,w]=(0,s.useState)(!0),[T,D]=(0,s.useState)(null),[E,F]=(0,s.useState)("newest"),[R,L]=(0,s.useState)(1),[A,P]=(0,s.useState)(1),[O,M]=(0,s.useState)(null),[z,W]=(0,s.useState)([]),[U,H]=(0,s.useState)(!1),[q,B]=(0,s.useState)({}),[K,Q]=(0,s.useState)({}),[J,Y]=(0,s.useState)([]),[G,X]=(0,s.useState)(0),[$,ee]=(0,s.useState)(0),[en,ed]=(0,s.useState)(0),[ei,eg]=(0,s.useState)(0),[em,eu]=(0,s.useState)(0),[ey,ep]=(0,s.useState)(!1),[eb,eh]=(0,s.useState)(!1),[ef,ek]=(0,s.useState)(!1),[ev,ej]=(0,s.useState)(!1),[eN,ew]=(0,s.useState)(!1),[eC,eS]=(0,s.useState)([]);(0,s.useEffect)(()=>{fetch("/api/users?role=REP&status=ACTIVE").then(e=>e.json()).then(e=>eS((e.users||[]).map(e=>({id:e.id,name:e.name})).sort((e,t)=>e.name.localeCompare(t.name)))).catch(()=>{})},[]);let[eT,eD]=(0,s.useState)(new Set),[eE,eF]=(0,s.useState)("all"),[eR,eL]=(0,s.useState)(""),[eA,eP]=(0,s.useState)(""),eI=(0,s.useCallback)(()=>{let e=new Date,t=e=>e.toISOString().split("T")[0];if("today"===eE)return{startDate:t(e),endDate:t(e)};if("7d"===eE){let a=new Date(e);return a.setDate(a.getDate()-6),{startDate:t(a),endDate:t(e)}}if("30d"===eE){let a=new Date(e);return a.setDate(a.getDate()-29),{startDate:t(a),endDate:t(e)}}return"custom"===eE&&eR&&eA?{startDate:eR,endDate:eA}:{}},[eE,eR,eA]),e_=(0,s.useCallback)(async()=>{w(!0);try{let e=new URLSearchParams({page:String(R)});T&&e.set("stage",T),"newest"!==E&&e.set("sort",E);let t=eI();t.startDate&&e.set("startDate",t.startDate),t.endDate&&e.set("endDate",t.endDate);let a=await fetch("/api/campaigns/".concat(d.id,"/leads?").concat(e));if(a.ok){let e=await a.json();m(e.leads||[]),P(e.totalPages||1),e.phoneTypeStats&&Q(e.phoneTypeStats);try{let e=await fetch("/api/campaigns/".concat(d.id,"/domain-health"));if(e.ok){let t=await e.json();Y(t.domains||[])}}catch(e){}}}catch(e){console.error("Failed to fetch campaign leads:",e)}finally{w(!1)}},[d.id,R,T,E,eI]);(0,s.useEffect)(()=>{e_()},[e_]);let eO=(0,s.useCallback)(async()=>{try{var e;let t=new URLSearchParams({campaignId:d.id}),a=eI();a.startDate&&t.set("startDate",a.startDate),a.endDate&&t.set("endDate",a.endDate);let r=await fetch("/api/campaigns/live-stats?".concat(t));if(!r.ok)return;let s=await r.json(),l=null==s?void 0:null===(e=s.stats)||void 0===e?void 0:e[d.id];if(!l)return;B({QUEUED:l.queued,TEXTED:l.texted,CLICKED:l.clicked,OPTED_IN:l.optedIn,HOT:l.hot,CLOSED:l.closed,OPTED_OUT:l.optedOut,ARCHIVED:l.archived}),ed(l.clicked),eu(l.closed),ee(l.delivered),X(l.texted),eg(l.optedOut)}catch(e){}},[d.id,eI]);(0,l.C)(eO,1e4),(0,s.useEffect)(()=>{eO()},[eO]);let eM=(0,s.useCallback)(async e=>{H(!0);try{let t=await fetch("/api/campaigns/".concat(d.id,"/leads/").concat(e,"/timeline"));if(t.ok){let e=await t.json();W(e.events||[])}}catch(e){console.error("Failed to fetch timeline:",e)}finally{H(!1)}},[d.id]),eZ=(0,s.useCallback)(e=>{O===e?(M(null),W([])):(M(e),eM(e))},[O,eM]),ez=(0,s.useCallback)(async e=>{ep(!0);try{let t;if(!(t="start"===e?await fetch("/api/campaigns/".concat(d.id,"/start"),{method:"POST"}):"pause"===e?await fetch("/api/campaigns/".concat(d.id),{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({status:"PAUSED"})}):await fetch("/api/campaigns/".concat(d.id),{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({status:"SENDING"})})).ok){let a=await t.json().catch(()=>({}));alert(a.error||"Failed to ".concat(e," campaign"));return}o()}catch(e){console.error("Campaign action failed:",e),alert("Network error — please try again")}finally{ep(!1)}},[d.id,o]),eW=I[d.status]||I.DRAFT,eU=d.sentCount||0;return(0,r.jsxs)("div",{className:"space-y-6",children:[(0,r.jsxs)("div",{className:"flex items-center gap-4",children:[(0,r.jsx)("button",{onClick:i,className:"p-2 rounded-lg border border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors",children:(0,r.jsx)(y.Z,{size:18,className:"text-gray-500 dark:text-gray-400"})}),(0,r.jsxs)("div",{className:"flex-1",children:[(0,r.jsxs)("div",{className:"flex items-center gap-3",children:[(0,r.jsx)("h2",{className:"text-xl font-bold text-gray-900 dark:text-white",children:d.name}),(0,r.jsx)("span",{className:"px-2 py-1 rounded-full text-xs font-medium ".concat(eW.bg," ").concat(eW.text),children:d.status})]}),(0,r.jsxs)("p",{className:"text-sm text-gray-500 dark:text-gray-400 mt-0.5",children:["Created ",(0,n.p6)(d.createdAt)]})]}),(0,r.jsxs)("div",{className:"flex items-center gap-2",children:["DRAFT"===d.status&&(0,r.jsxs)("button",{onClick:()=>ez("start"),disabled:ey,className:"bg-green-600 text-white px-4 py-2 rounded-lg hover:bg-green-700 transition-colors text-sm font-medium disabled:opacity-50 flex items-center gap-2",children:[(0,r.jsx)(p.Z,{size:16}),"Start Sending"]}),"SENDING"===d.status&&(0,r.jsxs)("button",{onClick:()=>ez("pause"),disabled:ey,className:"bg-yellow-600 text-white px-4 py-2 rounded-lg hover:bg-yellow-700 transition-colors text-sm font-medium disabled:opacity-50 flex items-center gap-2",children:[(0,r.jsx)(b.Z,{size:16}),"Pause"]}),"PAUSED"===d.status&&(0,r.jsxs)("button",{onClick:()=>ez("resume"),disabled:ey,className:"bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition-colors text-sm font-medium disabled:opacity-50 flex items-center gap-2",children:[(0,r.jsx)(p.Z,{size:16}),"Resume"]}),"COMPLETED"===d.status&&(0,r.jsxs)("button",{onClick:()=>ez("resume"),disabled:ey,className:"bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition-colors text-sm font-medium disabled:opacity-50 flex items-center gap-2",children:[(0,r.jsx)(p.Z,{size:16}),"Resume Sending"]}),["DRAFT","SENDING","PAUSED","COMPLETED"].includes(d.status)&&(0,r.jsxs)("button",{onClick:()=>eh(!0),className:"border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300 px-4 py-2 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors text-sm font-medium flex items-center gap-2",children:[(0,r.jsx)(h.Z,{size:16}),"Add Leads"]}),"ARCHIVED"!==d.status&&(0,r.jsxs)("button",{onClick:()=>ek(!0),className:"border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300 px-4 py-2 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors text-sm font-medium flex items-center gap-2",children:[(0,r.jsx)(f.Z,{size:16}),"Edit"]}),(0,r.jsxs)("button",{onClick:()=>ej(!0),className:"border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300 px-4 py-2 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors text-sm font-medium flex items-center gap-2",children:[(0,r.jsx)(k.Z,{size:16}),"Export Stats"]}),(0,r.jsxs)("button",{onClick:()=>{let e=new URLSearchParams;T&&e.set("stage",T);let t=eI();t.startDate&&e.set("startDate",t.startDate),t.endDate&&e.set("endDate",t.endDate);let a="/api/campaigns/".concat(d.id,"/leads/export?").concat(e),r=document.createElement("a");r.href=a,r.download="",document.body.appendChild(r),r.click(),document.body.removeChild(r)},className:"border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300 px-4 py-2 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors text-sm font-medium flex items-center gap-2",children:[(0,r.jsx)(k.Z,{size:16}),"Export Leads"]}),"SENDING"!==d.status&&(0,r.jsxs)("button",{onClick:async()=>
1{if(window.confirm("Are you sure? This will permanently delete this campaign and all associated records. Leads will not be deleted.")){ep(!0);try{let e=await fetch("/api/campaigns/".concat(d.id),{method:"DELETE"});if(e.ok)i();else{let t=await e.json().catch(()=>({}));alert(t.error||"Failed to delete campaign")}}catch(e){console.error("Delete failed:",e)}finally{ep(!1)}}},disabled:ey,className:"border border-red-300 dark:border-red-700 text-red-600 dark:text-red-400 px-4 py-2 rounded-lg hover:bg-red-50 dark:hover:bg-red-900/20 transition-colors text-sm font-medium disabled:opacity-50 flex items-center gap-2",children:[(0,r.jsx)(v.Z,{size:16}),"Delete"]}),(0,r.jsx)("button",{onClick:o,className:"p-2 rounded-lg border border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors",title:"Refresh",children:(0,r.jsx)(x.Z,{size:16,className:"text-gray-500 dark:text-gray-400"})})]})]}),(0,r.jsx)(S,{channel:"sms"}),d.pauseReason&&(0,r.jsxs)("div",{className:"flex items-start gap-3 px-4 py-3 rounded-lg border ".concat("manual_admin"===d.pauseCategory?"bg-gray-50 border-gray-300 text-gray-800 dark:bg-gray-900/30 dark:border-gray-700 dark:text-gray-200":"daily_limit_reached"===d.pauseCategory||"outside_send_window"===d.pauseCategory||"auto_rekick"===d.pauseCategory?"bg-blue-50 border-blue-200 text-blue-800 dark:bg-blue-900/20 dark:border-blue-800 dark:text-blue-300":"twilio_rate_limit"===d.pauseCategory||"twilio_carrier_block"===d.pauseCategory||"all_leads_archived"===d.pauseCategory||"empty_queue_at_start"===d.pauseCategory||"delivery_failure_pattern"===d.pauseCategory?"bg-yellow-50 border-yellow-300 text-yellow-900 dark:bg-yellow-900/20 dark:border-yellow-800 dark:text-yellow-300":"bg-red-50 border-red-300 text-red-900 dark:bg-red-900/20 dark:border-red-800 dark:text-red-300"),children:[(0,r.jsxs)("div",{className:"flex-shrink-0 mt-0.5",children:["manual_admin"===d.pauseCategory&&(0,r.jsx)(b.Z,{size:16}),("daily_limit_reached"===d.pauseCategory||"outside_send_window"===d.pauseCategory||"auto_rekick"===d.pauseCategory)&&(0,r.jsx)(j.Z,{size:16}),!["manual_admin","daily_limit_reached","outside_send_window","auto_rekick"].includes(d.pauseCategory||"")&&(0,r.jsx)(N.Z,{size:16})]}),(0,r.jsxs)("div",{className:"flex-1 min-w-0",children:[(0,r.jsx)("div",{className:"text-xs font-semibold uppercase tracking-wide opacity-70 mb-0.5",children:"manual_admin"===d.pauseCategory?"Paused":"daily_limit_reached"===d.pauseCategory?"Waiting — daily limit":"outside_send_window"===d.pauseCategory?"Waiting — send window":"auto_rekick"===d.pauseCategory?"Restarting — automatic pause/resume":"master_switch_off"===d.pauseCategory?"Stopped — master switch off":"twilio_auth_failed"===d.pauseCategory?"Stopped — Twilio auth":"twilio_insufficient_funds"===d.pauseCategory?"Stopped — Twilio out of funds":"twilio_rate_limit"===d.pauseCategory?"Stopped — Twilio rate limit":"twilio_tfn_unverified"===d.pauseCategory?"Stopped — TFN not verified":"twilio_carrier_block"===d.pauseCategory?"Stopped — carrier block":"circuit_breaker"===d.pauseCategory?"Stopped — circuit breaker":"delivery_failure_pattern"===d.pauseCategory?"Stopped — delivery failure pattern":"worker_crashed"===d.pauseCategory?"Stopped — worker crashed":"worker_exception"===d.pauseCategory?"Stopped — worker exception":"empty_queue_at_start"===d.pauseCategory?"Ended — no sendable leads":"all_leads_archived"===d.pauseCategory?"Ended — most leads archived":"Stopped"}),(0,r.jsx)("div",{className:"text-sm leading-snug",children:d.pauseReason}),d.pausedAt&&(0,r.jsxs)("div",{className:"text-xs opacity-60 mt-1",children:["system"===d.pausedBy?"Auto-detected":"Paused by ".concat(d.pausedBy)," \xb7 ",new Date(d.pausedAt).toLocaleString("en-US",{timeZone:"America/New_York",timeZoneName:"short"})]})]})]}),(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow p-6",children:[(0,r.jsxs)("div",{className:"flex items-center justify-between mb-4",children:[(0,r.jsx)("h3",{className:"text-sm font-semibold text-gray-700 dark:text-gray-300",children:"Funnel"}),(0,r.jsxs)("div",{className:"flex items-center gap-2",children:[["all","today","7d","30d","custom"].map(e=>(0,r.jsx)("button",{onClick:()=>{eF(e),L(1),eD(new Set)},className:"px-3 py-1 rounded-lg text-xs font-medium transition-colors ".concat(eE===e?"bg-slate-900 text-white dark:bg-slate-100 dar
1k:text-slate-900":"bg-gray-100 dark:bg-slate-800 text-gray-600 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-slate-700"),children:"all"===e?"All Time":"today"===e?"Today":"7d"===e?"7 Days":"30d"===e?"30 Days":"Custom"},e)),"custom"===eE&&(0,r.jsxs)("div",{className:"flex items-center gap-1 ml-1",children:[(0,r.jsx)("input",{type:"date",value:eR,onChange:e=>{eL(e.target.value),L(1),eD(new Set)},className:"border border-gray-300 dark:border-gray-600 rounded-lg px-2 py-1 text-xs bg-white dark:bg-gray-900 text-gray-900 dark:text-white"}),(0,r.jsx)("span",{className:"text-xs text-gray-400",children:"to"}),(0,r.jsx)("input",{type:"date",value:eA,onChange:e=>{eP(e.target.value),L(1),eD(new Set)},className:"border border-gray-300 dark:border-gray-600 rounded-lg px-2 py-1 text-xs bg-white dark:bg-gray-900 text-gray-900 dark:text-white"})]})]})]}),(0,r.jsxs)("div",{className:"flex flex-wrap gap-2",children:[_.filter(e=>"REP_CALLED"!==e.key&&"SET"!==e.key).map(e=>{let t=q[e.key]||0,a=T===e.key;return(0,r.jsxs)("button",{onClick:()=>{D(a?null:e.key),L(1),eD(new Set)},className:"flex items-center gap-2 px-3 py-2 rounded-lg border text-sm transition-all ".concat(a?"".concat(e.bgColor," border-current ").concat(e.textColor," ring-2 ring-offset-1 ring-blue-400"):"border-gray-200 dark:border-gray-700 hover:border-gray-300 dark:hover:border-gray-600"),children:[(0,r.jsx)("div",{className:"w-3 h-3 rounded-full ".concat(e.color)}),(0,r.jsx)("span",{className:a?e.textColor:"text-gray-700 dark:text-gray-300",children:e.label}),(0,r.jsx)("span",{className:"font-semibold ".concat(a?e.textColor:"text-gray-900 dark:text-white"),children:t})]},e.key)}),T&&(0,r.jsxs)("button",{onClick:()=>{D(null),L(1),eD(new Set)},className:"flex items-center gap-1 px-3 py-2 rounded-lg text-sm text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 transition-colors",children:[(0,r.jsx)(c.Z,{size:14}),"Clear filter"]})]})]}),(0,r.jsx)(el,{campaignId:d.id,onRefreshParent:o}),(0,r.jsx)(eo,{campaignId:d.id,templateBody:d.templateBody,tfnPool:null!==(a=null===(t=d.settings)||void 0===t?void 0:t.tfnPool)&&void 0!==a?a:null}),(()=>{var e,t;let a=null!=G?G:eU,s=null!=$?$:null!==(e=d.deliveredCount)&&void 0!==e?e:0,l=null!=en?en:d.clickedCount,n=null!=ei?ei:d.optedOutCount,i=null!=em?em:null!==(t=d.closedCount)&&void 0!==t?t:0;return(0,r.jsxs)("div",{className:"grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4",children:[(0,r.jsx)(ec,{label:"Attempted",value:String(a)}),(0,r.jsx)(ec,{label:"Delivered",value:String(s)}),(0,r.jsx)(ec,{label:"Delivery Rate",value:"".concat(Z(s,a),"%")}),(0,r.jsx)(ec,{label:"Click Rate (CTR)",value:"".concat(Z(l,s),"%")}),(0,r.jsx)(ec,{label:"Opt-Out Rate",value:"".concat(Z(n,s),"%")}),(0,r.jsx)(ec,{label:"Close Rate",value:"".concat(Z(i,s),"% \xb7 ").concat(i)})]})})(),(0,r.jsx)(V,{campaignId:d.id,startDate:eI().startDate,endDate:eI().endDate}),(()=>{let e=(K.mobile||0)+(K.landline||0)+(K.voip||0)+(K.unknown||0);if(0===e)return null;let t=[{key:"mobile",label:"Mobile",color:"bg-green-500",dot:"bg-green-500",count:K.mobile||0},{key:"landline",label:"Landline",color:"bg-red-500",dot:"bg-red-500",count:K.landline||0},{key:"voip",label:"VoIP",color:"bg-orange-500",dot:"bg-orange-500",count:K.voip||0},{key:"unknown",label:"Unknown",color:"bg-gray-400",dot:"bg-gray-400",count:K.unknown||0}];return(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow border border-gray-200 dark:border-gray-700 p-4",children:[(0,r.jsxs)("div",{className:"flex items-center justify-between mb-2",children:[(0,r.jsxs)("h3",{className:"text-sm font-semibold text-gray-700 dark:text-gray-300",children:["Phone Types",T?" — ".concat(T):""]}),(0,r.jsx)("div",{className:"flex items-center gap-4 text-xs text-gray-600 dark:text-gray-400",children:t.map(t=>(0,r.jsxs)("span",{className:"flex items-center gap-1.5",children:[(0,r.jsx)("span",{className:"w-2.5 h-2.5 rounded-full ".concat(t.dot)}),t.label,": ",t.count," (",(t.count/e*100).toFixed(1),"%)"]},t.key))})]}),(0,r.jsx)("div",{className:"flex w-full h-3 rounded-full overflow-hidden",children:t.filter(e=>e.count>0).map(t=>(0,r.jsx)("div",{className:t.color,style:{width:"".concat(t.count/e*100,"%")}},t.key))})]})})(),J.length>0&&(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow border border-gray-200 dark:border-gray-700 p-4",children:[(0,r.jsx)("h3",{className:"text-sm font-semibold text-gray-700 dark:text-gray-300 mb-3",children:"Domain Delivery Health"}),(0,r.jsxs)("div",{className:"space-y-2",children:[J.filter(e=>!1!==e.inPool).length>0&&(0,r.jsx)("p",{className:"text-[10px] text-teal-600 font-medium uppercase tracking-wider",children:"Active Pool"}),J.map(e=>{let t=!1===e.inPool,a=e.total<10?{text:"Not enough data",cls:"bg-gray-100 dark:bg-gray-700 text-gray-500 dark:text-gray-400"}:e.failureRate>=20?{text:"Flagged",cls:"bg-red-100 dark:bg-red-900/40 text-red-700 dark:text-red-300"}:e.failureRate>=10?{text:"Warning",cls:"bg-yellow-100 dark:bg-yellow-900/40 text-yellow-700 dark:text-yellow-300"}:{text:"Healthy",cls:"bg-green-100 dark:bg-green-900/40 text-green-700 dark:text-green-300"};return(0,r.jsxs)("div",{children:[t&&J.findIndex(e=>!1===e.inPool)===J.indexOf(e)&&(0,r.jsx)("p",{className:"text-[10px] text-gray-400 font-medium uppercase tracking-wider mt-3 mb-1",children:"Historical (no longer in pool)"}),(0,r.jsxs)("div",{className:"flex items-center gap-2 ".concat(t?"opacity-50":""),children:[(0,r.jsx)("span",{className:"text-xs font-mono text-gray-600 dark:text-gray-400 w-[220px] truncate flex-shrink-0",children:e.domain}),(0,r.jsxs)("div",{className:"flex-1 h-3 rounded-full bg-gray-200 dark:bg-gray-700 overflow-hidden flex",children:[e.deliveryRate>0&&(0,r.jsx)("div",{className:"bg-green-500",style:{width:"".concat(e.deliveryRate,"%")}}),e.failureRate>0&&(0,r.jsx)("div",{className:"bg-red-500",style:{width:"".concat(e.failureRate,"%")}}),100-e.deliveryRate-e.failureRate>0&&(0,r.jsx)("div",{className:"bg-gray-400",style:{width:"".concat(Math.max(0,100-e.deliveryRate-e.failureRate),"%")}})]}),(0,r.jsxs)("span",{className:"text-xs text-gray-500 dark:text-gray-400 whitespace-nowrap ml-2",children:[e.delivered,"/",e.total," (",e.deliveryRate.toFixed(1),"%)"]}),(0,r.jsx)("span",{className:"px-2 py-0.5 rounded-full text-xs font-medium ml-2 whitespace-nowrap ".concat(a.cls),children:a.text})]})]},e.domain)})]}),(0,r.jsxs)("div",{className:"flex items-center gap-4 text-xs text-gray-500 dark:text-gray-400 mt-2 pt-2 border-t border-gray-100 dark:border-gray-700",children:[(0,r.jsxs)("span",{className:"flex items-center gap-1.5",children:[(0,r.jsx)("span",{className:"w-2.5 h-2.5 rounded-full bg-green-500"})," Delivered"]}),(0,r.jsxs)("span",{className:"flex items-center gap-1.5",children:[(0,r.jsx)("span",{className:"w-2.5 h-2.5 rounded-full bg-red-500"})," Failed/Undelivered"]}),(0,r.jsxs)("span",{className:"flex items-center gap-1.5",children:[(0,r.jsx)("span",{className:"w-2.5 h-2.5 rounded-full bg-gray-400"})," Pending"]})]})]}),(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow overflow-hidden",children:[(0,r.jsxs)("div",{className:"px-6 py-4 border-b border-gray-200 dark:border-gray-700 flex items-center justify-between",children:[(0,r.jsxs)("h3",{className:"text-sm font-semibold text-gray-700 dark:text-gray-300",children:["Leads ",T?"— ".concat(T):""]}),(0,r.jsxs)("select",{value:E,onChange:e=>{F(e.target.value),L(1),eD(new Set)},className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-1.5 text-xs bg-white dark:bg-gray-900 text-gray-700 dark:text-gray-300 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none",children:[(0,r.jsx)("option",{value:"newest",children:"Newest First"}),(0,r.jsx)("option",{value:"oldest",children:"Oldest First"}
1),(0,r.jsx)("option",{value:"score_high",children:"Highest Score"}),(0,r.jsx)("option",{value:"score_low",children:"Lowest Score"}),(0,r.jsx)("option",{value:"company_az",children:"Company A → Z"}),(0,r.jsx)("option",{value:"company_za",children:"Company Z → A"}),(0,r.jsx)("option",{value:"last_activity",children:"Last Activity"})]})]}),u?(0,r.jsx)("div",{className:"flex items-center justify-center py-12",children:(0,r.jsx)("div",{className:"animate-spin rounded-full h-6 w-6 border-b-2 border-blue-600"})}):0===g.length?(0,r.jsxs)("div",{className:"text-center py-12 text-sm text-gray-500 dark:text-gray-400",children:["No leads found",T?' in stage "'.concat(T,'"'):""]}):(0,r.jsx)("div",{className:"overflow-x-auto",children:(0,r.jsxs)("table",{className:"w-full",children:[(0,r.jsx)("thead",{children:(0,r.jsxs)("tr",{className:"border-b border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50",children:[(0,r.jsx)("th",{className:"px-4 py-3 w-10",children:(0,r.jsx)(C.D,{pageItemIds:g.map(e=>e.leadId),selectedIds:eT,onSelectionChange:eD})}),(0,r.jsx)("th",{className:"text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider px-6 py-3",children:"Company"}),(0,r.jsx)("th",{className:"text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider px-6 py-3",children:"Contact"}),(0,r.jsx)("th",{className:"text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider px-6 py-3",children:"Phone"}),(0,r.jsx)("th",{className:"text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider px-6 py-3",children:"Type"}),(0,r.jsx)("th",{className:"text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider px-6 py-3",children:"City/State"}),(0,r.jsx)("th",{className:"text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider px-6 py-3",children:"Pipeline"}),(0,r.jsx)("th",{className:"text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider px-6 py-3",children:"Last Activity"}),(0,r.jsx)("th",{className:"text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider px-6 py-3",children:"Rep"}),(0,r.jsx)("th",{className:"text-center text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider px-6 py-3 w-16"})]})}),(0,r.jsx)("tbody",{children:g.map(e=>{let t=_.find(t=>t.key===e.stage),a=O===e.leadId;return(0,r.jsx)(ex,{lead:e,stageInfo:t,isExpanded:a,isSelected:eT.has(e.leadId),onSelect:t=>{let a=new Set(eT);t?a.add(e.leadId):a.delete(e.leadId),eD(a)},onToggle:()=>eZ(e.leadId),reps:eC,campaignId:d.id,onRepAssigned:()=>e_(),onRemove:async()=>{try{let t=await fetch("/api/campaigns/".concat(d.id,"/leads/").concat(e.leadId),{method:"DELETE"});if(t.ok)e_(),o();else{let e=await t.json().catch(()=>({}));alert(e.error||"Failed to remove lead")}}catch(e){console.error("Remove lead failed:",e)}},timeline:a?z:[],timelineLoading:a&&U},e.id||e.leadId)})})]})}),A>1&&(0,r.jsxs)("div",{className:"flex items-center justify-between px-6 py-3 border-t border-gray-200 dark:border-gray-700",children:[(0,r.jsx)("button",{onClick:()=>{L(e=>Math.max(1,e-1)),eD(new Set)},disabled:R<=1,className:"px-3 py-1 rounded text-sm border border-gray-300 dark:border-gray-600 disabled:opacity-30 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors text-gray-700 dark:text-gray-300",children:"Previous"}),(0,r.jsxs)("span",{className:"text-sm text-gray-500 dark:text-gray-400",children:["Page ",R," of ",A]}),(0,r.jsx)("button",{onClick:()=>{L(e=>Math.min(A,e+1)),eD(new Set)},disabled:R>=A,className:"px-3 py-1 rounded text-sm border border-gray-300 dark:border-gray-600 disabled:opacity-30 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors text-gray-700 dark:text-gray-300",children:"Next"})]})]}),d.fromNumber&&(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow p-4 border border-gray-200 dark:border-gray-700",children:[(0,r.jsx)("p",{className:"text-xs text-gray-500 dark:text-gray-400 mb-1",children:"From Number"}),(0,r.jsx)("p",{className:"text-sm font-mono text-gray-900 dark:text-white",children:d.fromNumber})]}),eT.size>0&&(0,r.jsx)("div",{className:"fixed bottom-0 left-0 right-0 bg-white dark:bg-slate-900 border-t border-gray-200 dark:border-slate-700 shadow-lg z-50 px-6 py-3",children:(0,r.jsxs)("div",{className:"max-w-[1400px] mx-auto flex items-center justify-between",children:[(0,r.jsxs)("span",{className:"text-sm font-medium text-gray-700 dark:text-gray-300",children:[eT.size," lead",1!==eT.size?"s":""," selected"]}),(0,r.jsxs)("div",{className:"flex items-center gap-2",children:[(0,r.jsx)("button",{onClick:()=>ew(!0),className:"px-4 py-2 text-sm font-medium bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors",children:"Assign to Rep"}),(0,r.jsx)("button",{onClick:async()=>
1{if(window.confirm("Remove ".concat(eT.size," lead").concat(1!==eT.size?"s":""," from this campaign?")))try{let e=await fetch("/api/campaigns/".concat(d.id,"/leads/bulk-remove"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({leadIds:[...eT]})});if(e.ok){let t=await e.json();eD(new Set),e_(),o(),t.skipped>0&&alert("Removed ".concat(t.removed," lead").concat(1!==t.removed?"s":"",". ").concat(t.skipped," skipped (active funnel stage)."))}else{let t=await e.json().catch(()=>({}));alert(t.error||"Failed to remove leads")}}catch(e){console.error("Bulk remove failed:",e),alert("Network error — please try again")}},className:"px-4 py-2 text-sm font-medium bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors",children:"Remove from Campaign"}),(0,r.jsx)("button",{onClick:()=>eD(new Set),className:"px-3 py-2 text-sm text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-300",children:"Cancel"})]})]})}),eb&&(0,r.jsx)(et,{campaignId:d.id,onClose:()=>eh(!1),onAdded:()=>{eh(!1),e_(),o()}}),ef&&(0,r.jsx)(ea,{campaign:d,onClose:()=>ek(!1),onSaved:()=>{ek(!1),o()}}),eN&&(0,r.jsx)(es,{campaignId:d.id,leadIds:[...eT],reps:eC,title:"Assign ".concat(eT.size," Lead").concat(1!==eT.size?"s":""," to Rep"),onClose:()=>ew(!1),onAssigned:()=>{ew(!1),eD(new Set),e_()}}),ev&&(0,r.jsx)(er,{campaignId:d.id,campaignName:d.name,onClose:()=>ej(!1)})]})}function et(e){let{campaignId:t,onClose:a,onAdded:l}=e,[n,d]=(0,s.useState)([]),[i,o]=(0,s.useState)(""),[x,g]=(0,s.useState)(""),[m,u]=(0,s.useState)(!1),[y,p]=(0,s.use
1State)(""),[b,h]=(0,s.useState)(null);(0,s.useEffect)(()=>{fetch("/api/folders").then(e=>e.json()).then(e=>d(e.folders||[])).catch(()=>{})},[]);let f=async()=>{if(!i&&!x.trim()){p("Select a folder or paste lead IDs");return}u(!0),p(""),h(null);try{let e={};i&&(e.folderId=i),x.trim()&&(e.leadIds=x.split(",").map(e=>e.trim()).filter(Boolean));let a=await fetch("/api/campaigns/".concat(t,"/leads"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}),r=await a.json();if(!a.ok)throw Error(r.error||"Failed to add leads");h(r),r.added>0&&setTimeout(l,1500)}catch(e){p(e.message||"Failed to add leads")}finally{u(!1)}};return(0,r.jsx)("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-xl shadow-xl max-w-lg w-full p-6",children:[(0,r.jsxs)("div",{className:"flex items-center justify-between mb-4",children:[(0,r.jsx)("h3",{className:"text-lg font-bold text-gray-900 dark:text-white",children:"Add Leads to Campaign"}),(0,r.jsx)("button",{onClick:a,className:"p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-700",children:(0,r.jsx)(c.Z,{size:18})})]}),(0,r.jsxs)("div",{className:"space-y-4",children:[(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"text-xs text-gray-500 dark:text-gray-400 mb-1 block",children:"Select a Folder"}),(0,r.jsxs)("select",{value:i,onChange:e=>o(e.target.value),className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none",children:[(0,r.jsx)("option",{value:"",children:"— None —"}),n.map(e=>{var t,a;return(0,r.jsxs)("option",{value:e.id,children:[e.name," (",null!==(a=null===(t=e._count)||void 0===t?void 0:t.leads)&&void 0!==a?a:"?"," leads)"]},e.id)})]})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"text-xs text-gray-500 dark:text-gray-400 mb-1 block",children:"Or Paste Lead IDs (comma-separated)"}),(0,r.jsx)("textarea",{value:x,onChange:e=>g(e.target.value),className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none resize-none text-sm",rows:3,placeholder:"lead-id-1, lead-id-2, ..."})]}),y&&(0,r.jsx)("p",{className:"text-sm text-red-600 dark:text-red-400",children:y}),b&&(0,r.jsxs)("p",{className:"text-sm text-green-600 dark:text-green-400",children:["Added ",b.added," leads",b.skipped>0?", ".concat(b.skipped," already in campaign"):"",". Total: ",b.total]})]}),(0,r.jsxs)("div",{className:"flex justify-end gap-3 mt-6",children:[(0,r.jsx)("button",{onClick:a,className:"px-4 py-2 text-sm text-gray-600 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg",children:"Cancel"}),(0,r.jsx)("button",{onClick:f,disabled:m,className:"bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 text-sm font-medium disabled:opacity-50 flex items-center gap-2",children:m?"Adding...":"Add Leads"})]})]})})}function ea(e){var t;let{campaign:a,onClose:l,onSaved:n}=e,[d,i]=(0,s.useState)(a.name),[o,x]=(0,s.useState)(a.templateBody),[g,m]=(0,s.useState)(a.fromNumber||""),[u,y]=(0,s.useState)(!1),[p,b]=(0,s.useState)(""),h=(0,s.useRef)(null),f=null!==(t=a.settings)&&void 0!==t?t:null,k=f&&Array.isArray(f.tfnPool)&&f.tfnPool.length>0?f.tfnPool:null,v=null!==k,[j,N]=(0,s.useState)(null!=k?k:[]),[w,C]=(0,s.useState)((null==f?void 0:f.smartWeeklyEnabled)===!0),[S,T]=(0,s.useState)(!1),D=e=>{let t=h.current;if(!t)return;let a=t.selectionStart,r=t.selectionEnd;x(o.slice(0,a)+e+o.slice(r)),setTimeout(()=>{t.focus(),t.selectionStart=t.selectionEnd=a+e.length},0)},E=async()=>{if(!d.trim()){b("Name is required");return}if(!o.trim()){b(v?"Fallback template is required":"Template is required");return}if(v){if(0===j.length){b("Pick at least one TFN for the pool"),T(!0);return}for(let e of j){if(!Number.isInteger(e.throttlePerMinute)||e.throttlePerMinute<1||e.throttlePerMinute>30){b("TFN ".concat(e.tfn,": throttle must be 1–30/min")),T(!0);return}if(!Number.isInteger(e.dailyCap)||e.dailyCap<1){b("TFN ".concat(e.tfn,": daily cap must be ≥ 1")),T(!0);return}if(e.sendWindow.endHour<=e.sendWindow.startHour){b("TFN ".concat(e.tfn,": send window end must be after start")),T(!0);return}if(!Array.isArray(e.sendWindow.days)||0===e.sendWindow.days.length){b("TFN ".concat(e.tfn,": pick at least one send day")),T(!0);return}if(w&&(!Number.isInteger(e.weeklyTarget)||e.weeklyTarget<1)){b("TFN ".concat(e.tfn,": weekly target required while Smart Weekly is on")),T(!0);return}if(!w&&void 0!==e.weeklyTarget&&(!Number.isInteger(e.weeklyTarget)||e.weeklyTarget<1)){b("TFN ".concat(e.tfn,": weekly target must be a positive integer")),T(!0);return}if(void 0!==e.emergencyCap&&(!Number.isInteger(e.emergencyCap)||e.emergencyCap<1)){b("TFN ".concat(e.tfn,": emergency cap must be a positive integer")),T(!0);return}}}else if(!g.trim()){b("Pick a From Number");return}y(!0),b("");try{let e={};if(d!==a.name&&(e.name=d.trim()),o!==a.templateBody&&(e.templateBody=o.trim()),v?(e.tfnPool=j.map(e=>{var t;let a=(null!==(t=e.templates)&&void 0!==t?t:[]).filter(e=>"string"==typeof e&&e.trim().length>0);if(0===a.length){let{templates:t,...a}=e;return a}return{...e,templates:a}}),e.smartWeeklyEnabled=w):g!==a.fromNumber&&(e.fromNumber=g.trim()),0===Object.keys(e).length){n();return}let t=await fetch("/api/campaigns/".concat(a.id),{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!t.ok){let e=await t.json().catch(()=>({}));throw Error(e.error||"Failed to save")}n()}catch(e){b(e.message||"Failed to save")}finally{y(!1)}},F=o.length,L=F<=160?1:Math.ceil(F/153);return(0,r.jsx)("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-xl shadow-xl max-w-2xl w-full p-6 max-h-[90vh] overflow-y-auto",children:[(0,r.jsxs)("div",{className:"flex items-center justify-between mb-4",children:[(0,r.jsx)("h3",{className:"text-lg font-bold text-gray-900 dark:text-white",children:"Edit Campaign"}),(0,r.jsx)("button",{onClick:l,className:"p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-700",children:(0,r.jsx)(c.Z,{size:18})})]}),"DRAFT"!==a.status&&(0,r.jsx)("div",{className:"mb-4 px-4 py-3 bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-700 rounded-lg",children:(0,r.jsxs)("p",{className:"text-sm font-medium text-amber-800 dark:text-amber-300",children:["This campaign is currently ",a.status,". Changing the template or pool config affects future sends only. Previously sent leads and leads already stickied to a TFN are not affected."]})}),(0,r.jsxs)("div",{className:"space-y-4",children:[!v&&(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"text-xs text-gray-500 dark:text-gray-400 mb-1 block",children:"Campaign Name"}),(0,r.jsx)("input",{value:d,onChange:e=>i(e.target.value),className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none"})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"text-xs text-gray-500 dark:text-gray-400 mb-1 block",children:"Cold Text Template"}),(0,r.jsx)("div",{className:"flex flex-wrap gap-1 mb-2",children:["{companyName}","{city}","{state}","{industry}","{previewUrl}"].map(e=>(0,r.jsx)("button",{onClick:()=>
1D(e),className:"text-xs bg-blue-100 dark:bg-blue-900/40 text-blue-700 dark:text-blue-300 px-2 py-0.5 rounded-full hover:bg-blue-200 dark:hover:bg-blue-800 transition-colors",children:e},e))}),(0,r.jsx)("textarea",{ref:h,value:o,onChange:e=>x(e.target.value),className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none resize-none text-sm",rows:4}),(0,r.jsxs)("p",{className:"text-xs text-gray-400 mt-1",children:[F," chars \xb7 ",L," segment",1!==L?"s":""]})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"text-xs text-gray-500 dark:text-gray-400 mb-1 block",children:"From Number"}),(0,r.jsx)(X,{value:g,onChange:m,allowSystemDefault:!1})]})]}),v&&(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"text-xs text-gray-500 dark:text-gray-400 mb-1 block",children:"Campaign Name"}),(0,r.jsx)("input",{value:d,onChange:e=>i(e.target.value),className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none"})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"text-xs text-gray-500 dark:text-gray-400 mb-1 block",children:"Fallback Template"}),(0,r.jsx)("p",{className:"text-xs text-gray-500 dark:text-gray-400 mb-1",children:"Used only when a TFN has no per-TFN templates."}),(0,r.jsx)("div",{className:"flex flex-wrap gap-1 mb-2",children:["{companyName}","{city}","{state}","{industry}","{previewUrl}"].map(e=>(0,r.jsx)("button",{onClick:()=>D(e),className:"text-xs bg-blue-100 dark:bg-blue-900/40 text-blue-700 dark:text-blue-300 px-2 py-0.5 rounded-full hover:bg-blue-200 dark:hover:bg-blue-800 transition-colors",children:e},e))}),(0,r.jsx)("textarea",{ref:h,value:o,onChange:e=>x(e.target.value),rows:3,className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white font-mono text-sm"}),(0,r.jsxs)("p",{className:"text-xs text-gray-400 mt-1",children:[F," chars \xb7 ",L," segment",1!==L?"s":""]})]}),(0,r.jsx)(R,{value:j,onChange:N,showErrors:S,smartWeeklyEnabled:w,onSmartWeeklyChange:C})]}),p&&(0,r.jsx)("p",{className:"text-sm text-red-600 dark:text-red-400",children:p})]}),(0,r.jsxs)("div",{className:"flex justify-end gap-3 mt-6",children:[(0,r.jsx)("button",{onClick:l,className:"px-4 py-2 text-sm text-gray-600 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg",children:"Cancel"}),(0,r.jsx)("button",{onClick:E,disabled:u,className:"bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 text-sm font-medium disabled:opacity-50",children:u?"Saving...":"Save Changes"})]})]})})}function er(e){let{campaignId:t,campaignName:a,onClose:l}=e,[n,d]=(0,s.useState)(!0),[i,o]=(0,s.useState)(!0),[x,g]=(0,s.useState)(!0),[m,u]=(0,s.useState)(!0),[y,p]=(0,s.use
1State)(""),[b,h]=(0,s.useState)(""),[f,v]=(0,s.useState)(!1),[j,N]=(0,s.useState)(""),w=async()=>{v(!0),N("");try{let e=new URLSearchParams;n&&e.set("funnel","true"),i&&e.set("phoneType","true"),x&&e.set("totals","true"),m&&e.set("reps","true"),y&&e.set("startDate",y),b&&e.set("endDate",b);let r=await fetch("/api/campaigns/".concat(t,"/export-stats?").concat(e));if(!r.ok){let e=await r.json().catch(()=>({}));N(e.error||"Failed to export stats");return}let s=await r.blob(),d=document.createElement("a"),o=URL.createObjectURL(s);d.href=o;let c=(r.headers.get("Content-Disposition")||"").split("filename=")[1];d.download=c?c.trim():"".concat(a.replace(/[^a-zA-Z0-9_-]/g,"_"),"_stats.csv"),document.body.appendChild(d),d.click(),document.body.removeChild(d),URL.revokeObjectURL(o),l()}catch(e){N("Download failed. Please try again.")}finally{v(!1)}};return(0,r.jsx)("div",{className:"fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4",onClick:l,children:(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow-xl max-w-md w-full",onClick:e=>e.stopPropagation(),children:[(0,r.jsxs)("div",{className:"flex items-center justify-between p-4 border-b border-gray-200 dark:border-gray-700",children:[(0,r.jsx)("h3",{className:"text-lg font-bold text-gray-900 dark:text-white",children:"Export Campaign Stats"}),(0,r.jsx)("button",{onClick:l,className:"p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-700",children:(0,r.jsx)(c.Z,{size:18,className:"text-gray-500"})})]}),(0,r.jsxs)("div",{className:"p-4 space-y-3",children:[(0,r.jsxs)("div",{children:[(0,r.jsxs)("div",{className:"flex items-center justify-between mb-1",children:[(0,r.jsx)("label",{className:"text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider",children:"Date Range (optional)"}),(y||b)&&(0,r.jsx)("button",{onClick:()=>{p(""),h("")},className:"text-xs text-blue-600 dark:text-blue-400 hover:underline",children:"Clear"})]}),(0,r.jsxs)("div",{className:"flex items-center gap-2",children:[(0,r.jsx)("input",{type:"date",value:y,onChange:e=>p(e.target.value),className:"flex-1 border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-1.5 text-sm bg-white dark:bg-gray-900 text-gray-700 dark:text-gray-300 outline-none focus:ring-2 focus:ring-blue-500"}),(0,r.jsx)("span",{className:"text-gray-400 text-sm",children:"—"}),(0,r.jsx)("input",{type:"date",value:b,onChange:e=>h(e.target.value),className:"flex-1 border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-1.5 text-sm bg-white dark:bg-gray-900 text-gray-700 dark:text-gray-300 outline-none focus:ring-2 focus:ring-blue-500"})]})]}),(0,r.jsx)("p",{className:"text-sm text-gray-500 dark:text-gray-400",children:"Select which sections to include:"}),[{label:"Campaign Totals",desc:"Attempted, delivered, rates",checked:x,onChange:g},{label:"Funnel Metrics",desc:"Stage counts for every funnel stage",checked:n,onChange:d},{label:"Phone Type Breakdown",desc:"Phone type distribution per funnel stage",checked:i,onChange:o},{label:"Rep Stats",desc:"Per-rep queue data",checked:m,onChange:u}].map(e=>(0,r.jsxs)("label",{className:"flex items-start gap-3 cursor-pointer",children:[(0,r.jsx)("input",{type:"checkbox",checked:e.checked,onChange:t=>e.onChange(t.target.checked),className:"mt-0.5 w-4 h-4 rounded border-gray-300 dark:border-slate-600"}),(0,r.jsxs)("div",{children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white",children:e.label}),(0,r.jsx)("p",{className:"text-xs text-gray-500 dark:text-gray-400",children:e.desc})]})]},e.label)),y&&b&&(0,r.jsx)("p",{className:"text-xs text-gray-400 dark:text-gray-500 italic",children:"When a date range is selected, daily breakdowns are included for each section."}),j&&(0,r.jsx)("p",{className:"text-sm text-red-600 dark:text-red-400",children:j})]}),(0,r.jsxs)("div",{className:"p-4 border-t border-gray-200 dark:border-gray-700 flex justify-end gap-3",children:[(0,r.jsx)("button",{onClick:l,className:"px-4 py-2 text-sm text-gray-700 dark:text-gray-300 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700",children:"Cancel"}),(0,r.jsx)("button",{onClick:w,disabled:f||!n&&!i&&!x&&!m,className:"px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700 disabled:opacity-50 flex items-center gap-2",children:f?(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)("div",{className:"animate-spin rounded-full h-4 w-4 border-b-2 border-white"}),"Exporting..."]}):(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(k.Z,{size:16}),"Download CSV"]})})]})]})})}function es(e){let{campaignId:t,leadIds:a,reps:l,title:n,onClose:d,onAssigned:i}
1=e,[o,x]=(0,s.useState)(""),[g,m]=(0,s.useState)(!1),[u,y]=(0,s.useState)(""),p=async()=>{m(!0),y("");try{let e="__unassign__"===o?null:o,r=await fetch("/api/campaigns/".concat(t,"/leads/bulk-assign"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({leadIds:a,repId:e})});if(!r.ok){let e=await r.json().catch(()=>({}));y(e.error||"Failed to assign");return}i()}catch(e){y("Network error — please try again")}finally{m(!1)}};return(0,r.jsx)("div",{className:"fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4",onClick:d,children:(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow-xl max-w-md w-full",onClick:e=>e.stopPropagation(),children:[(0,r.jsxs)("div",{className:"flex items-center justify-between p-4 border-b border-gray-200 dark:border-gray-700",children:[(0,r.jsx)("h3",{className:"text-lg font-bold text-gray-900 dark:text-white",children:n}),(0,r.jsx)("button",{onClick:d,className:"p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-700",children:(0,r.jsx)(c.Z,{size:18,className:"text-gray-500"})})]}),(0,r.jsxs)("div",{className:"p-4",children:[(0,r.jsxs)("select",{value:o,onChange:e=>x(e.target.value),className:"w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-900 text-gray-700 dark:text-gray-300 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none",children:[(0,r.jsx)("option",{value:"",disabled:!0,children:"Select a rep..."}),(0,r.jsx)("option",{value:"__unassign__",children:"— Unassign —"}),l.map(e=>(0,r.jsx)("option",{value:e.id,children:e.name},e.id))]}),u&&(0,r.jsx)("p",{className:"text-sm text-red-600 dark:text-red-400 mt-2",children:u})]}),(0,r.jsxs)("div",{className:"p-4 border-t border-gray-200 dark:border-gray-700 flex justify-end gap-3",children:[(0,r.jsx)("button",{onClick:d,className:"px-4 py-2 text-sm text-gray-700 dark:text-gray-300 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700",children:"Cancel"}),(0,r.jsx)("button",{onClick:p,disabled:""===o||g,className:"px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700 disabled:opacity-50",children:g?"Assigning...":"Assign"})]})]})})}function el(e){let{campaignId:t,onRefreshParent:a}=e,[l,n]=(0,s.useState)(!1),[i,c]=(0,s.useState)([]),[x,g]=(0,s.useState)({warmAvailable:0,coldAvailable:0}
1),[m,u]=(0,s.useState)(!1),[y,p]=(0,s.useState)(null),[b,h]=(0,s.useState)(null),[f,k]=(0,s.useState)({}),v=(0,s.useCallback)(async()=>{u(!0);try{let e=await fetch("/api/campaigns/".concat(t,"/queue-stats"));if(e.ok){let t=await e.json();c(t.reps||[]),g(t.totals||{warmAvailable:0,coldAvailable:0})}}catch(e){console.error("Failed to fetch queue stats:",e)}finally{u(!1)}},[t]);(0,s.useEffect)(()=>{l&&v()},[l,v]);let j=async(e,a)=>{p(e||"all"),h(null);try{let r=await fetch("/api/campaigns/".concat(t,"/fill-queue"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e?{repId:e,...a?{count:a}:{}}:{all:!0})});if(409===r.status){h("Another fill is in progress, try again");return}if(r.ok){let t=await r.json();if(e){let e=t.filled||{};h(t.message||"Filled ".concat(e.warm||0," warm + ").concat(e.cold||0," cold"))}else{let e=Object.entries(t.filled||{}),a=e.reduce((e,t)=>{let[,a]=t;return e+(a.warm||0)},0),r=e.reduce((e,t)=>{let[,a]=t;return e+(a.cold||0)},0);h("Filled ".concat(a," warm + ").concat(r," cold across ").concat(e.length," reps"))}v()}else{let e=await r.json().catch(()=>({}));h(e.error||"Fill failed")}}catch(e){console.error("Fill failed:",e),h("Network error")}finally{p(null)}},N=async(e,a)=>{p("empty-".concat(a,"-").concat(e)),h(null);try{let r=await fetch("/api/campaigns/".concat(t,"/empty-queue"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({repId:e,type:a})});if(409===r.status){h("Another operation is in progress, try again");return}if(r.ok){let e=await r.json();h("Emptied ".concat(e.emptied," ").concat(a," leads")),v()}else{let e=await r.json().catch(()=>({}));h(e.error||"Empty failed")}}catch(e){console.error("Empty failed:",e),h("Network error")}finally{p(null)}};return(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow",children:[(0,r.jsx)("button",{onClick:()=>n(!l),className:"w-full px-6 py-4 flex items-center justify-between text-left",children:(0,r.jsxs)("div",{className:"flex items-center gap-3",children:[l?(0,r.jsx)(d.Z,{size:16,className:"text-gray-400"}):(0,r.jsx)(o.Z,{size:16,className:"text-gray-400"}),(0,r.jsx)("h3",{className:"text-sm font-semibold text-gray-700 dark:text-gray-300",children:"Rep Queues"}),!l&&i.length>0&&(0,r.jsxs)("span",{className:"text-xs text-gray-500 dark:text-gray-400",children:[i.length," rep",1!==i.length?"s":""," \xb7 ",x.warmAvailable," warm available \xb7 ",x.coldAvailable," cold available"]})]})}),l&&(0,r.jsx)("div",{className:"px-6 pb-4 space-y-3",children:m?(0,r.jsx)("div",{className:"flex items-center justify-center py-6",children:(0,r.jsx)("div",{className:"animate-spin rounded-full h-5 w-5 border-b-2 border-blue-600"})}):(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)("div",{className:"flex items-center justify-between",children:[(0,r.jsxs)("span",{className:"text-sm text-gray-600 dark:text-gray-400",children:[x.warmAvailable," warm available \xb7 ",x.coldAvailable," cold available"]}),(0,r.jsx)("button",{onClick:()=>j(),disabled:null!==y,className:"bg-green-600 text-white px-4 py-1.5 rounded-lg text-xs font-medium hover:bg-green-700 transition-colors disabled:opacity-50",children:"all"===y?"Filling...":"Fill All Queues"})]}),i.map(e=>{let t=e.queueTarget>0?Math.min(e.currentFresh/e.queueTarget*100,100):0;return(0,r.jsxs)("div",{className:"flex items-center gap-4 py-2 border-t border-gray-100 dark:border-gray-700",children:[(0,r.jsxs)("div",{className:"flex-1 min-w-0",children:[(0,r.jsxs)("div",{className:"flex items-center gap-3 mb-1",children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white truncate",children:e.name}),(0,r.jsxs)("span",{className:"text-xs font-mono text-gray-500 dark:text-gray-400",children:[e.currentFresh," / ",e.queueTarget]})]}),(0,r.jsx)("div",{className:"w-full bg-gray-200 dark:bg-gray-700 rounded-full h-1.5",children:(0,r.jsx)("div",{className:"bg-blue-600 rounded-full h-1.5 transition-all",style:{width:"".concat(t,"%")}})}),(0,r.jsxs)("span",{className:"text-xs text-gray-400 dark:text-gray-500 mt-0.5 block",children:["In queue: ",(0,r.jsxs)("span",{className:"text-orange-500",children:[e.currentWarm," warm"]})," \xb7 ",(0,r.jsxs)("span",{className:"text-blue-400",children:[e.currentCold," cold"]}),"\xa0\xa0|\xa0\xa0Available: ",e.warmAvailable," warm \xb7 ",e.coldAvailable," cold"]})]}),(0,r.jsxs)("div",{className:"flex items-center gap-1.5 flex-shrink-0",children:[(0,r.jsxs)("div",{className:"flex items-center gap-1",children:[(0,r.jsx)("input",{type:"number",min:"1",placeholder:"All",value:f[e.id]||"",onChange:t=>k(a=>({...a,[e.id]:t.target.value})),className:"w-16 border border-gray-300 dark:border-gray-600 rounded-lg px-2 py-1 text-xs bg-white dark:bg-gray-900 text-gray-900 dark:text-white text-center"}),(0,r.jsx)("button",{onClick:()=>j(e.id,f[e.id]?parseInt(f[e.id]):void 0),disabled:null!==y,className:"bg-green-600 text-white px-3 py-1 rounded-lg text-xs font-medium hover:bg-green-700 transition-colors disabled:opacity-50",children:y===e.id?"...":"Fill"})]}),(0,r.jsx)("button",{onClick:()=>N(e.id,"cold"),disabled:null!==y,className:"border border-gray-300 dark:border-gray-600 text-gray-600 dark:text-gray-400 px-3 py-1 rounded-lg text-xs font-medium hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors disabled:opacity-50",children:y==="empty-cold-".concat(e.id)?"...":"Empty Cold"}),(0,r.jsx)("button",{onClick:()=>N(e.id,"warm"),disabled:null!==y,className:"border border-gray-300 dark:border-gray-600 text-gray-600 dark:text-gray-400 px-3 py-1 rounded-lg text-xs font-medium hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors disabled:opacity-50",children:y==="empty-warm-".concat(e.id)?"...":"Empty Warm"})]})]},e.id)}),0===i.length&&(0,r.jsx)("p",{className:"text-sm text-gray-400 dark:text-gray-500 text-center py-4",children:"No active reps with queue targets"}),b&&(0,r.jsx)("p",{className:"text-xs text-center text-blue-600 dar
1k:text-blue-400 mt-2",children:b})]})})]})}let en=["Mon","Tue","Wed","Thu","Fri","Sat","Sun"],ed=["AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"];function ei(e){return 0===e?"12:00 AM":e<12?"".concat(e,":00 AM"):12===e?"12:00 PM":"".concat(e-12,":00 PM")}function eo(e){var t,a,l,n,i,c,x,g,m;let{campaignId:u,templateBody:y,tfnPool:p}=e,[b,h]=(0,s.useState)(!1),[f,k]=(0,s.useState)({}),[v,j]=(0,s.useState)([]),[N,w]=(0,s.useState)(!1),[C,S]=(0,s.useState)(!1),[T,D]=(0,s.useState)(null),[E,F]=(0,s.useState)([]),[R,L]=(0,s.useState)(!1),P=Array.isArray(p)&&p.length>0,I=(0,s.useCallback)(async()=>{w(!0);try{let t=await fetch("/api/campaigns/settings?campaignId=".concat(u));if(t.ok){var e;let a=await t.json();k(a.settings||{}),j((null===(e=a.settings)||void 0===e?void 0:e.templateVariants)||[])}}catch(e){console.error("Failed to fetch campaign settings:",e)}finally{w(!1)}},[u]);(0,s.useEffect)(()=>{b&&(I(),L(!0),fetch("/api/campaigns/".concat(u,"/variant-stats")).then(e=>e.ok?e.json():null).then(e=>{(null==e?void 0:e.variants)&&F(e.variants)}).catch(()=>{}).finally(()=>L(!1)))},[b,I,u]);let _=(e,t)=>k(a=>({...a,[e]:t})),O=e=>{let t=f.sendDays||[];_("sendDays",t.includes(e)?t.filter(t=>t!==e):[...t,e])},M=e=>{let t=f.stateBlacklist||[];_("stateBlacklist",t.includes(e)?t.filter(t=>t!==e):[...t,e])},Z=async()=>{S(!0),D(null);try{let e={sendWindowStart:f.sendWindowStart,sendWindowEnd:f.sendWindowEnd,sendDays:f.sendDays,dailyLimit:f.dailyLimit,messagesPerMinute:f.messagesPerMinute,maxTextsPerLead:f.maxTextsPerLead,quietHoursStart:f.quietHoursStart,quietHoursEnd:f.quietHoursEnd,bookingUrl:f.bookingUrl,repCallWaitHours:f.repCallWaitHours,stateBlacklist:f.stateBlacklist,templateVariants:v.filter(e=>e.trim().length>0)},t=await fetch("/api/campaigns/settings?campaignId=".concat(u),{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({overrides:e})});D(t.ok?"Saved":"Save failed"),setTimeout(()=>D(null),2e3)}catch(e){D("Network error")}finally{S(!1)}},z="border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none text-sm";return(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow",children:[(0,r.jsx)("button",{onClick:()=>h(!b),className:"w-full px-6 py-4 flex items-center justify-between text-left",children:(0,r.jsxs)("div",{className:"flex items-center gap-3",children:[b?(0,r.jsx)(d.Z,{size:16,className:"text-gray-400"}):(0,r.jsx)(o.Z,{size:16,className:"text-gray-400"}),(0,r.jsx)("h3",{className:"text-sm font-semibold text-gray-700 dark:text-gray-300",children:"Campaign Settings"})]})}),b&&(0,r.jsx)("div",{className:"px-6 pb-4 space-y-4",children:N?(0,r.jsx)("div",{className:"flex items-center justify-center py-6",children:(0,r.jsx)("div",{className:"animate-spin rounded-full h-5 w-5 border-b-2 border-blue-600"})}):(0,r.jsxs)(r.Fragment,{children:[y&&(0,r.jsxs)("div",{className:"border border-gray-100 dark:border-gray-700 rounded-lg p-4",children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white mb-2 block",children:"Cold Text Template"}),(0,r.jsx)("div",{className:"bg-gray-50 dark:bg-gray-900 rounded-lg p-3 text-xs font-mono text-gray-700 dark:text-gray-300 whitespace-pre-wrap leading-relaxed",children:y}),(0,r.jsx)("p",{className:"text-[10px] text-gray-400 mt-2",children:"Edit via the Edit button above."})]}),P?(0,r.jsx)(A,{campaignId:u}):(0,r.jsxs)("div",{className:"border border-gray-100 dark:border-gray-700 rounded-lg p-4",children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white mb-2 block",children:"Template Variants"}),(0,r.jsx)("p",{className:"text-xs text-gray-400 mb-3",children:"Each variant is a complete SMS template. One is picked at random per send. If empty, the campaign template above is used."}),(0,r.jsx)("div",{className:"flex flex-wrap gap-1.5 mb-3",children:["{companyName}","{city}","{state}","{industry}","{previewUrl}"].map(e=>(0,r.jsx)("button",{type:"button",onClick:()=>{let t=document.activeElement;if((null==t?void 0:t.tagName)==="TEXTAREA"&&void 0!==t.dataset.variantIdx){var a,r;let s=parseInt(t.dataset.variantIdx),l=null!==(a=t.selectionStart)&&void 0!==a?a:t.value.length,n=[...v];n[s]=n[s].slice(0,l)+e+n[s].slice(null!==(r=t.selectionEnd)&&void 0!==r?r:l),j(n),setTimeout(()=>{t.focus(),t.selectionStart=t.selectionEnd=l+e.length},0)}},className:"text-xs bg-blue-50 dark:bg-blue-900/30 text-blue-600 dar
1k:text-blue-400 hover:bg-blue-100 dark:hover:bg-blue-900/50 border border-blue-200 dark:border-blue-800 px-2 py-0.5 rounded",children:e},e))}),(0,r.jsx)("div",{className:"space-y-3",children:v.map((e,t)=>{let a=e.length,s=Math.ceil(a/160)||0,l=E.find(e=>e.variantIndex===t),n=E.filter(e=>null!==e.variantIndex&&e.delivered>0),d=n.length>0?n.reduce((e,t)=>e+parseFloat(t.clickRate||"0"),0)/n.length:0,i=n.length>0?n.reduce((e,t)=>e+parseFloat(t.replyRate||"0"),0)/n.length:0,o=n.length>0?n.reduce((e,t)=>e+parseFloat(t.optOutRate||"0"),0)/n.length:0;return(0,r.jsxs)("div",{children:[(0,r.jsx)("textarea",{rows:3,"data-variant-idx":t,value:e,onChange:e=>{let a=[...v];a[t]=e.target.value,j(a)},className:"border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 w-full bg-white dark:bg-gray-900 text-gray-900 dark:text-white font-mono text-sm",placeholder:"Hey, free preview for {companyName} in {city}: {previewUrl}"}),(0,r.jsxs)("div",{className:"flex items-center justify-between mt-1",children:[(0,r.jsxs)("span",{className:"text-xs text-gray-400",children:[a," chars \xb7 ",s," segment",1!==s?"s":""]}),(0,r.jsx)("button",{onClick:()=>j(v.filter((e,a)=>a!==t)),className:"text-red-400 hover:text-red-600 text-xs",children:"Remove"})]}),l&&l.sent>0&&(0,r.jsx)("div",{className:"mt-2 flex flex-wrap gap-x-4 gap-y-1 bg-gray-50 dark:bg-gray-900/50 rounded-lg px-3 py-2",children:[{label:"Sent",val:l.sent.toLocaleString()},{label:"Del.",val:l.delivered.toLocaleString()},{label:"Del. Rate",val:"".concat(l.deliveryRate,"%")},{label:"Clicked",val:l.clicked.toLocaleString()},{label:"Click Rate",val:"".concat(l.clickRate,"%"),color:parseFloat(l.clickRate)>=d?"text-emerald-600":"text-red-500"},{label:"Opt-Out",val:l.optedOut.toLocaleString()},{label:"Opt-Out %",val:"".concat(l.optOutRate,"%"),color:parseFloat(l.optOutRate)<=o?"text-emerald-600":"text-red-500"},{label:"Replied",val:l.replied.toLocaleString()},{label:"Reply %",val:"".concat(l.replyRate,"%"),color:parseFloat(l.replyRate)>=i?"text-emerald-600":"text-red-500"}].map(e=>(0,r.jsxs)("div",{className:"text-center min-w-[48px]",children:[(0,r.jsx)("p",{className:"text-xs font-semibold tabular-nums ".concat(e.color||"text-gray-900 dark:text-white"),children:e.val}),(0,r.jsx)("p",{className:"text-[9px] text-gray-400 leading-tight",children:e.label})]},e.label))}),R&&!l&&(0,r.jsx)("div",{className:"mt-1 text-[10px] text-gray-400 animate-pulse",children:"Loading stats..."})]},t)})}),(0,r.jsxs)("div",{className:"flex items-center justify-between mt-3",children:[(0,r.jsx)("button",{onClick:()=>j([...v,""]),className:"px-3 py-1.5 bg-blue-600 text-white rounded-lg text-xs font-medium hover:bg-blue-700",children:"Add Variant"}),(0,r.jsxs)("span",{className:"text-xs text-gray-400",children:[v.filter(e=>e.trim()).length," variant",1!==v.filter(e=>e.trim()).length?"s":""]})]})]}),(0,r.jsxs)("div",{className:"border border-gray-100 dark:border-gray-700 rounded-lg p-4",children:[(0,r.jsxs)("span",{className:"text-sm font-medium text-gray-900 dark:text-white mb-3 block",children:["Send Window ",(0,r.jsx)("span",{className:"text-xs font-normal text-gray-400",children:"EST"})]}),(0,r.jsxs)("div",{className:"grid grid-cols-2 gap-3 mb-3",children:[(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-xs text-gray-500 dark:text-gray-400 mb-1",children:"Start"}),(0,r.jsx)("select",{value:null!==(t=f.sendWindowStart)&&void 0!==t?t:9,onChange:e=>_("sendWindowStart",parseInt(e.target.value)),className:z,children:Array.from({length:24},(e,t)=>(0,r.jsx)("option",{value:t,children:ei(t)},t))})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-xs text-gray-500 dark:text-gray-400 mb-1",children:"End"}),(0,r.jsx)("select",{value:null!==(a=f.sendWindowEnd)&&void 0!==a?a:20,onChange:e=>_("sendWindowEnd",parseInt(e.target.value)),className:z,children:Array.from({length:24},(e,t)=>(0,r.jsx)("option",{value:t,children:ei(t)},t))})]})]}),(0,r.jsx)("div",{className:"flex flex-wrap gap-2",children:en.map(e=>(0,r.jsx)("button",{onClick:()=>O(e),className:"px-3 py-1.5 rounded-lg text-xs font-medium transition-colors border ".concat((f.sendDays||[]).includes(e)?"bg-blue-600 text-white border-blue-600":"bg-white dark:bg-gray-900 text-gray-700 dark:text-gray-300 border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700"),children:e},e))})]}),(0,r.jsxs)("div",{className:"border border-gray-100 dark:border-gray-700 rounded-lg p-4",children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white mb-3 block",children:"Daily Limit"}),(0,r.jsx)("input",{type:"number",min:1,value:null!==(l=f.dailyLimit)&&void 0!==l?l:200,onChange:e=>_("dailyLimit",parseInt(e.target.value)||0),className:z})]}),(0,r.jsxs)("div",{className:"border border-gray-100 dark:border-gray-700 rounded-lg p-4",children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white mb-3 block",children:"Messages Per Minute"}),(0,r.jsx)("input",{type:"number",min:1,value:null!==(n=f.messagesPerMinute)&&void 0!==n?n:10,onChange:e=>_("messagesPerMinute",parseInt(e.target.value)||0),className:z})]}),(0,r.jsxs)("div",{className:"border border-gray-100 dark:border-gray-700 rounded-lg p-4",children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white mb-3 block",children:"Max Texts Per Lead"}),(0,r.jsx)("input",{type:"number",min:1,value:null!==(i=f.maxTextsPerLead)&&void 0!==i?i:9,onCh
1ange:e=>_("maxTextsPerLead",parseInt(e.target.value)||0),className:z})]}),(0,r.jsxs)("div",{className:"border border-gray-100 dark:border-gray-700 rounded-lg p-4",children:[(0,r.jsxs)("span",{className:"text-sm font-medium text-gray-900 dark:text-white mb-3 block",children:["Quiet Hours ",(0,r.jsx)("span",{className:"text-xs font-normal text-gray-400",children:"EST"})]}),(0,r.jsxs)("div",{className:"grid grid-cols-2 gap-3",children:[(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-xs text-gray-500 dark:text-gray-400 mb-1",children:"Start"}),(0,r.jsx)("select",{value:null!==(c=f.quietHoursStart)&&void 0!==c?c:21,onChange:e=>_("quietHoursStart",parseInt(e.target.value)),className:z,children:Array.from({length:24},(e,t)=>(0,r.jsx)("option",{value:t,children:ei(t)},t))})]}),(0,r.jsxs)("div",{children:[(0,r.jsx)("label",{className:"block text-xs text-gray-500 dark:text-gray-400 mb-1",children:"End"}),(0,r.jsx)("select",{value:null!==(x=f.quietHoursEnd)&&void 0!==x?x:8,onChange:e=>_("quietHoursEnd",parseInt(e.target.value)),className:z,children:Array.from({length:24},(e,t)=>(0,r.jsx)("option",{value:t,children:ei(t)},t))})]})]})]}),(0,r.jsxs)("div",{className:"border border-gray-100 dark:border-gray-700 rounded-lg p-4",children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white mb-3 block",children:"Booking URL"}),(0,r.jsx)("input",{type:"text",value:null!==(g=f.bookingUrl)&&void 0!==g?g:"",onChange:e=>_("bookingUrl",e.target.value),placeholder:"https://cal.com/...",className:z})]}),(0,r.jsxs)("div",{className:"border border-gray-100 dark:border-gray-700 rounded-lg p-4",children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white mb-3 block",children:"Rep Call Wait Hours"}),(0,r.jsx)("p",{className:"text-xs text-gray-400 mb-2",children:"Hours to wait after cold text before rep calls the lead."}),(0,r.jsx)("input",{type:"number",min:0,value:null!==(m=f.repCallWaitHours)&&void 0!==m?m:2,onChange:e=>_("repCallWaitHours",parseInt(e.target.value)||0),className:z})]}),(0,r.jsxs)("div",{className:"border border-gray-100 dark:border-gray-700 rounded-lg p-4",children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-900 dark:text-white mb-3 block",children:"State Blacklist (global)"}),(0,r.jsx)("p",{className:"text-xs text-gray-400 mb-2",children:"Leads in selected states are skipped on every campaign. This edits the one global list — a campaign can never loosen it (SendGateUnion)."}),(0,r.jsx)("div",{className:"flex flex-wrap gap-1.5",children:ed.map(e=>(0,r.jsx)("button",{onClick:()=>M(e),className:"px-2 py-1 rounded text-[10px] font-semibold transition-colors border ".concat((f.stateBlacklist||[]).includes(e)?"bg-red-600 text-white border-red-600":"bg-white dark:bg-gray-900 text-gray-600 dark:text-gray-400 border-gray-200 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700"),children:e},e))})]}),(0,r.jsxs)("div",{className:"flex items-center justify-end gap-3 pt-2",children:[T&&(0,r.jsx)("span",{className:"text-xs ".concat("Saved"===T?"text-green-600 dark:text-green-400":"text-red-600 dark:text-red-400"),children:T}),(0,r.jsx)("button",{onClick:Z,disabled:C,className:"bg-blue-600 text-white px-5 py-2 rounded-lg text-sm font-medium hover:bg-blue-700 transition-colors disabled:opacity-50",children:C?"Saving...":"Save Settings"})]})]})})]})}function ec(e){let{label:t,value:a}=e;return(0,r.jsxs)("div",{className:"bg-white dark:bg-gray-800 rounded-lg shadow p-4 border border-gray-200 dark:border-gray-700",children:[(0,r.jsx)("p",{className:"text-xs text-gray-500 dark:text-gray-400 mb-1",children:t}),(0,r.jsx)("p",{className:"text-xl font-bold text-gray-900 dark:text-white",children:a})]})}function ex(e){let{lead:t,stageInfo:a,isExpanded:l,isSelected:i,onSelect:x,onToggle:g,reps:m,campaignId:u,onRepAssigned:y,onRemove:p,timeline:b,timelineLoading:h}=e,[f,k]=(0,s.useState)(!1);return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)("tr",{onClick:g,className:"border-b border-gray-200 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-900/30 cursor-pointer transition-colors",children:[(0,r.jsx)("td",{className:"px-4 py-3",onClick:e=>e.stopPropagation(),children:(0,r.jsx)("input",{type:"checkbox",checked:i,onChange:e=>x(e.target.checked),className:"w-4 h-4 cursor-pointer rounded border-gray-300 dark:border-slate-600"})}),(0,r.jsx)("td",{className:"px-6 py-3 text-sm text-gray-900 dark:text-white font-medium",children:(0,r.jsxs)("div",{className:"flex items-center gap-2",children:[l?(0,r.jsx)(d.Z,{size:14,className:"text-gray-400 flex-shrink-0"}):(0,r.jsx)(o.Z,{size:14,className:"text-gray-400 flex-shrink-0"}),t.companyName||"—"]})}),(0,r.jsx)("td",{className:"px-6 py-3 text-sm text-gray-700 dark:text-gray-300",children:[t.firstName,t.lastName].filter(
1Boolean).join(" ")||"—"}),(0,r.jsx)("td",{className:"px-6 py-3 text-sm text-gray-700 dark:text-gray-300 font-mono",children:t.phone||"—"}),(0,r.jsx)("td",{className:"px-6 py-3",children:t.phoneType?(0,r.jsx)("span",{className:"px-2 py-0.5 rounded-full text-xs font-medium ".concat({mobile:"bg-green-100 dark:bg-green-900/40 text-green-700 dark:text-green-300",landline:"bg-red-100 dark:bg-red-900/40 text-red-700 dark:text-red-300",voip:"bg-orange-100 dark:bg-orange-900/40 text-orange-700 dark:text-orange-300"}[t.phoneType]||"bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400"),children:"mobile"===t.phoneType?"Mobile":"landline"===t.phoneType?"Landline":"voip"===t.phoneType?"VoIP":t.phoneType}):(0,r.jsx)("span",{className:"text-gray-400",children:"—"})}),(0,r.jsxs)("td",{className:"px-6 py-3 text-sm text-gray-700 dark:text-gray-300",children:[[t.city,t.state].filter(Boolean).join(", ")||"—",t.timezone&&(0,r.jsxs)("span",{className:"text-xs text-gray-400 ml-1",children:["\xb7 ",(0,n.HG)(t.timezone)]})]}),(0,r.jsx)("td",{className:"px-6 py-3",children:t.pipelineStatus?(0,r.jsx)("span",{className:"px-2 py-1 rounded-full text-xs font-medium ".concat({NEW:"bg-gray-100 text-gray-600",COLD_SENT:"bg-blue-100 text-blue-700",WARM:"bg-amber-100 text-amber-700",CONTACTED:"bg-teal-100 text-teal-700",OPTED_IN:"bg-purple-100 text-purple-700",MEETING_BOOKED:"bg-emerald-100 text-emerald-700",CLOSED:"bg-green-100 text-green-800",COLD_NO_RESPONSE:"bg-gray-100 text-gray-500",NOT_INTERESTED:"bg-red-100 text-red-600",OPTED_OUT:"bg-red-100 text-red-700",EXHAUSTED:"bg-gray-200 text-gray-400"}[t.pipelineStatus]||"bg-gray-100 text-gray-600"),children:t.pipelineStatus.replace(/_/g," ")}):a?(0,r.jsx)("span",{className:"px-2 py-1 rounded-full text-xs font-medium ".concat(a.bgColor," ").concat(a.textColor),children:a.label}):(0,r.jsx)("span",{className:"px-2 py-1 rounded-full text-xs font-medium bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400",children:t.stage||"—"})}),(0,r.jsx)("td",{className:"px-6 py-3 text-sm text-gray-500 dark:text-gray-400",children:t.lastActivityAt?(0,n.o0)(t.lastActivityAt):"—"}),(0,r.jsxs)("td",{className:"px-6 py-3 text-sm",onClick:e=>e.stopPropagation(),children:[(0,r.jsx)("button",{onClick:()=>k(!0),className:"text-sm hover:underline ".concat(t.repName?"text-gray-700 dark:text-gray-300":"text-gray-400 dark:text-gray-500 italic"),children:t.repName||"Assign"}),f&&(0,r.jsx)(es,{campaignId:u,leadIds:[t.leadId],reps:m,title:"Assign Rep — ".concat(t.companyName||t.firstName||"Lead"),onClose:()=>k(!1),onAssigned:()=>{k(!1),y()}})]}),(0,r.jsx)("td",{className:"px-6 py-3 text-center",onClick:e=>e.stopPropagation(),children:p&&["QUEUED","TEXTED","CLICKED","REP_CALLED","OPTED_IN","DRIP_ACTIVE","HOT","ARCHIVED"].includes(t.stage)&&(0,r.jsx)("button",{onClick:()=>{window.confirm("Remove ".concat(t.companyName||t.firstName," from this campaign?"))&&p()},title:"Remove from campaign",className:"p-1 rounded text-red-500 hover:bg-red-50 dark:hover:bg-red-900/20 transition-colors",children:(0,r.jsx)(c.Z,{size:14})})})]}),l&&(0,r.jsx)("tr",{className:"bg-gray-50 dark:bg-gray-900/40",children:(0,r.jsx)("td",{colSpan:10,className:"px-6 py-4",children:(0,r.jsxs)("div",{className:"ml-6",children:[(0,r.jsx)("h4",{className:"text-xs font-semibold text-gray-500 dark:text-gray-400 uppercase tracking-wider mb-3",children:"Lead Timeline"}),h?(0,r.jsxs)("div",{className:"flex items-center gap-2 text-sm text-gray-400",children:[(0,r.jsx)("div",{className:"animate-spin rounded-full h-4 w-4 border-b-2 border-blue-600"}),"Loading timeline..."]}):0===b.length?(0,r.jsx)("p",{className:"text-sm text-gray-400 dark:text-gray-500",children:"No events recorded yet"}):(0,r.jsx)("div",{className:"space-y-3",children:b.map((e,t)=>(0,r.jsxs)("div",{className:"flex items-start gap-3",children:[(0,r.jsx)("span",{className:"text-lg flex-shrink-0 mt-0.5",children:M[e.type]||"\uD83D\uDCCB"}),(0,r.jsxs)("div",{className:"flex-1 min-w-0",children:[(0,r.jsxs)("div",{className:"flex items-center gap-2",children:[(0,r.jsx)("span",{className:"text-sm font-medium text-gray-800 dark:text-gray-200",children:e.title}),(0,r.jsx)("span",{className:"text-xs text-gray-400 dark:text-gray-500",children:(0,n.o0)(e.createdAt)})]}),e.description&&(0,r.jsx)("p",{className:"text-xs text-gray-500 dark:text-gray-400 mt-0.5",children:e.description})]})]},e.id||t))})]})})})]})}},80225:function(e,t,a){a.d(t,{D:function(){return d}});var r=a(57437),s=a(2265),l=a(54887),n=a(40875);function d(e){let{pageItemIds:t,allItemIds:a,selectedIds:d,onSelectionChange:i,onLazyLoadAll:o,isLazyLoading:c}
1=e,[x,g]=(0,s.useState)(!1),[m,u]=(0,s.useState)("25"),y=(0,s.useRef)(null),p=(0,s.useRef)(null),b=(0,s.useRef)(null),[h,f]=(0,s.useState)(null),k=t.length>0&&t.every(e=>d.has(e)),v=t.some(e=>d.has(e));(0,s.useEffect)(()=>{y.current&&(y.current.indeterminate=v&&!k)},[v,k]);let j=(0,s.useCallback)(()=>{if(p.current){let e=p.current.getBoundingClientRect();f({top:e.bottom+4,left:e.left})}},[]);(0,s.useEffect)(()=>{if(!x)return;j();let e=()=>j();return window.addEventListener("scroll",e,!0),()=>window.removeEventListener("scroll",e,!0)},[x,j]),(0,s.useEffect)(()=>{if(!x)return;let e=e=>{b.current&&!b.current.contains(e.target)&&p.current&&!p.current.contains(e.target)&&g(!1)};return document.addEventListener("mousedown",e),()=>document.removeEventListener("mousedown",e)},[x]),(0,s.useEffect)(()=>{if(!x)return;let e=e=>{"Escape"===e.key&&g(!1)};return document.addEventListener("keydown",e),()=>document.removeEventListener("keydown",e)},[x]);let N=()=>{let e=a&&a.length>0?a:t,r=Math.max(1,Math.min(e.length,parseInt(m)||0)),s=new Set(d);e.slice(0,r).forEach(e=>s.add(e)),i(s),g(!1)},w=async()=>{if((!a||0===a.length)&&o){try{await o()}finally{g(!1)}return}if(!a)return;let e=new Set(d);a.forEach(t=>e.add(t)),i(e),g(!1)};return(0,r.jsxs)("div",{className:"flex items-center gap-0.5",children:[(0,r.jsx)("input",{ref:y,type:"checkbox",checked:k,onChange:()=>{if(k)i(new Set);else{let e=new Set(d);t.forEach(t=>e.add(t)),i(e)}},className:"w-4 h-4 cursor-pointer rounded border-gray-300 dark:border-slate-600"}),(0,r.jsx)("button",{ref:p,onClick:()=>g(!x),className:"p-0.5 rounded hover:bg-gray-200 dark:hover:bg-slate-700 transition-colors","aria-label":"Selection options",children:(0,r.jsx)(n.Z,{size:12,className:"text-gray-400 dark:text-gray-500"})}),x&&h&&(0,l.createPortal)((0,r.jsxs)("div",{ref:b,className:"bg-white dark:bg-slate-900 border border-gray-200 dark:border-slate-700 rounded-lg shadow-lg py-1 z-[9999] w-56",style:{position:"fixed",top:h.top,left:h.left},children:[(0,r.jsx)("div",{className:"px-3 py-2 hover:bg-gray-50 dark:hover:bg-slate-800",children:(0,r.jsxs)("div",{className:"flex items-center gap-2",children:[(0,r.jsx)("span",{className:"text-sm text-gray-700 dark:text-gray-300",children:"Select"}),(0,r.jsx)("input",{type:"number",value:m,onChange:e=>u(e.target.value),onKeyDown:e=>{"Enter"===e.key&&N()},min:1,max:(null==a?void 0:a.length)||t.length,className:"w-16 border border-gray-300 dark:border-slate-600 rounded px-2 py-1 text-sm dark:bg-slate-800 dark:text-gray-100"}),(0,r.jsx)("button",{onClick:N,className:"bg-blue-600 text-white rounded px-3 py-1 text-sm hover:bg-blue-700 transition-colors",children:"Apply"})]})}),(0,r.jsxs)("button",{onClick:()=>{let e=new Set(d);t.forEach(t=>e.add(t)),i(e),g(!1)},className:"w-full text-left px-3 py-2 text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-slate-800 transition-colors",children:["Select this page [",t.length,"]"]}),(a&&a.length>t.length||o&&(!a||0===a.length))&&(0,r.jsx)("button",{onClick:w,disabled:c,className:"w-full text-left px-3 py-2 text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-slate-800 transition-colors disabled:opacity-50 disabled:cursor-wait",children:c?"Loading…":a&&a.length>0?"Select all in list [".concat(a.length,"]"):"Select all in list"}),d.size>0&&(0,r.jsx)("button",{onClick:()=>{i(new Set),g(!1)},className:"w-full text-left px-3 py-2 text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-slate-800 transition-colors border-t border-gray-100 dark:border-slate-800",children:"Deselect all"})]}),document.body)]})}},48311:function(e,t,a){function r(e){if(!e)return"—";let t=new Date(e);return isNaN(t.getTime())?"—":t.toLocaleDateString("en-US",{timeZone:"America/New_York",month:"short",day:"numeric",year:"numeric"})}function s(e){if(!e)return"—";let t=new Date(e);return isNaN(t.getTime())?"—":t.toLocaleString("en-US",{timeZone:"America/New_York",month:"short",day:"numeric",hour:"numeric",minute:"2-digit",hour12:!0,timeZoneName:"short"})}function l(e){if(!e)return"—";let t=new Date(e);return isNaN(t.getTime())?"—":t.toLocaleTimeString("en-US",{timeZone:"America/Ne
1w_York",hour:"numeric",minute:"2-digit",hour12:!0,timeZoneName:"short"})}function n(e){return e&&({"America/New_York":"EST","America/Detroit":"EST","America/Indiana/Indianapolis":"EST","America/Kentucky/Louisville":"EST","America/Chicago":"CST (-1hr)","America/Denver":"MST (-2hr)","America/Phoenix":"MST (-2hr)","America/Los_Angeles":"PST (-3hr)","America/Anchorage":"AKST (-4hr)","Pacific/Honolulu":"HST (-5hr)"})[e]||"EST"}function d(e,t){if(!e)return"—";let a=new Date(e);if(isNaN(a.getTime()))return"—";let r=a.toLocaleTimeString("en-US",{timeZone:"America/New_York",hour:"numeric",minute:"2-digit",hour12:!0,timeZoneName:"short"}),s=new Set(["America/New_York","America/Detroit","America/Indiana/Indianapolis","America/Kentucky/Louisville"]);if(!t||s.has(t))return r;let l=function(e,t){if(!e)return"—";let a=new Date(e);if(isNaN(a.getTime()))return"—";let r=t||"America/New_York",s=a.toLocaleTimeString("en-US",{timeZone:r,hour:"numeric",minute:"2-digit",hour12:!0}),l=n(r);return"".concat(s," ").concat(l)}(a,t);return"".concat(r," / ").concat(l," for lead")}a.d(t,{HG:function(){return n},mr:function(){return l},o0:function(){return s},p6:function(){return r},wz:function(){return d}})}}]);

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.