PageSourceSearch

https://www.readysethome.ai/assets/AdminDashboardDesktop-CN4UtoAX.js

js readysethome.ai collected 2026-10-05 15:02:48 UTC 104,870 bytes, 8 lines download raw bytes

1import{j as e}from"./ui-vendor-CwKPOJE8.js";import{r as l,u as Ps}from"./react-vendor-BoB-7tOT.js";import{a3 as Ms,s as S,C as m,c as p,B as $,cp as Es,R as pe,G as q,c9 as qe,O as be,b9 as Me,bG as ts,bi as Ce,U as te,bV as Fs,aS as as,a as J,b as ee,h as de,r as gs,ap as Ds,aP as ls,N as _e,ab as Se,V as Ae,J as ns,W as Ge,u as Ie,bB as Us,i as Ee,b0 as Fe,bZ as Ts,k as js,L as ie,m as Ze,n as Xe,o as Je,p as es,q as le,w as fs,ea as He,aO as We,Q as fe,H as Is,ag as bs,ai as Ns,aj as vs,ak as ys,aq as ws,z as $s,P as is,al as Bs,bK as _s,bn as Ke,M as Ss,af as cs,au as Os,I as Ye,bA as Be,bW as Vs,v as zs,dK as qs,S as Gs,e as os,bN as ds,a7 as Hs,dO as Ws,eb as Ks,dP as Ys,b6 as Qs,cs as Zs}from"./index-BdZNXa7t.js";import{D as Xs,a as Js,b as et,c as Oe,e as st}from"./dropdown-menu-C8QcDzhn.js";import{U as tt,a as at,B as nt,A as rt,K as lt,E as it,S as ct,b as ot,N as dt}from"./NewSignupsPanel-CSSGSgeg.js";import{T as ks,a as Cs,b as ke,c as Qe}from"./tabs-o26mfh8e.js";import{A as Le,b as Ve,a as Pe}from"./alert-JvnEJBXK.js";import{T as De,a as Ue,b as oe,c as U,d as Te,e as E}from"./table-1YKcyRfB.js";import{S as Rs}from"./switch-Cl6oB8p-.js";import{D as rs}from"./database-BD2gYUSz.js";import{Z as we}from"./zap-DPOlaoou.js";import{R as mt}from"./receipt-Db6wootE.js";import{C as ms}from"./chart-pie-D_6OZgAH.js";import{C as xt}from"./chart-column-DBa0o5EB.js";import{U as xs}from"./user-minus-_RakIQYK.js";import{f as hs,r as ht,d as ut,a as pt}from"./promotionComplianceService-LId_yB1m.js";import{B as us}from"./ban-ByUcz59E.js";import{S as gt}from"./separator-CD3HR37Y.js";import{geocodeAllBrokers as jt}from"./geocodingService-C95TIWO1.js";import{S as ft}from"./square-pen-CepLhSEn.js";import{R as bt}from"./rocket-BtIj_6eO.js";import{B as Nt}from"./book-open-BNXmAZRO.js";import{F as vt}from"./file-check-Dn9WgavX.js";import{A as yt}from"./activity-CRdqwxpe.js";import{S as wt}from"./share-2-bJzwOlIy.js";import{C as _t}from"./chevron-left-DCP3e1mp.js";import{C as St}from"./circle-user-CCPLzejF.js";import"./supabase-QBQ6M2WM.js";import"./utils-BomB368b.js";import"./charts-DdJgN9kB.js";import"./star-Bmo8ee07.js";import"./building-BZ7FVKsW.js";import"./heart-B5JtK1Gn.js";import"./subscriptionSyncService-BVCQy6al.js";import"./link-2-DeTFF72A.js";import"./progress-CEMEtJgA.js";import"./newspaper-zyzYlws4.js";import"./aiKnowledgeService-DJWd7qCi.js";import"./index-BVFjzQKh.js";import"./xlsx-D_0l8YDs.js";import"./brain-C_stFd71.js";import"./square-xwKYNCIm.js";/**
2 * @license lucide-react v0.462.0 - ISC
3 *
4 * This source code is licensed under the ISC license.
5 * See the LICENSE file in the root directory of this source tree.
6 */const ps=Ms("TestTube",[["path",{d:"M14.5 2v17.5c0 1.4-1.1 2.5-2.5 2.5c-1.4 0-2.5-1.1-2.5-2.5V2",key:"125lnx"}],["path",{d:"M8.5 2h7",key:"csnxdl"}],["path",{d:"M14.5 16h-5",key:"1ox875"}]]),ze={"one-time":{monthly:14.99,annual:14.99},"mortgage-monthly":{monthly:29.99,annual:0},"mortgage-annual":{monthly:0,annual:251},monthly:{monthly:29.99,annual:0},annual:{monthly:0,annual:251},"property-starter":{monthly:49,annual:0},"property-starter-monthly":{monthly:49,annual:0},"property-starter-annual":{monthly:0,annual:411},"property-pro":{monthly:79,annual:0},"property-pro-monthly":{monthly:79,annual:0},"property-pro-annual":{monthly:0,annual:663},"property-ultimate":{monthly:99,annual:0},"property-ultimate-monthly":{monthly:99,annual:0},"property-ultimate-annual":{monthly:0,annual:831},pro:{monthly:59,annual:0},premium:{monthly:99,annual:0},basic:{monthly:29,annual:0}},kt=()=>{const[t,c]=l.useState(!0),[j,g]=l.useState(!1),[w,N]=l.useState("overview"),[n,C]=l.useState({totalPaidUsers:0,totalFreeUsers:0,monthlyRecurringRevenue:0,annualRecurringRevenue:0,planBreakdown:[],recentActivity:[],verifiedPaidUsers:0,unverifiedPaidUsers:0,verifiedMRR:0,betaTesterCount:0}),[x,P]=l.useState([]),[r,i]=l.useState([]),[v,k]=l.useState(!1),[T,d]=l.useState(!1),[h,A]=l.useState("all"),[u,y]=l.useState(!0),[R,se]=l.useState(null),[b,z]=l.useState(!1),[G,F]=l.useState(null),o=async()=>{z(!0),F(null);try{const{data:s}=await S.auth.getSession();if(!s.session)throw new Error("Not authenticated");const{data:M,error:B}=await S.functions.invoke("admin-stripe-revenue",{headers:{Authorization:`Bearer ${s.session.access_token}`}});if(B)throw new Error(B.message||"Failed to fetch Stripe data");if(!M.success)throw new Error(M.error||"Failed to fetch Stripe data");se(M.data)}catch(s){console.error("Error loading Stripe data:",s),F(s.message||"Failed to load Stripe data"),y(!1)}finally{z(!1)}};l.useEffect(()=>{o(),K(),Y(),H()},[]);const K=async()=>{c(!0);try{const{data:s,error:M}=await S.rpc("admin_get_all_users");if(M){console.error("Error loading user profiles:",M);return}let B=[];try{const{data:W,error:O}=await S.from("client_profiles").select("user_id, subscription_status");!O&&W&&(B=W)}catch{}let D=0;try{const{count:W,error:O}
6=await S.from("beta_testers").select("*",{count:"exact",head:!0});!O&&W&&(D=W)}catch{}const I={};let ne=0,Ne=0,me=0,ve=0,xe=0,he=0,ss=0;(s||[]).forEach(W=>{const O=W.subscription_plan||"free",ce=W.subscription_status,ye=W.stripe_customer_id&&W.stripe_customer_id.startsWith("cus_");if(O&&O!=="free"&&O!=="none"&&(ce==="active"||ce==="trial"||ce==="trialing")){ne++,I[O]=(I[O]||0)+1,ye?xe++:he++;const X=ze[O];X&&(X.monthly>0&&(me+=X.monthly,ye&&(ss+=X.monthly)),X.annual>0&&(ve+=X.annual,me+=X.annual/12,ye&&(ss+=X.annual/12)))}else Ne++}),(B||[]).forEach(W=>{const O=W.subscription_plan,ce=W.subscription_status;if(O&&O!=="free"&&O!=="none"&&(ce==="active"||ce==="trial"||ce==="trialing")){const $e=(s||[]).find(X=>X.user_id===W.user_id);if(!$e||!$e.subscription_plan||$e.subscription_plan==="free"){I[O]=(I[O]||0)+1,he++;const X=ze[O];X&&(X.monthly>0&&(me+=X.monthly),X.annual>0&&(ve+=X.annual,me+=X.annual/12))}}});const As=Object.entries(I).map(([W,O])=>{const ce=ze[W]||{monthly:0,annual:0},ye=ce.monthly>0?ce.monthly*O:ce.annual/12*O;return{plan:W,count:O,monthlyRevenue:ye}}).sort((W,O)=>O.monthlyRevenue-W.monthlyRevenue);C({totalPaidUsers:ne,totalFreeUsers:Ne,monthlyRecurringRevenue:me,annualRecurringRevenue:ve+me*12,planBreakdown:As,recentActivity:[],verifiedPaidUsers:xe,unverifiedPaidUsers:he,verifiedMRR:ss,betaTesterCount:D||0})}catch(s){console.error("Error loading revenue data:",s)}finally{c(!1)}},Y=async()=>{k(!0);try{const{data:s,error:M}=await S.from("subscription_payments").select("*").order("created_at",{ascending:!1}).limit(100);if(M){const{data:B}=await S.rpc("admin_get_all_users"),D=(B||[]).filter(I=>I.subscription_plan&&I.subscription_plan!=="free"&&I.subscription_plan!=="none").map(I=>{const ne=ze[I.subscription_plan];return{id:I.id||I.user_id,user_id:I.user_id,email:I.email,full_name:I.full_name||"Unknown",amount:(ne==null?void 0:ne.monthly)||(ne==null?void 0:ne.annual)||0,plan:I.subscription_plan,status:I.subscription_status==="active"?"succeeded":"pending",payment_date:I.subscription_changed_at||I.updated_at||I.created_at,stripe_payment_id:I.stripe_customer_id}});P(D)}else P(s||[])}catch(s){console.error("Error loading payment history:",s)}finally{k(!1)}},H=async()=>{d(!0);try{const{data:s}=await S.rpc("admin_get_all_users"),M=new Date;M.setDate(M.getDate()-30);const B=(s||[]).map(D=>{const ne=new Date(D.created_at)>M,Ne=D.subscription_plan&&D.subscription_plan!=="free"&&D.subscription_plan!=="none",me=D.subscription_status==="active",ve=D.subscription_status==="canceled"||D.subscription_status==="cancelled",xe=D.subscription_status==="trial"||D.subscription_status==="trialing";let he="non-paying";return ve?he="canceled":xe?he="trial":ne&&!Ne?he="new":Ne&&me?he="active":he="non-paying",{id:D.id||D.user_id,user_id:D.user_id,email:D.email,full_name:D.full_name||"Unknown",user_type:D.user_type||"unknown",subscription_plan:D.subscription_plan||"free",subscription_status:D.subscription_status||"none",created_at:D.created_at,updated_at:D.updated_at||D.created_at,status_category:he}});i(B)}catch(s){console.error("Error loading user status data:",s)}finally{d(!1)}},L=async()=>{g(!0),await Promise.all([o(),K(),Y(),H()]),g(!1)},V=(s,M)=>{if(s.length===0)return;const B=Object.keys(s[0]),D=[B.join(","),...s.map(Ne=>B.map(me=>{const ve=Ne[me],xe=String(ve||"");return xe.includes(",")||xe.includes('"')||xe.includes(`
7`)?`"${xe.replace(/"/g,'""')}"`:xe}).join(","))].join(`
8`),I=new Blob([D],{type:"text/csv;charset=utf-8;"}),ne=document.createElement("a");ne.href=URL.createObjectURL(I),ne.download=`${M}_${new Date().toISOString().split("T")[0]}.csv`,ne.click()},f=()=>{const s=x.map(M=>({Email:M.email,Name:M.full_name,Plan:ge(M.plan),Amount:a(M.amount),Status:M.status,Date:new Date(M.payment_date).toLocaleDateString(),"Stripe ID":M.stripe_payment_id||"N/A"}));V(s,"payment_history")},Z=()=>{const M=(h==="all"?r:r.filter(B=>B.status_category===h)).map(B=>({Email:B.email,Name:B.full_name,"User Type":B.user_type,Plan:ge(B.subscription_plan),Status:B.subscription_status,Category:B.status_category,Created:new Date(B.created_at).toLocaleDateString(),Updated:new Date(B.updated_at).toLocaleDateString()}));V(M,`user_status_${h}`)},ae=()=>{const s=[{Metric:"Monthly Recurring Revenue (MRR)",Value:a(n.monthlyRecurringRevenue)},{Metric:"Annual Recurring Revenue (ARR)",Value:a(n.annualRecurringRevenue)},{Metric:"Total Paid Users",Value:n.totalPaidUsers},{Metric:"Total Free Users",Value:n.totalFreeUsers},{Metric:"Conversion Rate",Value:`${(n.totalPaidUsers/(n.totalPaidUsers+n.totalFreeUsers)*100).toFixed(1)}%`},{Metric:"",Value:""},{Metric:"Plan Breakdown",Value:""},...n.planBreakdown.map(M=>({Metric:ge(M.plan),Value:`${M.count} users - ${a(M.monthlyRevenue)}/mo`}))];V(s,"revenue_report")},_=h==="all"?r:r.filter(s=>s.status_category===h),Q={all:r.length,new:r.filter(s=>s.status_category==="new").length,active:r.filter(s=>s.status_category==="active").length,canceled:r.filter(s=>s.status_category==="canceled").length,"non-paying":r.filter(s=>s.status_category==="non-paying").length,trial:r.filter(s=>s.status_category==="trial").length},a=s=>new Intl.NumberFormat("en-US",{style:"currency",currency:"USD",minimumFractionDigits:2}).format(s),ge=s=>({"one-time":"One-Time Analysis","mortgage-monthly":"Mortgage Monthly","mortgage-annual":"Mortgage Annual",monthly:"Mortgage Monthly",annual:"Mortgage Annual","property-starter":"Property Starter","property-starter-monthly":"Property Starter Mo","property-starter-
8annual":"Property Starter Yr","property-pro":"Property Pro","property-pro-monthly":"Property Pro Mo","property-pro-annual":"Property Pro Yr","property-ultimate":"Property Ultimate","property-ultimate-monthly":"Property Ultimate Mo","property-ultimate-annual":"Property Ultimate Yr",pro:"Pro (Professional)",premium:"Premium (Professional)",basic:"Basic (Professional)"})[s]||s,re=s=>s.includes("ultimate")||s.includes("pro")?"bg-blue-100 text-blue-700":s.includes("starter")?"bg-indigo-100 text-indigo-700":s.includes("mortgage")||s==="monthly"||s==="annual"?"bg-green-100 text-green-700":s==="one-time"?"bg-cyan-100 text-cyan-700":s==="premium"?"bg-blue-100 text-blue-700":"bg-stone-100 text-stone-700";return t?e.jsx(m,{children:e.jsx(p,{className:"flex items-center justify-center h-64",children:e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"animate-spin rounded-full h-12 w-12 border-b-2 border-green-600 mx-auto mb-4"}),e.jsx("p",{className:"text-stone-500",children:"Loading revenue analytics..."})]})})}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex flex-col md:flex-row md:items-center justify-between gap-4",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-2xl font-bold text-stone-900",children:"Revenue Analytics"}),e.jsx("p",{className:"text-stone-500",children:"Super Admin Only - Financial Overview"})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-4",children:[e.jsxs("div",{className:"flex items-center gap-3 bg-white border rounded-lg px-4 py-2",children:[e.jsxs("div",{className:`flex items-center gap-2 ${u?"text-stone-400":"text-stone-900 font-medium"}`,children:[e.jsx(rs,{className:"w-4 h-4"}),e.jsx("span",{className:"text-sm",children:"Database"})]}),e.jsx(Rs,{checked:u,onCheckedChange:y,disabled:b||!R}),e.jsxs("div",{className:`flex items-center gap-2 ${u?"text-green-700 font-medium":"text-stone-400"}`,children:[e.jsx(we,{className:"w-4 h-4"}),e.jsx("span",{className:"text-sm",children:"Stripe Live"})]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs($,{variant:"outline",onClick:ae,size:"sm",children:[e.jsx(Es,{className:"w-4 h-4 mr-2"}),"Export"]}),e.jsxs($,{variant:"outline",onClick:L,disabled:j||b,children:[e.jsx(pe,{className:`w-4 h-4 mr-2 ${j||b?"animate-spin":""}`}),"Refresh"]})]})]})]}),u&&R?e.jsxs(Le,{className:"border-2 border-green-400 bg-green-50",children:[e.jsx(we,{className:"h-5 w-5 text-green-600"}),e.jsxs(Ve,{className:"text-green-800 font-bold flex items-center gap-2",children:["Connected to Stripe API",e.jsx(q,{className:"bg-green-600",children:"LIVE DATA"})]}),e.jsxs(Pe,{className:"text-green-700",children:[e.jsxs("p",{children:["Showing real-time payment data from your Stripe account. Last updated: ",new Date(R.lastUpdated).toLocaleString()]}),e.jsxs("a",{href:"https://dashboard.stripe.com",target:"_blank",rel:"noopener noreferrer",className:"inline-flex items-center gap-1 text-green-800 hover:text-green-900 font-medium mt-2",children:[e.jsx(qe,{className:"w-3.5 h-3.5"}),"Open Stripe Dashboard"]})]})]}):G?e.jsxs(Le,{variant:"destructive",className:"border-2 border-red-300 bg-red-50",children:[e.jsx(be,{className:"h-5 w-5 text-red-600"}),e.jsx(Ve,{className:"text-red-800 font-bold",children:"Stripe Connection Failed"}),e.jsxs(Pe,{className:"text-red-700",children:[e.jsx("p",{children:G}),e.jsx("p",{className:"mt-1",children:"Showing database data instead (includes beta testers)."})]})]}):b?e.jsxs(Le,{className:"border-2 border-blue-300 bg-blue-50",children:[e.jsx(pe,{className:"h-5 w-5 text-blue-600 animate-spin"}),e.jsx(Ve,{className:"text-blue-800 font-bold",children:"Connecting to Stripe..."}),e.jsx(Pe,{className:"text-blue-700",children:"Fetching live payment data from Stripe API."})]}):e.jsxs(Le,{className:"border-2 border-orange-400 bg-orange-50",children:[e.jsx(rs,{className:"h-5 w-5 text-orange-600"}),e.jsxs(Ve,{className:"text-orange-800 font-bold flex items-center gap-2",children:["Database View",e.jsx(q,{className:"bg-orange-500",children:"INCLUDES BETA TESTERS"})]}),e.jsxs(Pe,{className:"text-orange-700",children:[e.jsx("p",{children:"Showing database subscription records. Many users are beta tester
8s with manually-set subscriptions."}),e.jsxs("div",{className:"flex flex-wrap gap-4 mt-2",children:[e.jsxs("span",{children:[e.jsx("strong",{children:"Verified Stripe:"})," ",n.verifiedPaidUsers]}),e.jsxs("span",{children:[e.jsx("strong",{children:"Beta/Test:"})," ",n.unverifiedPaidUsers]})]})]})]}),u&&R&&e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-4",children:[e.jsx(m,{className:"bg-gradient-to-br from-green-100 to-emerald-100 border-2 border-green-400",children:e.jsx(p,{className:"p-6",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(we,{className:"w-4 h-4 text-green-600"}),e.jsx("p",{className:"text-sm font-bold text-green-700",children:"LIVE MRR"})]}),e.jsx("p",{className:"text-3xl font-bold text-green-800",children:a(R.mrr)}),e.jsx("p",{className:"text-xs text-green-600 mt-1",children:"From Stripe API"})]}),e.jsx("div",{className:"h-12 w-12 rounded-full bg-green-200 flex items-center justify-center",children:e.jsx(Me,{className:"h-6 w-6 text-green-700"})})]})})}),e.jsx(m,{className:"bg-gradient-to-br from-green-100 to-emerald-100 border-2 border-green-400",children:e.jsx(p,{className:"p-6",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(we,{className:"w-4 h-4 text-green-600"}),e.jsx("p",{className:"text-sm font-bold text-green-700",children:"LIVE ARR"})]}),e.jsx("p",{className:"text-3xl font-bold text-green-800",children:a(R.arr)}),e.jsx("p",{className:"text-xs text-green-600 mt-1",children:"Projected annual"})]}),e.jsx("div",{className:"h-12 w-12 rounded-full bg-green-200 flex items-center justify-center",children:e.jsx(ts,{className:"h-6 w-6 text-green-700"})})]})})}),e.jsx(m,{className:"bg-gradient-to-br from-blue-100 to-indigo-100 border-2 border-blue-400",children:e.jsx(p,{className:"p-6",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(mt,{className:"w-4 h-4 text-blue-600"}),e.jsx("p",{className:"text-sm font-bold text-blue-700",children:"Last 30 Days"})]}),e.jsx("p",{className:"text-3xl font-bold text-blue-800",children:a(R.totalRevenue30Days)}),e.jsxs("p",{className:"text-xs text-blue-600 mt-1",children:[R.recentPayments.length," payments"]})]}),e.jsx("div",{className:"h-12 w-12 rounded-full bg-blue-200 flex items-center justify-center",children:e.jsx(Ce,{className:"h-6 w-6 text-blue-700"})})]})})}),e.jsx(m,{className:"bg-gradient-to-br from-blue-100 to-blue-200 border-2 border-blue-400",children:e.jsx(p,{className:"p-6",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(te,{className:"w-4 h-4 text-blue-600"}),e.jsx("p",{className:"text-sm font-bold text-blue-700",children:"Active Subscriptions"})]}),e.jsx("p",{className:"text-3xl font-bold text-blue-800",children:R.activeSubscriptions}),e.jsxs("p",{className:"text-xs text-blue-600 mt-1",children:[R.totalCustomers," total customers"]})]}),e.jsx("div",{className:"h-12 w-12 rounded-full bg-blue-200 flex items-center justify-center",children:e.jsx(Fs,{className:"h-6 w-6 text-blue-700"})})]})})})]}),(!u||!R)&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:[e.jsx(m,{className:"bg-gradient-to-br from-green-100 to-emerald-100 border-2 border-green-400",children:e.jsx(p,{className:"p-6",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(as,{className:"w-4 h-4 text-green-600"}),e.jsx("p",{className:"text-sm font-bold text-green-700",children:"VERIFIED MRR"})]}),e.jsx("p",{className:"text-3xl font-bold text-green-800",children:a(n.verifiedMRR)}),e.jsxs("p",{className:"text-xs text-green-600 mt-1",children:["From ",n.verifiedPaidUsers," with Stripe ID"]})]}),e.jsx("div",{className:"h-12 w-12 rounded-full bg-green-200 flex items-center justify-center",children:e.jsx(Me,{className:"h-6 w-6 text-green-700"})})]})})}),e.jsx(m,{className:"bg-gradient-to-br from-green-100 to-emerald-100 border-2 border-green-400",children:e.jsx(p,{className:"p-6",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(as,{className:"w-4 h-4 text-green-600"}),e.jsx("p",{className:"text-sm font-bold text-green-700",children:"VERIFIED ARR"})]}),e.jsx("p",{className:"text-3xl font-bold text-green-800",children:a(n.verifiedMRR*12)}),e.jsx("p",{className:"text-xs text-green-600 mt-1",children:"Projected annual"})]}),e.jsx("div",{className:"h-12 w-12 rounded-full bg-green-200 flex items-center justify-center",children:e.jsx(ts,{className:"h-6 w-6 text-green-700"})})]})})}),e.jsx(m,{className:"bg-gradient-to-br from-green-100 to-emerald-100 border-2 border-green-400",children:e.jsx(p,{className:"p-6",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(as,{className:"w-4 h-4 text-green-600"}),e.jsx("p",{className:"text-sm font-bold text-green-700",children:"VERIFIED Customers"})]}),e.jsx("p",{className:"text-3xl font-bold text-green-800",children:n.verifiedPaidUsers}),e.jsx("p",{className:"text-xs text-green-600 mt-1",children:"With valid Stripe ID"})]}),e.jsx("div",{className:"h-12 w-12 rounded-full bg-green-200 flex items-center justify-center",children:e.jsx(Ce,{className:"h-6 w-6 text-green-700"})})]})})})]}),e.jsxs("div",{className:"opacity-60",children:[e.jsxs("p",{className:"text-sm text-stone-500 mb-2 flex items-center gap-2",children:[e.jsx(ps,{className:"w-4 h-4"}),"Database Totals (Includes Beta Testers - NOT Real Revenue)"]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4",children:[e.jsx(m,{className:"bg-stone-50 border-stone-200 border-dashed",children:e.jsx(p,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-stone-500",children:"Database MRR (All)"}),e.jsx("p",{className:"text-xl font-bold text-stone-500",children:a(n.monthlyRecurringRevenue)})]}),e.jsx(Me,{className:"h-5 w-5 text-stone-400"})]})})}),e.jsx(m,{className:"bg-stone-50 border-stone-200 border-dashed",children:e.jsx(p,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-stone-500",children:"Database ARR (All)"}),e.jsx("p",{className:"text-xl font-bold text-stone-500",children:a(n.annualRecurringRevenue)})]}),e.jsx(ts,{className:"h-5 w-5 text-stone-400"})]})})}),e.jsx(m,{className:"bg-blue-50 border-blue-200 border-dashed",children:e.jsx(p,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-blue-600",children:"Beta Testers / Unverified"}),e.jsx("p",{className:"text-xl font-bold text-blue-700",children:n.unverifiedPaidUsers})]}),e.jsx(ps,{className:"h-5 w-5 text-blue-500"})]})})}),e.jsx(m,{className:"bg-stone-50 border-stone-200 border-dashed",children:e.jsx(p,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-stone-500",children:"Free Users"}),e.jsx("p",{className:"text-xl font-bold text-stone-500",children:n.totalFreeUsers})]}),e.jsx(te,{className:"h-5 w-5 text-stone-400"})]})})})]})]})]}),u&&R&&R.recentPayments.length>0&&e.jsxs(m,{children:[e.jsx(J,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs(ee,{className:"flex items-center gap-2",children:[e.jsx(we,{className:"w-5 h-5 text-green-600"}),"Recent Stripe Payments"]}),e.jsxs(de,{children:["Live payment data from Stripe (",R.recentPayments.length," transactions)"]})]}),e.jsx(q,{className:"bg-green-600",children:"LIVE"})]})}),e.jsx(p,{children:e.jsxs("div",{className:"overflow-x-auto",children:[e.jsxs(De,{children:[e.jsx(Ue,{children:e.jsxs(oe,{children:[e.jsx(U,{children:"Customer"}),e.jsx(U,{children:"Amount"}),e.jsx(U,{children:"Description"}),e.jsx(U,{children:"Date"}),e.jsx(U,{children:"Receipt"})]})}),e.jsx(Te,{children:R.recentPayments.slice(0,10).map(s=>e.jsxs(oe,{children:[e.jsx(E,{children:e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:s.customer_name}),e.jsx("p",{className:"text-sm text-stone-500",children:s.customer_email})]})}),e.jsx(E,{className:"font-semibold text-green-700",children:a(s.amount)}),e.jsx(E,{className:"text-sm",children:s.description}),e.jsx(E,{className:"text-sm text-stone-500",children:new Date(s.created).toLocaleDateString()}),e.jsx(E,{children:s.receipt_url&&e.jsxs("a",{href:gs(s.receipt_url),target:"_blank",rel:"noopener noreferrer",className:"text-blue-600 hover:text-blue-800 text-sm flex items-center gap-1",children:[e.jsx(qe,{className:"w-3.5 h-3.5"}),"View"]}
8)})]},s.id))})]}),R.recentPayments.length>10&&e.jsxs("p",{className:"text-center text-sm text-stone-500 mt-4",children:["Showing 10 of ",R.recentPayments.length," payments"]})]})})]}),e.jsxs(ks,{value:w,onValueChange:N,className:"w-full",children:[e.jsxs(Cs,{className:"grid w-full grid-cols-3",children:[e.jsxs(ke,{value:"overview",className:"flex items-center gap-2",children:[e.jsx(ms,{className:"w-4 h-4"}),"Overview"]}),e.jsxs(ke,{value:"payments",className:"flex items-center gap-2",children:[e.jsx(Ce,{className:"w-4 h-4"}),"Payment History"]}),e.jsxs(ke,{value:"users",className:"flex items-center gap-2",children:[e.jsx(te,{className:"w-4 h-4"}),"User Status"]})]}),e.jsxs(Qe,{value:"overview",className:"space-y-6 mt-6",children:[e.jsx(m,{children:e.jsx(p,{className:"p-6",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-stone-600",children:"Conversion Rate"}),e.jsxs("p",{className:"text-2xl font-bold text-stone-900",children:[n.totalPaidUsers+n.totalFreeUsers>0?(n.totalPaidUsers/(n.totalPaidUsers+n.totalFreeUsers)*100).toFixed(1):0,"%"]}),e.jsxs("p",{className:"text-xs text-stone-500 mt-1",children:[n.totalPaidUsers," paid of ",n.totalPaidUsers+n.totalFreeUsers," total users"]})]}),e.jsx("div",{className:"w-32 h-4 bg-stone-200 rounded-full overflow-hidden",children:e.jsx("div",{className:"h-full bg-gradient-to-r from-green-500 to-emerald-500",style:{width:`${n.totalPaidUsers+n.totalFreeUsers>0?n.totalPaidUsers/(n.totalPaidUsers+n.totalFreeUsers)*100:0}%`}})})]})})}),e.jsxs(m,{children:[e.jsxs(J,{children:[e.jsxs(ee,{className:"flex items-center gap-2",children:[e.jsx(ms,{className:"w-5 h-5"}),"Revenue by Plan"]}),e.jsx(de,{children:"Breakdown of monthly recurring revenue by subscription plan"})]}),e.jsx(p,{children:n.planBreakdown.length===0?e.jsx("div",{className:"text-center py-8 text-stone-500",children:"No paid subscriptions yet"}):e.jsx("div",{className:"space-y-4",children:n.planBreakdown.map(s=>e.jsxs("div",{className:"flex items-center justify-between p-3 bg-stone-50 rounded-lg",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(q,{className:re(s.plan),children:ge(s.plan)}),e.jsxs("span",{className:"text-sm text-stone-600",children:[s.count," user",s.count!==1?"s":""]})]}),e.jsx("div",{className:"text-right",children:e.jsxs("p",{className:"font-semibold text-stone-900",children:[a(s.monthlyRevenue),"/mo"]})})]},s.plan))})})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsx(m,{children:e.jsx(p,{className:"p-6",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-stone-600",children:"Avg Revenue Per Paid User"}),e.jsx("p",{className:"text-2xl font-bold text-stone-900",children:n.totalPaidUsers>0?a(n.monthlyRecurringRevenue/n.totalPaidUsers):"$0.00"}),e.jsx("p",{className:"text-xs text-stone-500 mt-1",children:"per month"})]}),e.jsx(xt,{className:"h-8 w-8 text-stone-400"})]})})}),e.jsx(m,{children:e.jsx(p,{className:"p-6",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-stone-600",children:"Lifetime Value (Est.)"}),e.jsx("p",{className:"text-2xl font-bold text-stone-900",children:n.totalPaidUsers>0?a(n.monthlyRecurringRevenue/n.totalPaidUsers*12):"$0.00"}),e.jsx("p",{className:"text-xs text-stone-500 mt-1",children:"assuming 12-month retention"})]}),e.jsx(Ds,{className:"h-8 w-8 text-stone-400"})]})})})]})]}),e.jsx(Qe,{value:"payments",className:"space-y-4 mt-6",children:e.jsxs(m,{children:[e.jsx(J,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs(ee,{className:"flex items-center gap-2",children:[e.jsx(Ce,{className:"w-5 h-5"}),"Payment History"]}),e.jsxs(de,{children:["All payments made through Stripe (",x.length," records)"]})]}),e.jsxs($,{variant:"outline",size:"sm",onClick:f,children:[e.jsx(ls,{className:"w-4 h-4 mr-2"}),"Export CSV"]})]})}),e.jsx(p,{children:v?e.jsxs("div",{className:"text-center py-8",children:[e.jsx("div",{className:"animate-spin rounded-full h-8 w-8 border-b-2 border-green-600 mx-auto"}),e.jsx("p",{className:"text-stone-500 mt-2",children:"Loading payments..."})]}):x.length===0?e.jsxs("div",{className:"text-center py-8 text-stone-500",children:[e.jsx(Ce,{className:"w-12 h-12 mx-auto mb-4 text-stone-400"}
8),e.jsx("p",{children:"No payment records found"}),e.jsx("p",{className:"text-sm",children:"Payments will appear here once users subscribe"})]}):e.jsxs("div",{className:"overflow-x-auto",children:[e.jsxs(De,{children:[e.jsx(Ue,{children:e.jsxs(oe,{children:[e.jsx(U,{children:"User"}),e.jsx(U,{children:"Plan"}),e.jsx(U,{children:"Amount"}),e.jsx(U,{children:"Status"}),e.jsx(U,{children:"Date"})]})}),e.jsx(Te,{children:x.slice(0,50).map(s=>e.jsxs(oe,{children:[e.jsx(E,{children:e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:s.full_name}),e.jsx("p",{className:"text-sm text-stone-500",children:s.email})]})}),e.jsx(E,{children:e.jsx(q,{className:re(s.plan),children:ge(s.plan)})}),e.jsx(E,{className:"font-semibold",children:a(s.amount)}),e.jsx(E,{children:e.jsxs(q,{variant:s.status==="succeeded"?"default":s.status==="pending"?"secondary":s.status==="refunded"?"outline":"destructive",children:[s.status==="succeeded"&&e.jsx(_e,{className:"w-3 h-3 mr-1"}),s.status==="pending"&&e.jsx(Se,{className:"w-3 h-3 mr-1"}),s.status==="failed"&&e.jsx(Ae,{className:"w-3 h-3 mr-1"}),s.status]})}),e.jsx(E,{className:"text-sm text-stone-500",children:new Date(s.payment_date).toLocaleDateString()})]},s.id))})]}),x.length>50&&e.jsxs("p",{className:"text-center text-sm text-stone-500 mt-4",children:["Showing 50 of ",x.length," records. Export to CSV for full data."]})]})})]})}),e.jsxs(Qe,{value:"users",className:"space-y-4 mt-6",children:[e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-5 gap-4",children:[e.jsx(m,{className:`cursor-pointer transition-all ${h==="new"?"ring-2 ring-blue-500":""}`,onClick:()=>A("new"),children:e.jsxs(p,{className:"p-4 text-center",children:[e.jsx(ns,{className:"w-6 h-6 mx-auto text-blue-600 mb-2"}),e.jsx("p",{className:"text-2xl font-bold text-blue-700",children:Q.new}),e.jsx("p",{className:"text-xs text-stone-500",children:"New (30 days)"})]})}),e.jsx(m,{className:`cursor-pointer transition-all ${h==="active"?"ring-2 ring-green-500":""}`,onClick:()=>A("active"),children:e.jsxs(p,{className:"p-4 text-center",children:[e.jsx(_e,{className:"w-6 h-6 mx-auto text-green-600 mb-2"}),e.jsx("p",{className:"text-2xl font-bold text-green-700",children:Q.active}),e.jsx("p",{className:"text-xs text-stone-500",children:"Active Paid"})]})}),e.jsx(m,{className:`cursor-pointer transition-all ${h==="trial"?"ring-2 ring-yellow-500":""}`,onClick:()=>A("trial"),children:e.jsxs(p,{className:"p-4 text-center",children:[e.jsx(Se,{className:"w-6 h-6 mx-auto text-yellow-600 mb-2"}),e.jsx("p",{className:"text-2xl font-bold text-yellow-700",children:Q.trial}),e.jsx("p",{className:"text-xs text-stone-500",children:"On Trial"})]})}),e.jsx(m,{className:`cursor-pointer transition-all ${h==="canceled"?"ring-2 ring-red-500":""}`,onClick:()=>A("canceled"),children:e.jsxs(p,{className:"p-4 text-center",children:[e.jsx(xs,{className:"w-6 h-6 mx-auto text-red-600 mb-2"}),e.jsx("p",{className:"text-2xl font-bold text-red-700",children:Q.canceled}),e.jsx("p",{className:"text-xs text-stone-500",children:"Canceled"})]})}),e.jsx(m,{className:`cursor-pointer transition-all ${h==="non-paying"?"ring-2 ring-stone-500":""}`,onClick:()=>A("non-paying"),children:e.jsxs(p,{className:"p-4 text-center",children:[e.jsx(Ge,{className:"w-6 h-6 mx-auto text-stone-600 mb-2"}),e.jsx("p",{className:"text-2xl font-bold text-stone-700",children:Q["non-paying"]}),e.jsx("p",{className:"text-xs text-stone-500",children:"Non-Paying"})]})})]}),e.jsxs(m,{children:[e.jsx(J,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs(ee,{className:"flex items-center gap-2",children:[e.jsx(te,{className:"w-5 h-5"}),"User Status Report"]}),e.jsxs(de,{children:[h==="all"?"All users":`${h.charAt(0).toUpperCase()+h.slice(1)} users`," (",_.length," records)"]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx($,{variant:h==="all"?"default":"outline",size:"sm",onClick:()=>A("all"),children:"Show All"}),e.jsxs($,{variant:"outline",size:"sm",onClick:Z,children:[e.jsx(ls,{className:"w-4 h-4 mr-2"}),"Export CSV"]})]})]})}),e.jsx(p,{children:T?e.jsxs("div",{className:"text-center py-8",children:[e.jsx("div",{className:"animate-spin rounded-full h-8 w-8 border-b-2 border-green-600 mx-auto"}),e.jsx("p",{className:"text-stone-500 mt-2",children:"Loading user status..."})]}):_.length===0?e.jsxs("div",{className:"text-center py-8 text-stone-500",children:[e.jsx(te,{className:"w-12 h-12 mx-auto mb-4 text-stone-400"}),e.jsx("p",{children:"No users found in this category"})]}):e.jsxs("div",{className:"overflow-x-auto",children:[e.jsxs(De,{children:[e.jsx(Ue,{children:e.jsxs(oe,{children:[e.jsx(U,{children:"User"}),e.jsx(U,{children:"Type"}),e.jsx(U,{children:"Plan"}),e.jsx(U,{children:"Status"}),e.jsx(U,{children:"Category"}),e.jsx(U,{children:"Created"})]})}),e.jsx(Te,{children:_.slice(0,50).map(s=>e.jsxs(oe,{children:[e.jsx(E,{children:e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:s.full_name}),e.jsx("p",{className:"text-sm text-stone-500",children:s.email})]})}),e.jsx(E,{children:e.jsx(q,{variant:"outline",className:"capitalize",children:s.user_type})}),e.jsx(E,{children:e.jsx(q,{className:re(s.subscription_plan),children:ge(s.subscription_plan)})}),e.jsx(E,{className:"capitalize text-sm",children:s.subscription_status||"none"}),e.jsx(E,{children:e.jsxs(q,{variant:s.status_category==="active"?"default":s.status_category==="new"?"secondary":s.status_category==="trial"?"outline":s.status_category==="canceled"?"destructive":"secondary",children:[s.status_category==="new"&&e.jsx(ns,{className:"w-3 h-3 mr-1"}),s.status_category==="active"&&e.jsx(_e,{className:"w-3 h-3 mr-1"}),s.status_category==="canceled"&&e.jsx(xs,{className:"w-3 h-3 mr-1"}),s.status_category==="trial"&&e.jsx(Se,{className:"w-3 h-3 mr-1"}),s.status_category==="non-paying"&&e.jsx(Ge,{className:"w-3 h-3 mr-1"}),s.status_category]})}),e.jsx(E,{className:"text-sm text-stone-500",children:new Date(s.created_at).toLocaleDateString()})]},s.id))})]}),_.length>50&&e.jsxs("p",{className:"text-center text-sm text-stone-500 mt-4",children:["Showing 50 of ",_.length," records. Export to CSV for full data."]})]})})]})]})]}),e.jsx(m,{className:"bg-blue-50 border-blue-200",children:e.jsx(p,{className:"p-4",children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx(Ge,{className:"w-5 h-5 text-blue-600 mt-0.5"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-blue-800",children:"How to Connect to Stripe for Real-Time Data"}),e.jsx("p",{className:"text-sm text-blue-700 mt-1",children:"To show actual Stripe payment data, we need to integrate the Stripe API. Currently, this dashboard only reads subscription status from the database (which includes beta tester
8s with manually-set statuses)."}),e.jsxs("div",{className:"mt-3 flex flex-wrap gap-2",children:[e.jsxs("a",{href:"https://dashboard.stripe.com/payments",target:"_blank",rel:"noopener noreferrer",className:"inline-flex items-center gap-1.5 text-sm bg-blue-600 text-white px-3 py-1.5 rounded-md hover:bg-blue-700",children:[e.jsx(qe,{className:"w-3.5 h-3.5"}),"View Payments in Stripe"]}),e.jsxs("a",{href:"https://dashboard.stripe.com/customers",target:"_blank",rel:"noopener noreferrer",className:"inline-flex items-center gap-1.5 text-sm bg-white text-blue-700 border border-blue-300 px-3 py-1.5 rounded-md hover:bg-blue-50",children:[e.jsx(te,{className:"w-3.5 h-3.5"}),"View Customers in Stripe"]})]})]})]})})})]})},Re={broker_trial_days:14,lender_trial_days:60,agent_trial_days:14,vendor_trial_days:14,client_trial_days:3},Ct=[{value:"3",label:"3 Days",description:"Quick evaluation"},{value:"14",label:"14 Days",description:"Standard trial"},{value:"30",label:"30 Days",description:"Extended trial"},{value:"60",label:"60 Days",description:"Premium trial"}],Rt=[{key:"broker_trial_days",label:"Brokers / Loan Officers",icon:te,color:"text-blue-500",bgColor:"bg-blue-100"},{key:"lender_trial_days",label:"Lenders / Account Executives",icon:Ee,color:"text-green-500",bgColor:"bg-green-100"},{key:"agent_trial_days",label:"Real Estate Agents",icon:Fe,color:"text-blue-500",bgColor:"bg-blue-100"},{key:"vendor_trial_days",label:"Service Providers / Vendors",icon:Ts,color:"text-orange-500",bgColor:"bg-orange-100"},{key:"client_trial_days",label:"Clients (ReadySetHome.ai)",icon:js,color:"text-teal-500",bgColor:"bg-teal-100"}],At=()=>{const[t,c]=l.useState(!0),[j,g]=l.useState(!1),[w,N]=l.useState(Re),[n,C]=l.useState(Re),[x,P]=l.useState(!1),{toast:r}=Ie();l.useEffect(()=>{i()},[]),l.useEffect(()=>{const d=w.broker_trial_days!==n.broker_trial_days||w.lender_trial_days!==n.lender_trial_days||w.agent_trial_days!==n.agent_trial_days||w.vendor_trial_days!==n.vendor_trial_days||w.client_trial_days!==n.client_trial_days;P(d)},[w,n]);const i=async()=>{c(!0);try{const{data:d,error:h}=await S.from("trial_settings").select("*").eq("id","default").single();if(h)if(h.code==="PGRST116")N(Re),C(Re);else throw h;else if(d){const A={broker_trial_days:d.broker_trial_days,lender_trial_days:d.lender_trial_days,agent_trial_days:d.agent_trial_days,vendor_trial_days:d.vendor_trial_days,client_trial_days:d.client_trial_days,updated_at:d.updated_at,updated_by:d.updated_by};N(A),C(A)}}catch(d){console.error("Error fetching trial settings:",d),r({title:"Error",description:"Failed to load trial settings.",variant:"destructive"})}finally{c(!1)}},v=async()=>{g(!0);try{const{error:d}=await S.from("trial_settings").upsert({id:"default",broker_trial_days:w.broker_trial_days,lender_trial_days:w.lender_trial_days,agent_trial_days:w.agent_trial_days,vendor_trial_days:w.vendor_trial_days,client_trial_days:w.client_trial_days,updated_at:new Date().toISOString(),updated_by:"admin"});if(d)throw d;C(w),P(!1),r({title:"Settings Saved",description:"Trial duration settings have been updated successfully."})}catch(d){console.error("Error saving trial settings:",d),r({title:"Error",description:"Failed to save trial settings.",variant:"destructive"})}finally{g(!1)}},k=()=>{N(Re),r({title:"Reset to Defaults",description:"Settings reset. Save to apply changes."})},T=(d,h)=>{N(A=>({...A,[d]:parseInt(h)}))};return t?e.jsx(m,{className:"bg-stone-800 border-stone-700",children:e.jsx(p,{className:"p-8",children:e.jsxs("div",{className:"flex items-center justify-center gap-2 text-stone-400",children:[e.jsx(pe,{className:"w-5 h-5 animate-spin"}),"Loading trial settings..."]})})}):e.jsxs(m,{className:"bg-stone-800 border-stone-700",children:[e.jsxs(J,{children:[e.jsxs(ee,{className:"text-white flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Se,{className:"w-5 h-5 text-blue-400"}),"Free Trial Duration Settings"]}),e.jsxs("div",{className:"flex gap-2",children:[x&&e.jsx(q,{className:"bg-yellow-600",children:"Unsaved Changes"}),e.jsxs($,{onClick:k,variant:"outline",size:"sm",className:"bg-stone-700 text-white border-stone-600 hover:bg-stone-600",children:[e.jsx(pe,{className:"w-4 h-4 mr-2"}),"Reset to Defaults"]}),e.jsxs($,{onClick:v,size:"sm",className:"bg-green-600 hover:bg-green-700",disabled:!x||j,children:[e.jsx(Us,{className:"w-4 h-4 mr-2"}),j?"Saving...":"Save Changes"]})]})]}),e.jsx(de,{className:"text-stone-400",children:"Configure the default free trial duration for each user type. New users will automatically receive the configured trial period."})]}),e.jsxs(p,{className:"space-y-6",children:[e.jsx("div",{className:"bg-blue-900/30 border border-blue-700 rounded-lg p-4",children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx(Ge,{className:"w-5 h-5 text-blue-400 mt-0.5 flex-shrink-0"}),e.jsxs("div",{className:"text-sm text-blue-200",children:[e.jsx("p",{className:"font-medium mb-1",children:"How Trial Settings Work:"}),e.jsxs("ul",{className:"list-disc list-inside space-y-1 text-blue-300",children:[e.jsxs("li",{children:["These settings apply to ",e.jsx("strong",{children:"new signups only"})]}),e.jsx("li",{children:"Existing users keep their current trial period"}),e.jsx("li",{children:"All users get full premium access during trial"}
8),e.jsx("li",{children:"Beta testers bypass trials and get permanent access"})]})]})]})}),e.jsx("div",{className:"grid gap-4",children:Rt.map(d=>{const h=d.icon,A=w[d.key],u=n[d.key],y=A!==u;return e.jsxs("div",{className:`flex items-center justify-between p-4 rounded-lg border ${y?"border-yellow-500 bg-yellow-900/20":"border-stone-600 bg-stone-700/50"}`,children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx("div",{className:`p-2.5 rounded-lg ${d.bgColor}`,children:e.jsx(h,{className:`w-5 h-5 ${d.color}`})}),e.jsxs("div",{children:[e.jsx(ie,{className:"text-white font-medium text-base",children:d.label}),e.jsxs("p",{className:"text-sm text-stone-400",children:["Current: ",A," days free trial",y&&e.jsxs("span",{className:"text-yellow-400 ml-2",children:["(was ",u," days)"]})]})]})]}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs(Ze,{value:A.toString(),onValueChange:R=>T(d.key,R),children:[e.jsx(Xe,{className:"w-[160px] bg-stone-600 border-stone-500 text-white",children:e.jsx(Je,{})}),e.jsx(es,{children:Ct.map(R=>e.jsx(le,{value:R.value,children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"font-medium",children:R.label}),e.jsxs("span",{className:"text-stone-400 text-xs",children:["- ",R.description]})]})},R.value))})]}),y&&e.jsx(_e,{className:"w-5 h-5 text-yellow-400"})]})]},d.key)})}),n.updated_at&&e.jsxs("div",{className:"text-sm text-stone-500 text-right",children:["Last updated: ",new Date(n.updated_at).toLocaleString(),n.updated_by&&` by ${n.updated_by}`]})]})]})},Lt=()=>{const{toast:t}=Ie(),{user:c}=fs(),[j,g]=l.useState([]),[w,N]=l.useState(!0),[n,C]=l.useState("open"),[x,P]=l.useState(null),[r,i]=l.useState(!1),[v,k]=l.useState(!1),[T,d]=l.useState(""),[h,A]=l.useState("dismiss"),[u,y]=l.useState(!1),R=async()=>{var o;N(!0);try{const{data:K,error:Y}=await S.from("promotion_flags").select("*").eq("status",n.toUpperCase()).order("created_at",{ascending:!1});if(Y){if(Y.code==="42P01"||(o=Y.message)!=null&&o.includes("does not exist")){g([]),N(!1);return}console.error("Error loading flags:",Y),t({title:"Error",description:"Failed to load promotion flags.",variant:"destructive"}),g([]);return}const H=await Promise.all((K||[]).map(async L=>{let V=null,f=null,Z=null,ae=0;if(L.promotion_type==="broker"){const{data:_}=await S.from("broker_promotions").select("promotion_title, description, broker_id, flag_count").eq("id",L.promotion_id).single();if(V=_,ae=(_==null?void 0:_.flag_count)||0,_!=null&&_.broker_id){const{data:Q}=await S.from("user_profiles").select("full_name, email").eq("user_id",_.broker_id).single();f=Q}}else if(L.promotion_type==="agent"){const{data:_}=await S.from("agent_promotions").select("promotion_title, description, agent_id, flag_count").eq("id",L.promotion_id).single();if(V=_,ae=(_==null?void 0:_.flag_count)||0,_!=null&&_.agent_id){const{data:Q}=await S.from("agent_profiles").select("first_name, last_name, email").eq("id",_.agent_id).single();Q&&(f={full_name:`${Q.first_name} ${Q.last_name}`,email:Q.email})}}else if(L.promotion_type==="vendor"){const{data:_}=await S.from("vendor_promotions").select("title, description, vendor_id").eq("id",L.promotion_id).single();if(V=_?{promotion_title:_.title,description:_.description}:null,_!=null&&_.vendor_id){const{data:Q}=await S.from("service_providers").select("company_name, email").eq("id",_.vendor_id).single();Q&&(f={full_name:Q.company_name,email:Q.email})}}if(L.flagged_by_user_id){const{data:_}=await S.from("user_profiles").select("email").eq("user_id",L.flagged_by_user_id).single();Z=_}return{...L,promotion_title:(V==null?void 0:V.promotion_title)||"Unknown Promotion",promotion_description:(V==null?void 0:V.description)||"",professional_name:(f==null?void 0:f.full_name)||"Unknown",professional_email:(f==null?void 0:f.email)||"",reporter_email:(Z==null?void 0:Z.email)||"Anonymous",flag_count:ae}}));g(H)}catch(K){console.error("Error in loadFlags:",K),t({title:"Error",description:"An error occurred while loading flags.",variant:"destructive"})}finally{N(!1)}};l.useEffect(()=>{R()},[n]);const se=o=>{P(o),d(""),A("dismiss"),i(!0)},b=async()=>
8{if(!(!x||!(c!=null&&c.id))){y(!0);try{const o=h==="dismiss"?"DISMISSED":"RESOLVED",K=await ht(x.id,c.id,o,T);if(!K.success){t({title:"Error",description:K.error||"Failed to resolve flag.",variant:"destructive"});return}h==="disable"?(await ut(x.promotion_id,x.promotion_type,c.id,`Admin action: ${T||"Flagged content review"}`)).success||t({title:"Warning",description:"Flag resolved but failed to disable promotion.",variant:"destructive"}):h==="remove"&&((await pt(x.promotion_id,x.promotion_type,c.id,`Admin action: ${T||"Flagged content removal"}`)).success||t({title:"Warning",description:"Flag resolved but failed to remove promotion.",variant:"destructive"})),t({title:"Flag resolved",description:`The flag has been ${o.toLowerCase()} successfully.`}),i(!1),P(null),R()}catch(o){console.error("Error resolving flag:",o),t({title:"Error",description:"An error occurred while resolving the flag.",variant:"destructive"})}finally{y(!1)}}},z=o=>{switch(o){case"broker":return e.jsx(Ke,{className:"w-4 h-4 text-blue-500"});case"agent":return e.jsx(Fe,{className:"w-4 h-4 text-green-500"});case"vendor":return e.jsx(te,{className:"w-4 h-4 text-orange-500"})}},G=o=>{switch(o){case"GUARANTEE_LANGUAGE":case"PREFERRED_PROVIDER":case"ILLEGAL_REBATE":return"bg-red-100 text-red-800 border-red-200";case"MISLEADING":return"bg-orange-100 text-orange-800 border-orange-200";case"INAPPROPRIATE_CONTENT":return"bg-blue-100 text-blue-800 border-blue-200";case"EXPIRED_OFFER":return"bg-yellow-100 text-yellow-800 border-yellow-200";default:return"bg-stone-100 text-stone-800 border-stone-200"}},F=o=>new Date(o).toLocaleString("en-US",{month:"short",day:"numeric",year:"numeric",hour:"numeric",minute:"2-digit"});return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("h2",{className:"text-2xl font-bold text-stone-900 flex items-center gap-2",children:[e.jsx(He,{className:"w-6 h-6 text-red-500"}),"Promotion Review Queue"]}),e.jsx("p",{className:"text-stone-500 mt-1",children:"Review and moderate flagged promotions from users"})]}),e.jsxs($,{variant:"outline",onClick:R,disabled:w,children:[e.jsx(pe,{className:`w-4 h-4 mr-2 ${w?"animate-spin":""}`}),"Refresh"]})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsx(m,{children:e.jsx(p,{className:"pt-6",children:e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx("div",{className:"p-3 bg-red-100 rounded-lg",children:e.jsx(be,{className:"w-6 h-6 text-red-600"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-2xl font-bold text-stone-900",children:j.filter(o=>o.status==="OPEN").length||0}),e.jsx("p",{className:"text-sm text-stone-500",children:"Open Flags"})]})]})})}),e.jsx(m,{children:e.jsx(p,{className:"pt-6",children:e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx("div",{className:"p-3 bg-green-100 rounded-lg",children:e.jsx(We,{className:"w-6 h-6 text-green-600"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-2xl font-bold text-stone-900",children:j.filter(o=>o.status==="RESOLVED").length||0}),e.jsx("p",{className:"text-sm text-stone-500",children:"Resolved"})]})]})})}),e.jsx(m,{children:e.jsx(p,{className:"pt-6",children:e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx("div",{className:"p-3 bg-stone-100 rounded-lg",children:e.jsx(Ae,{className:"w-6 h-6 text-stone-600"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-2xl font-bold text-stone-900",children:j.filter(o=>o.status==="DISMISSED").length||0}),e.jsx("p",{className:"text-sm text-stone-500",children:"Dismissed"})]})]})})})]}),e.jsxs(ks,{value:n,onValueChange:o=>C(o),children:[e.jsxs(Cs,{children:[e.jsxs(ke,{value:"open",className:"gap-2",children:[e.jsx(be,{className:"w-4 h-4"}),"Open"]}),e.jsxs(ke,{value:"resolved",className:"gap-2",children:[e.jsx(We,{className:"w-4 h-4"}),"Resolved"]}),e.jsxs(ke,{value:"dismissed",className:"gap-2",children:[e.jsx(Ae,{className:"w-4 h-4"}),"Dismissed"]})]}),e.jsx(Qe,{value:n,className:"mt-6",children:w?e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(fe,{className:"w-8 h-8 animate-spin text-stone-400"})}):j.length===0?e.jsx(m,{children:e.jsxs(p,{className:"py-12 text-center",children:[n==="open"?e.jsx(We,{className:"w-16 h-16 text-green-300 mx-auto mb-4"}):e.jsx(He,{className:"w-16 h-16 text-stone-300 mx-auto mb-4"}),e.jsx("h3",{className:"text-xl font-semibold text-stone-700 mb-2",children:n==="open"?"No open flags":`No ${n} flags`}),e.jsx("p",{className:"text-stone-500",children:n==="open"?"All promotions are looking good!":"No flags with this status yet."})]})}):e.jsx(m,{children:e.jsxs(De,{children:[e.jsx(Ue,{children:e.jsxs(oe,{children:[e.jsx(U,{children:"Promotion"}),e.jsx(U,{children:"Type"}),e.jsx(U,{children:"Reason"}),e.jsx(U,{children:"Reporter"}),e.jsx(U,{children:"Date"}),e.jsx(U,{className:"text-right",children:"Actions"})]})}),e.jsx(Te,{children:j.map(o=>e.jsxs(oe,{children:[e.jsx(E,{children:e.jsxs("div",{className:"max-w-xs",children:[e.jsx("p",{className:"font-medium text-stone-900 truncate",children:o.promotion_title}),e.jsxs("p",{className:"text-sm text-stone-500 truncate",children:["by ",o.professional_name]}),o.flag_count&&o.flag_count>1&&e.jsxs(q,{variant:"outline",className:"mt-1 text-red-600 border-red-200",children:[o.flag_count," total flags"]})]})}),e.jsx(E,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[z(o.promotion_type),e.jsx("span",{className:"capitalize text-sm",children:o.promotion_type})]})}),e.jsxs(E,{children:[e.jsx(q,{className:`${G(o.reason)} border`,children:hs(o.reason)}),o.details&&e.jsxs("p",{className:"text-xs text-stone-500 mt-1 max-w-xs truncate",children:['"',o.details,'"']})]}),e.jsx(E,{children:e.jsx("p",{className:"text-sm text-stone-600",children:o.reporter_email})}),e.jsx(E,{children:e.jsx("p",{className:"text-sm text-stone-600",children:F(o.created_at)})}),e.jsx(E,{className:"text-right",children:n==="open"?e.jsxs($,{size:"sm",onClick:()=>se(o),children:[e.jsx(Is,{className:"w-4 h-4 mr-2"}),"Review"]}):e.jsx("div",{className:"text-sm text-stone-500",children:o.resolution_note&&e.jsx("p",{className:"max-w-xs truncate",children:o.resolution_note})})})]},o.id))})]})})})]}),e.jsx(bs,{open:r,onOpenChange:i,children:e.jsxs(Ns,{className:"sm:max-w-lg",children:[e.jsxs(vs,{children:[e.jsxs(ys,{className:"flex items-center gap-2",children:[e.jsx(He,{className:"w-5 h-5 text-red-500"}),"Review Flagged Promotion"]}),e.jsx(ws,{children:"Take action on this reported promotion"})]}),x&&e.jsxs("div",{className:"space-y-4 py-4",children:[e.jsxs("div",{className:"bg-stone-50 p-4 rounded-lg space-y-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[z(x.promotion_type),e.jsxs("span",{className:"font-medium capitalize",children:[x.promotion_type," Promotion"]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"font-semibold text-stone-900",children:x.promotion_title}),e.jsx("p",{className:"text-sm text-stone-600 mt-1 line-clamp-2",children:x.promotion_description})]}),e.jsxs("div",{className:"flex items-center gap-2 text-sm text-stone-500",children:[e.jsx(js,{className:"w-4 h-4"}
8),e.jsxs("span",{children:[x.professional_name," (",x.professional_email,")"]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ie,{className:"text-sm font-medium text-stone-700",children:"Flag Reason"}),e.jsx(q,{className:`${G(x.reason)} border`,children:hs(x.reason)}),x.details&&e.jsx("div",{className:"bg-red-50 p-3 rounded-lg border border-red-100 mt-2",children:e.jsxs("div",{className:"flex items-start gap-2",children:[e.jsx($s,{className:"w-4 h-4 text-red-500 mt-0.5"}),e.jsx("p",{className:"text-sm text-red-800",children:x.details})]})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ie,{className:"text-sm font-medium text-stone-700",children:"Action"}),e.jsxs(Ze,{value:h,onValueChange:o=>A(o),children:[e.jsx(Xe,{children:e.jsx(Je,{})}),e.jsxs(es,{children:[e.jsx(le,{value:"dismiss",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ae,{className:"w-4 h-4 text-stone-500"}),e.jsx("span",{children:"Dismiss Flag (no action on promotion)"})]})}),e.jsx(le,{value:"disable",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(us,{className:"w-4 h-4 text-yellow-500"}),e.jsx("span",{children:"Disable Promotion (can be re-enabled)"})]})}),e.jsx(le,{value:"remove",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(is,{className:"w-4 h-4 text-red-500"}),e.jsx("span",{children:"Remove Promotion (permanent)"})]})})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ie,{htmlFor:"resolution-note",className:"text-sm font-medium text-stone-700",children:"Resolution Note"}),e.jsx(Bs,{id:"resolution-note",placeholder:"Add a note explaining your decision...",value:T,onChange:o=>d(o.target.value),className:"h-20 resize-none"})]})]}),e.jsxs(_s,{className:"gap-2",children:[e.jsx($,{variant:"outline",onClick:()=>i(!1),disabled:u,children:"Cancel"}),e.jsx($,{onClick:b,disabled:u,className:h==="remove"?"bg-red-600 hover:bg-red-700":h==="disable"?"bg-yellow-600 hover:bg-yellow-700":"",children:u?e.jsxs(e.Fragment,{children:[e.jsx(fe,{className:"w-4 h-4 mr-2 animate-spin"}),"Processing..."]}):e.jsxs(e.Fragment,{children:[h==="dismiss"&&e.jsx(Ae,{className:"w-4 h-4 mr-2"}),h==="disable"&&e.jsx(us,{className:"w-4 h-4 mr-2"}),h==="remove"&&e.jsx(is,{className:"w-4 h-4 mr-2"}),h==="dismiss"?"Dismiss Flag":h==="disable"?"Disable Promotion":"Remove Promotion"]})})]})]})})]})},Pt=()=>{const{toast:t}=Ie(),[c,j]=l.useState(!1),[g,w]=l.useState(!1),[N,n]=l.useState(null),[C,x]=l.useState(null),[P,r]=l.useState("lender"),[i,v]=l.useState("initial"),[k,T]=l.useState(25),[d,h]=l.useState(!0);l.useEffect(()=>{A()},[]);const A=async()=>{var b,z,G,F;j(!0);try{const[o,K,Y,H]=await Promise.all([S.from("lender_profiles").select("profile_status",{count:"exact"}),S.from("service_providers").select("profile_status",{count:"exact"}),S.from("agent_profiles").select("profile_status",{count:"exact"}),S.from("user_profiles").select("profile_status",{count:"exact"}).eq("user_type","broker")]),L=(V,f)=>(V==null?void 0:V.filter(Z=>Z.profile_status===f).length)||0;n({lender:{total:((b=o.data)==null?void 0:b.length)||0,unclaimed:L(o.data,"unclaimed"),pending:L(o.data,"pending"),claimed:L(o.data,"claimed")},vendor:{total:((z=K.data)==null?void 0:z.length)||0,unclaimed:L(K.data,"unclaimed"),pending:L(K.data,"pending"),claimed:L(K.data,"claimed")},agent:{total:((G=Y.data)==null?void 0:G.length)||0,unclaimed:L(Y.data,"unclaimed"),pending:L(Y.data,"pending"),claimed:L(Y.data,"claimed")},broker:{total:((F=H.data)==null?void 0:F.length)||0,unclaimed:L(H.data,"unclaimed"),pending:L(H.data,"pending"),claimed:L(H.data,"claimed")}})}catch(o){console.error("Error loading stats:",o),t({title:"Error loading stats",description:"Failed to load profile statistics",variant:"destructive"})}finally{j(!1)}},u=async()=>{w(!0),x(null);try{const{data:b,error:z}=await S.functions.invoke("send-claim-invitation",{body:{profileType:P,batchSize:k,testMode:d,emailSequence:i}});if(z)throw z;x({success:b.success,sent:b.sent||0,errors:b.errors||0,skipped:b.skipped||0,message:b.message||"Emails sent successfully"}),t({title:b.success?"Invitations Sent!":"Partial Success",description:`Sent: ${b.sent}, Errors: ${b.errors}, Skipped: ${b.skipped}`,variant:b.errors>0?"destructive":"default"}),await A()}catch(b){console.error("Error sending invitations:",b),x({success:!1,sent:0,errors:1,skipped:0,message:b.message||"Failed to send invitations"}),t({title:"Send Failed",description:b.message||"Failed to send invitation emails",variant:"destructive"})}finally{w(!1)}},y=b=>{switch(b){case"lender":return Ee;case"vendor":return Ke;
8case"agent":return Fe;case"broker":return te;default:return te}},R=b=>{switch(b){case"lender":return"text-green-600 bg-green-50 border-green-200";case"vendor":return"text-orange-600 bg-orange-50 border-orange-200";case"agent":return"text-blue-600 bg-blue-50 border-blue-200";case"broker":return"text-indigo-600 bg-indigo-50 border-indigo-200";default:return"text-stone-600 bg-stone-50 border-stone-200"}},se={initial:"First invitation - introduces LendMatchPro and benefits",reminder_3day:"Day 3 reminder - emphasizes urgency and activity",reminder_7day:"Day 7 reminder - softer nudge with profile link",final_14day:"Final notice - strong urgency, profile may be reassigned"};return e.jsxs("div",{className:"space-y-6",children:[e.jsx(m,{children:e.jsx(J,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"p-2 bg-gradient-to-br from-blue-500 to-indigo-600 rounded-lg",children:e.jsx(Ss,{className:"w-6 h-6 text-white"})}),e.jsxs("div",{children:[e.jsx(ee,{children:"Claim Invitation Manager"}),e.jsx(de,{children:"Send invitation emails to unclaimed profiles in your directory"})]})]}),e.jsxs($,{variant:"outline",onClick:A,disabled:c,children:[e.jsx(pe,{className:`w-4 h-4 mr-2 ${c?"animate-spin":""}`}),"Refresh Stats"]})]})})}),e.jsx("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:["lender","vendor","agent","broker"].map(b=>{const z=y(b),G=R(b),F=N==null?void 0:N[b];return e.jsx(m,{className:`border-2 ${G.split(" ").slice(1).join(" ")}`,children:e.jsxs(p,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-3 mb-3",children:[e.jsx("div",{className:`p-2 rounded-lg ${G.split(" ").slice(0,2).join(" ")}`,children:e.jsx(z,{className:`w-5 h-5 ${G.split(" ")[0]}`})}),e.jsxs("div",{children:[e.jsxs("p",{className:"text-sm font-medium capitalize",children:[b,"s"]}),e.jsx("p",{className:"text-2xl font-bold",children:(F==null?void 0:F.total)||0})]})]}),e.jsxs("div",{className:"space-y-1 text-sm",children:[e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{className:"text-stone-500",children:"Unclaimed:"}),e.jsx(q,{variant:"outline",className:"bg-red-50 text-red-700 border-red-200",children:(F==null?void 0:F.unclaimed)||0})]}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{className:"text-stone-500",children:"Pending:"}),e.jsx(q,{variant:"outline",className:"bg-yellow-50 text-yellow-700 border-yellow-200",children:(F==null?void 0:F.pending)||0})]}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{className:"text-stone-500",children:"Claimed:"}),e.jsx(q,{variant:"outline",className:"bg-green-50 text-green-700 border-green-200",children:(F==null?void 0:F.claimed)||0})]})]})]})},b)})}),e.jsxs(m,{children:[e.jsxs(J,{children:[e.jsxs(ee,{className:"flex items-center gap-2",children:[e.jsx(cs,{className:"w-5 h-5"}),"Send Invitations"]}),e.jsx(de,{children:"Configure and send claim invitation emails"})]}),e.jsxs(p,{className:"space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-6",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(ie,{children:"Profile Type"}),e.jsxs(Ze,{value:P,onValueChange:b=>r(b),children:[e.jsx(Xe,{children:e.jsx(Je,{})}),e.jsxs(es,{children:[e.jsx(le,{value:"lender",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ee,{className:"w-4 h-4 text-green-600"}),"Lenders / AEs (",(N==null?void 0:N.lender.unclaimed)||0," unclaimed)"]})}),e.jsx(le,{value:"vendor",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ke,{className:"w-4 h-4 text-orange-600"}),"Service Providers (",(N==null?void 0:N.vendor.unclaimed)||0," unclaimed)"]})}),e.jsx(le,{value:"agent",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Fe,{className:"w-4 h-4 text-blue-600"}),"Real Estate Agents (",(N==null?void 0:N.agent.unclaimed)||0," unclaimed)"]})}),e.jsx(le,{value:"broker",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(te,{className:"w-4 h-4 text-indigo-600"}),"Mortgage Brokers (",(N==null?void 0:N.broker.unclaimed)||0," unclaimed)"]})})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ie,{children:"Email Sequence"}),e.jsxs(Ze,{value:i,onValueChange:b=>v(b),children:[e.jsx(Xe,{children:e.jsx(Je,{})}),e.jsxs(es,{children:[e.jsx(le,{value:"initial",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Os,{className:"w-4 h-4 text-blue-500"}),"Initial Invitation"]})}),e.jsx(le,{value:"reminder_3day",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Se,{className:"w-4 h-4 text-yellow-500"}),"Day 3 Reminder"]})}),e.jsx(le,{value:"reminder_7day",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Se,{className:"w-4 h-4 text-orange-500"}),"Day 7 Reminder"]})}),e.jsx(le,{value:"final_14day",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(be,{className:"w-4 h-4 text-red-500"}),"Day 14 Final Notice"]})})]})]}),e.jsx("p",{className:"text-xs text-stone-500",children:se[i]})]})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-6",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(ie,{children:"Batch Size"}),e.jsx(Ye,{type:"number",min:1,max:100,value:k,onChange:b=>T(parseInt(b.target.value)||25)}),e.jsx("p",{className:"text-xs text-stone-500",children:"Number of emails to send in this batch (max 100)"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(ie,{children:"Test Mode"}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(Rs,{checked:d,onCheckedChange:h}),e.jsx("span",{className:`text-sm ${d?"text-yellow-600 font-medium":"text-stone-500"}`,children:d?"ON - Emails will be sent to [email protected]":"OFF - Emails will be sent to actual users"})]})]})]}),e.jsx(gt,{}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("p",{className:"text-sm font-medium",children:["Ready to send ",k," ",i.replace("_"," ")," emails to ",P,"s"]}),e.jsx("p",{className:"text-xs text-stone-500",children:d?"Test mode - emails go to [email protected]":"Live mode - emails go to actual users"})]}),e.jsx($,{onClick:u,disabled:g,className:"bg-gradient-to-r from-blue-600 to-indigo-600 hover:from-blue-700 hover:to-indigo-700",size:"lg",children:g?e.jsxs(e.Fragment,{children:[e.jsx(fe,{className:"w-4 h-4 mr-2 animate-spin"}),"Sending..."]}):e.jsxs(e.Fragment,{children:[e.jsx(cs,{className:"w-4 h-4 mr-2"}),"Send Invitations"]})})]}),C&&e.jsx(Le,{className:C.success&&C.errors===0?"border-green-200 bg-green-50":"border-yellow-200 bg-yellow-50",children:e.jsxs("div",{className:"flex items-start gap-3",children:[C.success&&C.errors===0?e.jsx(_e,{className:"w-5 h-5 text-green-600"}):e.jsx(be,{className:"w-5 h-5 text-yellow-600"}),e.jsxs("div",{children:[e.jsx(Pe,{className:"font-medium",children:C.message}),e.jsxs("div",{className:"flex gap-4 mt-2 text-sm",children:[e.jsxs("span",{className:"text-green-600",children:["Sent: ",C.sent]}),e.jsxs("span",{className:"text-red-600",children:["Errors: ",C.errors]}),e.jsxs("span",{className:"text-stone-500",children:["Skipped: ",C.skipped]})]})]})]})})]})]}),e.jsxs(m,{children:[e.jsxs(J,{children:[e.jsx(ee,{className:"text-lg",children:"Email Templates"}),e.jsx(de,{children:"Each user type receives a customized email highlighting relevant benefits"})]}),e.jsx(p,{children:e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"p-4 bg-green-50 rounded-lg border border-green-200",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(Ee,{className:"w-5 h-5 text-green-600"}),e.jsx("span",{className:"font-medium text-green-900",children:"Lenders/AEs"})]}),e.jsxs("ul",{className:"text-sm text-green-800 space-y-1",children:[e.jsx("li",{children:"- Promotions get seen (no spam folders)"}),e.jsx("li",{children:"- AI-powered deal matching"}),e.jsx("li",{children:"- Train AI to answer broker questions"}),e.jsx("li",{children:"- ReadySetHome.ai consumer leads"})]})]}),e.jsxs("div",{className:"p-4 bg-orange-50 rounded-lg border border-orange-200",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(Ke,{className:"w-5 h-5 text-orange-600"}),e.jsx("span",{className:"font-medium text-orange-900",children:"Service Providers"})]}),e.jsxs("ul",{className:"text-sm text-orange-800 space-y-1",children:[e.jsx("li",{children:"- Dual referral network (brokers + agents)"}),e.jsx("li",{children:"- AI actively recommends services"}),e.jsx("li",{children:"- Consumer leads from ReadySetHome.ai"}),e.jsx("li",{children:"- Featured listings"})]})]}),e.jsxs("div",{className:"p-4 bg-blue-50 rounded-lg border border-blue-200",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(Fe,{className:"w-5 h-5 text-blue-600"}),e.jsx("span",{className:"font-medium text-blue-900",children:"Real Estate Agents"})]}),e.jsxs("ul",{className:"text-sm text-blue-800 space-y-1",children:[e.jsx("li",{children:"- Connect with vetted mortgage brokers"}),e.jsx("li",{children:"- Group chat for transactions"}),e.jsx("li",{children:"- AI transaction assistant"}),e.jsx("li",{children:"- Financing-ready buyer leads"})]})]}),e.jsxs("div",{className:"p-4 bg-indigo-50 rounded-lg border border-indigo-200",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(te,{className:"w-5 h-5 text-indigo-600"}),e.jsx("span",{className:"font-medium text-indigo-900",children:"Mortgage Brokers"})]}),e.jsxs("ul",{className:"text-sm text-indigo-800 space-y-1",children:[e.jsx("li",{children:"- AI-powered lender matching"}),e.jsx("li",{children:"- Luna AI deal assistant"}),e.jsx("li",{children:"- Agent & vendor network"}),e.jsx("li",{children:"- Consumer leads from ReadySetHome.ai"})]})]})]})})]})]})};async function Mt(){const{count:t}=await S.from("broker_directory").select("*",{count:"exact",head:!0}),{count:c}=await S.from("broker_directory").select("*",{count:"exact",head:!0}).not("nmls_number","is",null),{count:j}=await S.from("broker_directory").select("*",{count:"exact",head:!0}).is("nmls_number",null).or("full_name.ilike.%mortgage%,full_name.ilike.%loan%,full_name.ilike.%lending%,company_name.ilike.%mortgage%,company_name.ilike.%loan%,company_name.ilike.%lending%,full_name.ilike.%home finance%,company_name.ilike.%home finance%");return{total_brokers:t||0,with_nmls:c||0,missing_nmls:(t||0)-(c||0),mortgage_related_missing:j||0}}async function Et(t){try{const{data:c,error:j}=await S.functions.invoke("nmls-lookup",{body:{broker_id:t}});return j?(console.error("NMLS lookup error:",j),{success:!1,error:j.message}):c}catch(c){return console.error("NMLS lookup exception:",c),{success:!1,error:c.message}}}async function Ft(t=50){try{const{data:c,error:j}=await S.functions.invoke("nmls-lookup",{body:{batch_lookup:!0,batch_limit:t}});return j?(console.error("Batch NMLS lookup error:",j),{success:!1,error:j.message}):c}catch(c){return console.error("Batch NMLS lookup exception:",c),{success:!1,error:c.message}}}async function Dt(t=100,c=!0){let j=S.from("broker_directory").select("id, full_name, company_name, city, state, website, email, nmls_lookup_at, nmls_lookup_error").is("nmls_number",null);c&&(j=j.or("full_name.ilike.%mortgage%,full_name.ilike.%loan%,full_name.ilike.%lending%,company_name.ilike.%mortgage%,company_name.ilike.%loan%,company_name.ilike.%lending%,full_name.ilike.%home finance%,company_name.ilike.%home finance%"));const{data:g,count:w}=await j.order("created_at",{ascending:!1}).limit(t);return{brokers:g||[],total:w||0}}async function Ut(t=50){try{const{data:c,error:j}=await S.functions.invoke("nmls-lookup",{body:{fetch_addresses:!0,batch_limit:t}});return j?(console.error("Fetch addresses error:",j),{success:!1,error:j.message}):c}catch(c){return console.error("Fetch addresses exception:",c),{success:!1,error:c.message}}}async function Tt(t,c,j){try{const g={};if(c&&(g.nmls_number=c),j&&(g.company_nmls_number=j),Object.keys(g).length===0)return{success:!1,error:"No NMLS numbers provided"};const{error:w}=await S.from("broker_directory").update(g).eq("id",t);return w?{success:!1,error:w.message}:{success:!0}}catch(g){return{success:!1,error:g.message}}}function It(){const{toast:t}=Ie(),[c,j]=l.useState(null),[g,w]=l.useState([]),[N,n]=l.useState(!0),[C,x]=l.useState(!1),[P,r]=l.useState(!1),[i,v]=l.useState(50),[k,T]=l.useState(!0),[d,h]=l.useState(null),[A,u]=l.useState(""),[y,R]=l.useState(""),[se,b]=l.useState(!1),[z,G]=l.useState(!1),[F,o]=l.useState(!1),[K,Y]=l.useState(null);l.useEffect(()=>{H(),L()},[]);const H=async()=>{n(!0);try{const a=await Mt();j(a)}catch(a){console.error("Failed to load NMLS stats:",a)}finally{n(!1)}},L=async()=>{x(!0);try{const{brokers:a}=await Dt(100,k);w(a)}catch(a){console.error("Failed to load brokers:",a)}finally{x(!1)}},V=async()=>{r(!0);try{const a=await Ft(i);a.success?(t({title:"Batch Lookup Complete",description:`Processed ${a.processed_count} brokers, updated ${a.updated_count} with NMLS numbers.`}),await H(),await L()):t({title:"Batch Lookup Failed",description:a.error||"Unknown error occurred",variant:"destructive"})}catch(a){t({title:"Error",description:a.message||"Batch lookup failed",variant:"destructive"})}finally{r(!1)}},f=async a=>{var ge;try{const re=await Et(a);re.success&&((ge=re.nmls_data)!=null&&ge.nmls_number)?(t({title:"N
8MLS Found",description:`NMLS #${re.nmls_data.nmls_number}${re.nmls_data.company_nmls_number?` (Company: ${re.nmls_data.company_nmls_number})`:""}`}),await L()):t({title:"No NMLS Found",description:re.error||"Could not find NMLS number",variant:"destructive"})}catch(re){t({title:"Lookup Failed",description:re.message,variant:"destructive"})}},Z=a=>{h(a),u(""),R("")},ae=async()=>{G(!0);try{const a=await Ut(i);a.success?(t({title:"Addresses Fetched",description:`Processed ${a.processed_count} brokers, updated ${a.updated_count} with addresses from NMLS.`}),await H()):t({title:"Fetch Failed",description:a.error||"Failed to fetch addresses",variant:"destructive"})}catch(a){t({title:"Error",description:a.message||"Address fetch failed",variant:"destructive"})}finally{G(!1)}},_=async()=>{o(!0);try{const a=await jt(i);a.success?(t({title:"Geocoding Complete",description:`Geocoded ${a.geocoded_count} brokers. ${a.remaining_count||0} remaining.`}),await H()):t({title:"Geocoding Failed",description:a.error||"Failed to geocode brokers",variant:"destructive"})}catch(a){t({title:"Error",description:a.message||"Geocoding failed",variant:"destructive"})}finally{o(!1)}},Q=async()=>{if(d){if(!A&&!y){t({title:"No NMLS Numbers",description:"Please enter at least one NMLS number",variant:"destructive"});return}b(!0);try{const a=await Tt(d.id,A||void 0,y||void 0);a.success?(t({title:"NMLS Updated",description:"NMLS numbers saved successfully"}),h(null),await H(),await L()):t({title:"Update Failed",description:a.error||"Could not save NMLS numbers",variant:"destructive"})}catch(a){t({title:"Error",description:a.message,variant:"destructive"})}finally{b(!1)}}};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-stone-900",children:"NMLS Compliance Management"}),e.jsx("p",{className:"text-stone-600 mt-1",children:"Manage NMLS numbers for broker_directory entries. SAFE Act requires all mortgage professionals to display NMLS numbers."})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-4",children:[e.jsx(m,{children:e.jsx(p,{className:"p-4",children:e.jsx("div",{className:"flex items-center justify-between",children:e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-stone-500",children:"Total Brokers"}),e.jsx("p",{className:"text-2xl font-bold",children:N?"-":c==null?void 0:c.total_brokers.toLocaleString()})]})})})}),e.jsx(m,{children:e.jsx(p,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-stone-500",children:"With NMLS"}),e.jsx("p",{className:"text-2xl font-bold text-green-600",children:N?"-":c==null?void 0:c.with_nmls.toLocaleString()})]}),e.jsx(_e,{className:"w-8 h-8 text-green-500"})]})})}),e.jsx(m,{children:e.jsx(p,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-stone-500",children:"Missing NMLS"}),e.jsx("p",{className:"text-2xl font-bold text-red-600",children:N?"-":c==null?void 0:c.missing_nmls.toLocaleString()})]}),e.jsx(be,{className:"w-8 h-8 text-red-500"})]})})}),e.jsx(m,{children:e.jsx(p,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-stone-500",children:"Mortgage-Related Missing"}),e.jsx("p",{className:"text-2xl font-bold text-orange-600",children:N?"-":c==null?void 0:c.mortgage_related_missing.toLocaleString()})]}),e.jsx(Be,{className:"w-8 h-8 text-orange-500"})]})})})]}),e.jsxs(m,{children:[e.jsxs(J,{children:[e.jsx(ee,{children:"Batch NMLS Lookup"}),e.jsx(de,{children:"Automatically search for NMLS numbers from broker websites and NMLS Consumer Access. Only mortgage-related entries are processed to avoid wasting API calls."})]}),e.jsxs(p,{children:[e.jsxs("div",{className:"flex items-end gap-4",children:[e.jsxs("div",{className:"w-32",children:[e.jsx(ie,{htmlFor:"batchLimit",children:"Batch Size"}),e.jsx(Ye,{id:"batchLimit",type:"number",value:i,onChange:a=>v(parseInt(a.target.value)||50),min:1,max:200})]}),e.jsx($,{onClick:V,disabled:P,children:P?e.jsxs(e.Fragment,{children:[e.jsx(fe,{className:"w-4 h-4 mr-2 animate-spin"}),"Processing..."]}):e.jsxs(e.Fragment,{children:[e.jsx(Be,{className:"w-4 h-4 mr-2"}),"Run Batch Lookup"]})}),e.jsxs($,{variant:"outline",onClick:()=>{H(),L()},children:[e.jsx(pe,{className:"w-4 h-4 mr-2"}),"Refresh"]})]}),P&&e.jsx("p",{className:"text-sm text-stone-500 mt-2",children:"Processing... This may take a few minutes. Rate limited to 1.5 seconds per broker."})]})]}),e.jsxs(m,{children:[e.jsxs(J,{children:[e.jsx(ee,{children:"Address Fetching & Geocoding"}),e.jsx(de,{children:"Fetch addresses from NMLS Consumer Access for brokers that have NMLS numbers but no address data, then geocode them for radius-based search."})]}),e.jsxs(p,{children:[e.jsxs("div",{className:"flex items-end gap-4 flex-wrap",children:[e.jsx($,{onClick:ae,disabled:z||F,variant:"secondary",children:z?e.jsxs(e.Fragment,{children:[e.jsx(fe,{className:"w-4 h-4 mr-2 animate-spin"}),"Fetching Addresses..."]}):e.jsxs(e.Fragment,{children:[e.jsx(Be,{className:"w-4 h-4 mr-2"}),"Fetch Addresses from NMLS"]})}),e.jsx($,{onClick:_,disabled:z||F,children:F?e.jsxs(e.Fragment,{children:[e.jsx(fe,{className:"w-4 h-4 mr-2 animate-spin"}),"Geocoding..."]}):e.jsxs(e.Fragment,{children:[e.jsx(pe,{className:"w-4 h-4 mr-2"}),"Geocode All Brokers"]})})]}),(z||F)&&e.jsx("p",{className:"text-sm text-stone-500 mt-2",children:"Processing... This may take several minutes due to rate limiting."})]})]}),e.jsxs(m,{children:[e.jsx(J,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(ee,{children:"Brokers Without NMLS Numbers"}),e.jsx(de,{children:'Click "Lookup" to search automatically, or "Edit" to enter NMLS numbers manually.'})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ie,{htmlFor:"mortgageOnly",className:"text-sm",children:"Mortgage-related only"}),e.jsx("input",{type:"checkbox",id:"mortgageOnly",checked:k,onChange:a=>{T(a.target.checked),setTimeout(L,0)}})]})]})}),e.jsx(p,{children:C?e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(fe,{className:"w-8 h-8 animate-spin text-stone-400"})}):e.jsxs(De,{children:[e.jsx(Ue,{children:e.jsxs(oe,{children:[e.jsx(U,{children:"Name / Company"}),e.jsx(U,{children:"Location"}),e.jsx(U,{children:"Website"}),e.jsx(U,{children:"Last Lookup"}),e.jsx(U,{className:"text-right",children:"Actions"})]})}),e.jsxs(Te,{children:[g.map(a=>e.jsxs(oe,{children:[e.jsx(E,{children:e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:a.full_name}),a.company_name&&a.company_name!==a.full_name&&e.jsx("p",{className:"text-sm text-stone-500",children:a.company_name})]})}),e.jsx(E,{children:a.city&&a.state?`${a.city}, ${a.state}`:a.state?a.state:e.jsx("span",{className:"text-stone-400",children:"Unknown"})}),e.jsx(E,{children:a.website?e.jsxs("a",{href:gs(a.website),target:"_blank",rel:"noopener noreferrer",className:"text-blue-600 hover:underline flex items-center gap-1",children:[e.jsx(qe,{className:"w-3 h-3"}),"Website"]}):e.jsx("span",{className:"text-stone-400",children:"-"})}),e.jsx(E,{children:a.nmls_lookup_at?e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-stone-500",children:new Date(a.nmls_lookup_at).toLocaleDateString()}),a.nmls_lookup_error&&e.jsx(q,{variant:"destructive",className:"text-xs",children:a.nmls_lookup_error})]}):e.jsx("span",{className:"text-stone-400",children:"Never"})}),e.jsx(E,{className:"text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-2",children:[e.jsxs($,{variant:"outline",size:"sm",onClick:()=>f(a.id),children:[e.jsx(Be,{className:"w-3 h-3 mr-1"}),"Lookup"]}),e.jsxs($,{variant:"outline",size:"sm",onClick:()=>Z(a),children:[e.jsx(ft,{className:"w-3 h-3 mr-1"}),"Edit"]})]})})]},a.id)),g.length===0&&e.jsx(oe,{children:e.jsx(E,{colSpan:5,className:"text-center py-8 text-stone-500",children:k?"No mortgage-related brokers without NMLS found":"All brokers have NMLS numbers"})})]})]})})]}),e.jsx(bs,{open:!!d,onOpenChange:()=>h(null),children:e.jsxs(Ns,{children:[e.jsxs(vs,{children:[e.jsx(ys,{children:"Edit NMLS Numbers"}),e.jsxs(ws,{children:["Enter the NMLS numbers for ",d==null?void 0:d.full_name]})]}),e.jsxs("div",{className:"space-y-4 py-4",children:[e.jsxs("div",{children:[e.jsx(ie,{htmlFor:"personalNmls",children:"Personal N
8MLS Number (MLO)"}),e.jsx(Ye,{id:"personalNmls",placeholder:"e.g., 1234567",value:A,onChange:a=>u(a.target.value.replace(/\D/g,"")),maxLength:7}),e.jsx("p",{className:"text-xs text-stone-500 mt-1",children:"Individual Mortgage Loan Originator number"})]}),e.jsxs("div",{children:[e.jsx(ie,{htmlFor:"companyNmls",children:"Company NMLS Number"}),e.jsx(Ye,{id:"companyNmls",placeholder:"e.g., 891464",value:y,onChange:a=>R(a.target.value.replace(/\D/g,"")),maxLength:7}),e.jsx("p",{className:"text-xs text-stone-500 mt-1",children:"Brokerage/Company number"})]}),e.jsx("div",{className:"bg-blue-50 p-3 rounded-lg",children:e.jsxs("p",{className:"text-sm text-blue-800",children:[e.jsx("strong",{children:"Tip:"})," You can find NMLS numbers on"," ",e.jsx("a",{href:"https://www.nmlsconsumeraccess.org",target:"_blank",rel:"noopener noreferrer",className:"underline",children:"NMLS Consumer Access"})," ","or on the broker's website footer."]})})]}),e.jsxs(_s,{children:[e.jsx($,{variant:"outline",onClick:()=>h(null),children:"Cancel"}),e.jsx($,{onClick:Q,disabled:se,children:se?e.jsxs(e.Fragment,{children:[e.jsx(fe,{className:"w-4 h-4 mr-2 animate-spin"}),"Saving..."]}):"Save NMLS Numbers"})]})]})})]})}const $t={"24h":"Last 24 hours","7d":"Last 7 days","30d":"Last 30 days",all:"All time"},je=t=>t>=1?`$${t.toFixed(2)}`:t>0&&t<.01?`$${t.toFixed(5)}`:`$${t.toFixed(4)}`,ue=t=>t.toLocaleString();function Bt(){const[t,c]=l.useState([]),[j,g]=l.useState(!0),[w,N]=l.useState(null),[n,C]=l.useState("7d");l.useEffect(()=>{let r=!1;return(async()=>{g(!0),N(null);try{let v=S.from("ai_usage_costed").select("*").order("created_at",{ascending:!1}).limit(5e3);if(n!=="all"){const d=n==="24h"?24:n==="7d"?168:720;v=v.gte("created_at",new Date(Date.now()-d*36e5).toISOString())}const{data:k,error:T}=await v;if(r)return;if(T)throw T;c(k??[])}catch(v){r||N(zs(v,"Failed to load usage"))}finally{r||g(!1)}})(),()=>{r=!0}},[n]);const x=l.useMemo(()=>{const r=u=>typeof u=="number"?u:0,i=t.reduce((u,y)=>u+r(y.cost_total),0),v=t.reduce((u,y)=>u+r(y.cost_without_cache),0),k=t.reduce((u,y)=>u+y.cache_read_input_tokens,0),T=t.reduce((u,y)=>u+y.cache_creation_input_tokens,0),d=t.reduce((u,y)=>u+y.input_tokens,0),h=k+T+d,A=new Map;for(const u of t){const y=A.get(u.function_name)??{fn:u.function_name,calls:0,cost:0,uncached:0,cacheRead:0,cacheWrite:0,freshIn:0,out:0,errors:0};y.calls+=1,y.cost+=r(u.cost_total),y.uncached+=r(u.cost_without_cache),y.cacheRead+=u.cache_read_input_tokens,y.cacheWrite+=u.cache_creation_input_tokens,y.freshIn+=u.input_tokens,y.out+=u.output_tokens,u.success||(y.errors+=1),A.set(u.function_name,y)}return{total:i,uncached:v,saved:v-i,calls:t.length,errors:t.filter(u=>!u.success).length,cacheHitRate:h>0?k/h*100:0,avgPerCall:t.length?i/t.length:0,functions:[...A.values()].sort((u,y)=>y.cost-u.cost),users:new Set(t.map(u=>u.user_id).filter(Boolean)).size}},[t]),P=x.functions.filter(r=>r.cacheRead===0&&r.freshIn/Math.max(r.calls,1)>2e3);return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-2xl font-bold text-stone-900",children:"AI Usage & Cost"}),e.jsx("p",{className:"text-sm text-stone-600",children:"Every AI call, what it cost, and what prompt c
8aching is saving you."})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[["24h","7d","30d","all"].map(r=>e.jsx($,{size:"sm",variant:n===r?"default":"outline",onClick:()=>C(r),children:r==="all"?"All":r},r)),e.jsx($,{size:"sm",variant:"outline",onClick:()=>C(r=>r),disabled:j,children:e.jsx(pe,{className:`w-4 h-4 ${j?"animate-spin":""}`})})]})]}),w&&e.jsx(m,{className:"border-red-300 bg-red-50",children:e.jsxs(p,{className:"p-4 text-sm text-red-800",children:["Couldn't load usage: ",w]})}),!j&&!w&&t.length===0&&e.jsx(m,{children:e.jsxs(p,{className:"p-8 text-center text-stone-600",children:[e.jsx(rs,{className:"w-10 h-10 mx-auto mb-3 text-stone-400"}),e.jsxs("p",{className:"font-medium",children:["No AI calls recorded in ",$t[n].toLowerCase(),"."]}),e.jsx("p",{className:"text-sm mt-1",children:"Logging starts from the moment the AI functions were deployed with usage tracking — anything before that isn't here."})]})}),t.length>0&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-4",children:[e.jsxs(m,{children:[e.jsx(J,{className:"pb-2",children:e.jsxs(ee,{className:"text-sm font-medium text-stone-600 flex items-center gap-2",children:[e.jsx(Me,{className:"w-4 h-4"})," Spend"]})}),e.jsxs(p,{children:[e.jsx("div",{className:"text-2xl font-bold",children:je(x.total)}),e.jsxs("p",{className:"text-xs text-stone-500 mt-1",children:[ue(x.calls)," calls · ",je(x.avgPerCall)," avg"]})]})]}),e.jsxs(m,{className:"border-green-200 bg-green-50/50",children:[e.jsx(J,{className:"pb-2",children:e.jsxs(ee,{className:"text-sm font-medium text-green-800 flex items-center gap-2",children:[e.jsx(Vs,{className:"w-4 h-4"})," Saved by caching"]})}),e.jsxs(p,{children:[e.jsx("div",{className:"text-2xl font-bold text-green-700",children:je(x.saved)}),e.jsxs("p",{className:"text-xs text-green-700/80 mt-1",children:["would have been ",je(x.uncached)," uncached"]})]})]}),e.jsxs(m,{children:[e.jsx(J,{className:"pb-2",children:e.jsxs(ee,{className:"text-sm font-medium text-stone-600 flex items-center gap-2",children:[e.jsx(we,{className:"w-4 h-4"})," Cache hit rate"]})}),e.jsxs(p,{children:[e.jsxs("div",{className:"text-2xl font-bold",children:[x.cacheHitRate.toFixed(1),"%"]}),e.jsx("p",{className:"text-xs text-stone-500 mt-1",children:"of all input tokens served from cache"})]})]}),e.jsxs(m,{children:[e.jsx(J,{className:"pb-2",children:e.jsx(ee,{className:"text-sm font-medium text-stone-600",children:"Users"})}),e.jsxs(p,{children:[e.jsx("div",{className:"text-2xl font-bold",children:ue(x.users)}),e.jsx("p",{className:"text-xs text-stone-500 mt-1",children:x.errors>0?`${x.errors} failed calls`:"no failed calls"})]})]})]}),P.length>0&&e.jsx(m,{className:"border-amber-300 bg-amber-50",children:e.jsxs(p,{className:"p-4 flex gap-3",children:[e.jsx(be,{className:"w-5 h-5 text-amber-600 shrink-0 mt-0.5"}),e.jsxs("div",{className:"text-sm text-amber-900",children:[e.jsxs("p",{className:"font-semibold",children:[P.length===1?"One function is":`${P.length} functions are`," sending large prompts with no caching"]}),e.jsxs("p",{className:"mt-1",children:[P.map(r=>r.fn).join(", ")," — averaging"," ",ue(Math.round(P[0].freshIn/Math.max(P[0].calls,1)))," uncached input tokens per call. Adding a ",e.jsx("code",{children:"cache_control"})," breakpoint typically cuts this by 80–90%."]})]})]})}),e.jsxs(m,{children:[e.jsx(J,{children:e.jsx(ee,{className:"text-lg",children:"Cost by function"})}),e.jsx(p,{className:"p-0",children:e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-stone-50 text-stone-600",children:e.jsxs("tr",{children:[e.jsx("th",{className:"text-left px-4 py-2 font-medium",children:"Function"}),e.jsx("th",{className:"text-right px-4 py-2 font-medium",children:"Calls"}),e.jsx("th",{className:"text-right px-4 py-2 font-medium",children:"Cost"}),e.jsx("th",{className:"text-right px-4 py-2 font-medium",children:"Per call"}),e.jsx("th",{className:"text-right px-4 py-2 font-medium",children:"Cached in"}),e.jsx("th",{className:"text-right px-4 py-2 font-medium",children:"Uncached in"}),e.jsx("th",{className:"text-right px-4 py-2 font-medium",children:"Saved"})]})}),e.jsx("tbody",{children:x.functions.map(r=>{const i=r.uncached-r.cost,v=r.cacheRead===0&&r.freshIn/Math.max(r.calls,1)>2e3;return e.jsxs("tr",{className:"border-t",children:[e.jsxs("td",{className:"px-4 py-2 font-medium text-stone-900",children:[r.fn,v&&e.jsx(q,{variant:"outline",className:"ml-2 border-amber-400 text-amber-700",children:"no cache"}),r.errors>0&&e.jsxs(q,{variant:"outline",className:"ml-2 border-red-400 text-red-700",children:[r.errors," failed"]})]}),e.jsx("td",{className:"px-4 py-2 text-right",children:ue(r.calls)}),e.jsx("td",{className:"px-4 py-2 text-right font-medium",children:je(r.cost)}),e.jsx("td",{className:"px-4 py-2 text-right text-stone-600",children:je(r.cost/Math.max(r.calls,1))}),e.jsx("td",{className:"px-4 py-2 text-right text-stone-600",children:ue(r.cacheRead)}),e.jsx("td",{className:"px-4 py-2 text-right text-stone-600",children:ue(r.fresh
8In)}),e.jsx("td",{className:`px-4 py-2 text-right ${i>0?"text-green-700":"text-stone-400"}`,children:i>0?je(i):"—"})]},r.fn)})})]})})})]}),e.jsxs(m,{children:[e.jsx(J,{children:e.jsx(ee,{className:"text-lg",children:"Recent calls"})}),e.jsx(p,{className:"p-0",children:e.jsx("div",{className:"overflow-x-auto max-h-96 overflow-y-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-stone-50 text-stone-600 sticky top-0",children:e.jsxs("tr",{children:[e.jsx("th",{className:"text-left px-4 py-2 font-medium",children:"When"}),e.jsx("th",{className:"text-left px-4 py-2 font-medium",children:"Function"}),e.jsx("th",{className:"text-right px-4 py-2 font-medium",children:"In"}),e.jsx("th",{className:"text-right px-4 py-2 font-medium",children:"Cache r/w"}),e.jsx("th",{className:"text-right px-4 py-2 font-medium",children:"Out"}),e.jsx("th",{className:"text-right px-4 py-2 font-medium",children:"Cost"})]})}),e.jsx("tbody",{children:t.slice(0,200).map(r=>e.jsxs("tr",{className:"border-t",children:[e.jsx("td",{className:"px-4 py-2 text-stone-600 whitespace-nowrap",children:new Date(r.created_at).toLocaleString()}),e.jsxs("td",{className:"px-4 py-2",children:[r.function_name,!r.success&&e.jsx(q,{variant:"outline",className:"ml-2 border-red-400 text-red-700",children:"failed"})]}),e.jsx("td",{className:"px-4 py-2 text-right",children:ue(r.input_tokens)}),e.jsxs("td",{className:"px-4 py-2 text-right text-stone-600",children:[ue(r.cache_read_input_tokens)," / ",ue(r.cache_creation_input_tokens)]}),e.jsx("td",{className:"px-4 py-2 text-right",children:ue(r.output_tokens)}),e.jsx("td",{className:"px-4 py-2 text-right",children:je(r.cost_total??0)})]},r.id))})]})})})]}),e.jsxs("p",{className:"text-xs text-stone-500",children:["Costs are computed from ",e.jsx("code",{children:"ai_model_pricing"})," at Anthropic list rates: a cache write bills at 1.25x input and a cache read at ~0.1x. A cache write therefore costs ",e.jsx("em",{children:"more"})," than not caching — caching pays from the second call onward, so a one-question-and-leave user is a small net loss."]})]})]})}const Ot={brokerage:"Brokerage details",logo:"Logo",offices:"Offices",staff:"Staff and access",commission:"Commission defaults",paid:"How agents get paid",cdarelease:"Who may issue a CDA",cdainfo:"CDA information",docs:"Documents to sign",seats:"Seats",agents:"Agents",access:"Seats granted",profile:"Profile",bio:"Bio",paperwork:"Paperwork",client:"First client",sponsor:"Sponsored a buyer",photo:"Photo",pricing:"Pricing",calendar:"Calendar sync"},Vt=["all","brokerage","agent","coordinator"],zt=()=>{const[t,c]=l.useState(null),[j,g]=l.useState(null),[w,N]=l.useState("all"),[n,C]=l.useState(!1),x=async()=>{C(!0);const{data:i,error:v}=await S.rpc("onboarding_progress");v?(g(v.message),c([])):(g(null),c(i??[])),C(!1)};l.useEffect(()=>{x()},[]);const P=l.useMemo(()=>[...(t??[]).filter(v=>w==="all"||v.kind===w)].sort((v,k)=>{const T=v.steps_done/Math.max(v.steps_total,1),d=k.steps_done/Math.max(k.steps_total,1);return T!==d?T-d:(k.days_since??0)-(v.days_since??0)}),[t,w]),r=l.useMemo(()=>{const i=t??[],v=i.filter(k=>k.steps_done>=k.steps_total).length;return{total:i.length,done:v,stuck:i.length-v}},[t]);return t===null?e.jsx("div",{className:"p-6 text-stone-500",children:"Loading…"}):e.jsxs("div",{className:"p-6 space-y-4",children:[e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-xl font-bold text-stone-900",children:"Onboarding"}),e.jsxs("p",{className:"text-sm text-stone-600",children:[r.done," of ",r.total," accounts have finished setting up",r.stuck>0&&e.jsxs(e.Fragment,{children:[" · ",e.jsx("strong",{children:r.stuck})," still owe something"]})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[Vt.map(i=>e.jsx("button",{onClick:()=>N(i),className:`px-3 py-1.5 rounded-lg text-sm capitalize ${w===i?"bg-stone-900 text-white":"bg-stone-100 text-stone-700 hover:bg-stone-200"}`,children:i},i)),e.jsxs($,{variant:"outline",size:"sm",onClick:x,disabled:n,children:[e.jsx(pe,{className:`w-4 h-4 mr-1.5 ${n?"animate-spin":""}`}),"Refresh"]})]})]}),j&&e.jsxs("div",{className:"flex items-start gap-2 rounded-lg border border-amber-300 bg-amber-50 p-3 text-sm text-amber-900",children:[e.jsx(be,{className:"w-4 h-4 mt-0.5 flex-shrink-0"}),e.jsx("span",{children:j})]}),e.jsxs("div",{className:"space-y-2",children:[P.map(i=>{var T,d,h;const v=Math.round(i.steps_done/Math.max(i.steps_total,1)*100),k=i.steps_done>=i.steps_total;return e.jsx(m,{className:"border-stone-200",children:e.jsxs(p,{className:"p-4",children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-x-3 gap-y-1",children:[e.jsx("span",{className:"text-[11px] uppercase tracking-wide font-semibold text-stone-500",children:i.kind}),e.jsx("span",{className:"font-semibold text-stone-900",children:((T=i.name)==null?void 0:T.trim())||i.email||"(no name)"}),i.email&&((d=i.name)==null?void 0:d.trim())&&e.jsx("span",{className:"text-sm text-stone-500",children:i.email}),e.jsxs("span",{className:"ml-auto flex items-center gap-2 text-sm",children:[k?e.jsxs("span",{className:"flex items-center gap-1 text-emerald-700 font-medium",children:[e.jsx(We,{className:"w-4 h-4"})," Finished"]}):e.jsxs("span",{className:"text-stone-700 tabular-nums",children:[i.steps_done," of ",i.steps_total]}),i.days_since!=null&&e.jsxs("span",{className:"text-stone-400 tabular-nums",children:["· ",i.days_since,"d"]})]})]}),e.jsx("div",{className:"mt-2 h-1.5 rounded-full bg-stone-100 overflow-hidden",children:e.jsx("div",{className:`h-full rounded-full ${k?"bg-emerald-500":"bg-amber-500"}`,style:{width:`${v}%`}})}),!k&&(((h=i.outstanding)==null?void 0:h.length)??0)>0&&e.jsxs("p",{className:"mt-2 text-sm text-stone-600",children:["Still to do:"," ",(i.outstanding??[]).map(A=>
8Ot[A]??A).join(", ")]})]})},`${i.kind}-${i.account_id}`)}),P.length===0&&!j&&e.jsx("p",{className:"text-sm text-stone-500",children:"Nothing here."})]})]})};function Ma(){var o,K,Y,H,L,V;const{user:t,realUser:c,profile:j,realProfile:g,signOut:w}=fs(),N=Ps(),{toast:n}=Ie(),[C,x]=l.useState(!1),[P,r]=l.useState("users"),[i,v]=l.useState("all"),[k,T]=l.useState({all:0,brokers:0,lenders:0,agents:0,vendors:0,clients:0}),[d,h]=l.useState(0),[A,u]=l.useState(!1),[y,R]=l.useState(null);l.useEffect(()=>{const f=async()=>{try{const{count:ae,error:_}=await S.from("admin_support_tickets").select("*",{count:"exact",head:!0}).eq("status","open");if(_){h(0);return}h(ae||0)}catch{h(0)}};f();const Z=S.channel("admin-tickets-count").on("postgres_changes",{event:"*",schema:"public",table:"support_tickets"},()=>{f()}).subscribe();return()=>{S.removeChannel(Z)}},[]),l.useEffect(()=>{const f=localStorage.getItem("adminMimicData");u(!!f)},[]),l.useEffect(()=>{(async()=>{const Z=(c==null?void 0:c.id)??(t==null?void 0:t.id);if(!Z)return;const{data:ae,error:_}=await S.from("admin_users").select("admin_level").eq("user_id",Z).maybeSingle();R(!_&&ae?ae.admin_level:"support_admin")})()},[c==null?void 0:c.id,t==null?void 0:t.id]);const se=y==="super_admin",b=async()=>{try{await S.auth.signOut(),n({title:"Signed out successfully",description:"You have been logged out."}),N("/")}catch(f){console.error("Error signing out:",f),n({title:"Error",description:"Failed to sign out. Please try again.",variant:"destructive"})}},z=async()=>{try{const{error:f}=await S.auth.reauthenticate();if(f)throw f;n({title:"Reauthentication Successful",description:"Your session has been refreshed."})}catch(f){console.error("Reauthentication error:",f),n({title:"Reauthentication Failed",description:"Please try signing out and signing back in.",variant:"destructive"})}},G=[{id:"new-signups",label:"New Signups",icon:ns,description:"Both platforms"},{id:"onboarding",label:"Onboarding",icon:bt,description:"Who is still setting up"},{id:"users",label:"Users",icon:te,description:"Platform Users",badge:k.all>0?k.all:void 0},{id:"support",label:"Support",icon:qs,description:"Support Tickets",badge:d>0?d:void 0,badgeColor:"bg-red-600"},{id:"knowledge-base",label:"Knowledge Base",icon:Nt,description:"KB Management"},{id:"admin-users",label:"Admin Users",icon:Gs,description:"Admin Access"},{id:"beta-testers",label:"Beta Testers",icon:os,description:"Test Accounts"},{id:"promotions",label:"Promotions",icon:He,description:"Review Queue"},{id:"claim-invitations",label:"Claim Emails",icon:Ss,description:"Invite Users"},{id:"nmls",label:"NMLS Compliance",icon:vt,description:"NMLS Numbers"},...se?[{id:"revenue",label:"Revenue",icon:Me,description:"Analytics & Reports",superAdminOnly:!0}]:[],...se?[{id:"ai-usage",label:"AI Usage & Cost",icon:yt,description:"Tokens, spend, cache savings",superAdminOnly:!0}]:[],{id:"social-workspaces",label:"Social Media",icon:wt,description:"Link posting workspaces"},...se?[{id:"settings",label:"Settings",icon:ds,description:"System Settings",superAdminOnly:!0}]:[]],F=()=>{switch(P){case"new-signups":return e.jsx(dt,{});case"onboarding":return e.jsx(zt,{});case"users":return e.jsxs("div",{className:"space-y-4",children:[e.jsx("div",{className:"bg-gradient-to-r from-stone-50 to-stone-100 border border-stone-200 px-4 py-3 rounded-lg",children:e.jsxs("div",{className:"flex items-center justify-center gap-3",children:[e.jsx("span",{className:"text-xs text-stone-500 font-medium uppercase tracking-wider",children:"Filter by:"}),e.jsxs("div",{className:"flex flex-wrap gap-2 justify-center",children:[e.jsxs("button",{onClick:()=>v("all"),className:`relative inline-flex items-center gap-1.5 px-3 py-1.5 rounded-full text-xs font-medium transition-all duration-200 ${i==="all"?"bg-gradient-to-r from-red-600 to-red-700 text-white shadow-sm":"bg-white text-stone-600 hover:bg-stone-100 border border-stone-300"}`,children:[e.jsx(te,{className:"w-3 h-3"}),e.jsxs("span",{children:["All (",k.all,")"]})]}),e.jsxs("button",{onClick:()=>v("brokers"),className:`relative inline-flex items-center gap-1.5 px-3 py-1.5 rounded-full text-xs font-medium transition-all duration-200 ${i==="brokers"?"bg-gradient-to-r from-red-600 to-red-700 text-white shadow-sm":"bg-white text-stone-600 hover:bg-stone-100 border border-stone-300"}`,children:[e.jsx(te,{className:"w-3 h-3"}),e.jsxs("span",{children:["Brokers/LOs (",k.brokers,")"]})]}),e.jsxs("button",{onClick:()=>v("lenders"),className:`relative inline-flex items-center gap-1.5 px-3 py-1.5 rounded-full text-xs font-medium transition-all duration-200 ${i==="lenders"?"bg-gradient-to-r from-red-600 to-red-700 text-white shadow-sm":"bg-white text-stone-600 hover:bg-stone-100 border border-stone-300"}`,children:[e.jsx(Ee,{className:"w-3 h-3"}),e.jsxs("span",{children:["Lenders/AEs (",k.lenders,")"]})]}),e.jsxs("button",{onClick:()=>v("agents"),className:`relative inline-flex items-center gap-1.5 px-3 py-1.5 rounded-full text-xs font-medium transition-all duration-200 ${i==="agents"?"bg-gradient-to-r from-red-600 to-red-700 text-white shadow-sm":"bg-white text-stone-600 hover:bg-stone-100 border border-stone-300"}`,children:[e.jsx(os,{className:"w-3 h-3"}),e.jsxs("span",{children:["Agents (",k.agents,")"]})]}),e.jsxs("button",{onClick:()=>v("vendors"),className:`relative inline-flex items-center gap-1.5 px-3 py-1.5 rounded-full text-xs font-medium transition-all duration-200 ${i==="vendors"?"bg-gradient-to-r from-red-600 to-red-700 text-white shadow-sm":"bg-white text-stone-600 hover:bg-stone-100 border border-stone-300"}`,children:[e.jsx(ct,{className:"w-3 h-3"}),e.jsxs("span",{children:["Vendors (",k.vendors,")"]})]}),e.jsxs("button",{onClick:()=>v("clients"),className:`relative inline-flex items-center gap-1.5 px-3 py-1.5 rounded-full text-xs font-medium transition-all duration-200 ${i==="clients"?"bg-gradient-to-r from-red-600 to-red-700 text-white shadow-sm":"bg-white text-stone-600 hover:bg-stone-100 border border-stone-300"}`,children:[e.jsx(te,{className:"w-3 h-3"}),e.jsxs("span",{children:["Clients (",k.clients,")"]})]})]})]})}),e.jsx(ot,{activeSubTab:i,onCountsUpdate:T})]});
8case"support":return e.jsx(it,{});case"knowledge-base":return e.jsx(lt,{});case"admin-users":return e.jsx(rt,{});case"beta-testers":return e.jsx(nt,{});case"promotions":return e.jsx(Lt,{});case"claim-invitations":return e.jsx(Pt,{});case"nmls":return e.jsx(It,{});case"revenue":return e.jsx(kt,{});case"social-workspaces":return e.jsx(at,{});case"ai-usage":return e.jsx(Bt,{});case"settings":return e.jsx(At,{});default:return null}};return e.jsxs("div",{className:"flex h-screen bg-stone-900 overflow-hidden",children:[e.jsxs("div",{className:`${C?"w-16":"w-64"} bg-stone-800 border-r border-stone-700 flex flex-col transition-all duration-300 relative`,children:[e.jsx("div",{className:"p-4 border-b border-stone-700",children:e.jsxs("div",{className:"flex items-center justify-between",children:[C?e.jsx("div",{className:"w-full flex justify-center",children:e.jsx("img",{src:"/lendmatchpro-180.png",alt:"LendMatchPro",className:"w-8 h-8 rounded-lg"})}):e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("img",{src:"/lendmatchpro-180.png",alt:"LendMatchPro",className:"w-10 h-10 rounded-lg"}),e.jsxs("div",{children:[e.jsx("h2",{className:"text-base font-bold text-white",children:"LendMatchPro.ai"}),e.jsx("p",{className:"text-xs text-stone-400",children:"Admin Dashboard"})]})]}),e.jsx("button",{onClick:()=>x(!C),className:"p-1.5 rounded-lg hover:bg-stone-700 text-stone-400 hover:text-white transition-colors ml-auto",children:C?e.jsx(Hs,{className:"w-5 h-5"}):e.jsx(_t,{className:"w-5 h-5"})})]})}),A&&e.jsx("div",{className:"px-3 py-2 bg-yellow-900/50 border-b border-yellow-700",children:C?e.jsx("div",{className:"text-center text-yellow-200",children:"🎭"}):e.jsx("div",{className:"text-xs text-yellow-200 font-medium",children:"🎭 Mimic Mode Active"})}),y&&e.jsx("div",{className:`px-3 py-2 border-b border-stone-700 ${se?"bg-amber-900/30":"bg-blue-900/30"}`,children:C?e.jsx("div",{className:"text-center",children:se?"👑":"🛠️"}):e.jsx("div",{className:`text-xs font-medium ${se?"text-amber-300":"text-blue-300"}`,children:se?"👑 Super Admin":"🛠️ Support Admin"})}),e.jsx("nav",{className:"flex-1 overflow-y-auto py-4",children:e.jsx("div",{className:"space-y-1 px-2",children:G.map(f=>{const Z=f.icon,ae=P===f.id;return e.jsxs("button",{onClick:()=>r(f.id),className:`w-full flex items-center gap-3 px-3 py-2.5 rounded-lg transition-all duration-200 group relative ${ae?"bg-red-600 text-white shadow-lg shadow-red-600/50":"text-stone-300 hover:bg-stone-700 hover:text-white"}`,children:[e.jsx(Z,{className:`w-5 h-5 flex-shrink-0 ${ae?"text-white":"text-stone-400 group-hover:text-white"}`}),!C&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex-1 text-left",children:[e.jsx("div",{className:"text-sm font-medium",children:f.label}),e.jsx("div",{className:"text-xs opacity-70",children:f.description})]}),f.badge!==void 0&&e.jsx("span",{className:`${f.badgeColor||"bg-red-600"} text-white text-xs font-bold px-2 py-0.5 rounded-full`,children:f.badge})]}),C&&f.badge!==void 0&&e.jsx("span",{className:`absolute -top-1 -right-1 ${f.badgeColor||"bg-red-600"} text-white text-xs font-bold w-5 h-5 rounded-full flex items-center justify-center`,children:f.badge>99?"99+":f.badge})]},f.id)})})}),e.jsx("div",{className:"border-t border-stone-700 p-3",children:e.jsxs(Xs,{children:[e.jsx(Js,{asChild:!0,children:e.jsxs("button",{className:`w-full flex items-center gap-3 p-2 rounded-lg hover:bg-stone-700 transition-colors ${C?"justify-center":""}`,children:[e.jsxs(Ws,{className:"w-8 h-8 border-2 border-red-600",children:[e.jsx(Ks,{src:(g==null?void 0:g.avatar_url)||void 0}),e.jsx(Ys,{className:"bg-red-600 text-white text-xs",children:((o=g==null?void 0:g.full_name)==null?void 0:o[0])||((Y=(K=g==null?void 0:g.email)==null?void 0:K[0])==null?void 0:Y.toUpperCase())||"A"})]}),!C&&e.jsxs("div",{className:"flex-1 text-left overflow-hidden min-w-0",children:[e.jsx("div",{className:"text-sm font-medium text-white truncate",children:(g==null?void 0:g.full_name)||((H=g==null?void 0:g.email)==null?void 0:H.split("@")[0])||"Account"}),e.jsx("div",{className:"text-xs text-stone-400 truncate",children:g==null?void 0:g.email})]})]})}),e.jsxs(et,{align:"end",className:"w-56 bg-stone-800 border-stone-700",children:[e.jsxs(Oe,{onClick:()=>N("/admin/settings"),className:"text-stone-300 hover:text-white hover:bg-stone-700",children:[e.jsx(ds,{className:"w-4 h-4 mr-2"}),"Settings"]}),e.jsxs(Oe,{onClick:()=>N("/admin/settings"),className:"text-stone-300 hover:text-white hover:bg-stone-700",children:[e.jsx(St,{className:"w-4 h-4 mr-2"}),"Edit Profile"]}),e.jsxs(Oe,{onClick:z,className:"text-stone-300 hover:text-white hover:bg-stone-700",children:[e.jsx(Qs,{className:"w-4 h-4 mr-2"}),"Reauthenticate"]}),e.jsx(st,{className:"bg-stone-700"}),e.jsxs(Oe,{onClick:b,className:"text-red-400 hover:text-red-300 hover:bg-stone-700",children:[e.jsx(Zs,{className:"w-4 h-4 mr-2"}),"Sign Out"]})]})]})})]}),e.jsxs("div",{className:"flex-1 flex flex-col overflow-hidden min-w-0",children:[e.jsx("div",{className:"bg-stone-800 border-b border-stone-700 px-6 py-4",children:e.jsx("div",{className:"flex items-center justify-between",children:e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-white",children:(L=G.find(f=>f.id===P))==null?void 0:L.label}),e.jsx("p",{className:"text-sm text-stone-400 mt-1",children:(V=G.find(f=>f.id===P))==null?void 0:V.description})]})})}),e.jsxs("div",{className:"flex-1 overflow-y-auto bg-stone-900 p-6 min-w-0",children:[e.jsx("div",{className:"mb-6",children:P==="support"&&e.jsx(tt,{})}),e.jsx("div",{className:"max-w-7xl mx-auto",children:F()})]})]})]})}export{Ma as default};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.