1import{c as $,a as X,r as x,s as j,j as e,L as F,S as D}from"./main-Cc8MXM0b.js";import{C as Y}from"./chevron-left-wJAKe9ZS.js";import{A as ee}from"./activity-BEr9c_Bk.js";import{U as se}from"./users-CyG-goHJ.js";import{Z as te}from"./zap-7Ts5Iffc.js";import{T as ae}from"./trending-up-Vu4TyrOf.js";import{T as oe}from"./trending-down-EKw4zw5y.js";import{C as re}from"./chart-column-CQ5Ki0Rd.js";import{A as ie}from"./arrow-right-CV1TWFR8.js";const ne=[["path",{d:"M3 3v16a2 2 0 0 0 2 2h16",key:"c24i48"}],["path",{d:"m19 9-5 5-4-4-3 3",key:"2osh9i"}]],le=$("chart-line",ne);const ce=[["line",{x1:"12",x2:"12",y1:"2",y2:"22",key:"7eqyqh"}],["path",{d:"M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6",key:"1b0p4s"}]],de=$("dollar-sign",ce);const me=[["path",{d:"M10 8h4",key:"1sr2af"}],["path",{d:"M12 21v-9",key:"17s77i"}],["path",{d:"M12 8V3",key:"13r4qs"}],["path",{d:"M17 16h4",key:"h1uq16"}],["path",{d:"M19 12V3",key:"o1uvq1"}],["path",{d:"M19 21v-5",key:"qua636"}],["path",{d:"M3 14h4",key:"bcjad9"}],["path",{d:"M5 10V3",key:"cb8scm"}],["path",{d:"M5 21v-7",key:"1w1uti"}]],xe=$("sliders-vertical",me),E={start:19.9,cat:19.9,pro:39.9,professor:39.9,premium:69.9,master:69.9};function ue(t){if(t.payment_amount&&t.payment_amount>0)return Number(t.payment_amount);const a=(t.plan_name||"").toLowerCase();for(const r of Object.keys(E))if(a.includes(r))return E[r];return 0}const N=t=>{const a=Math.abs(t),r=a>0&&a<.01?6:a>0&&a<1?4:2;return t.toLocaleString("pt-BR",{style:"currency",currency:"BRL",maximumFractionDigits:r})};function ve(){const t=X(),[a,r]=x.useState(!0),[c,u]=x.useState(!1),[d,V]=x.useState([]),[P,O]=x.useState([]),[H,z]=x.useState({}),[y,G]=x.useState(!0);x.useEffect(()=>{(async()=>{const{data:{user:i}}=await j.auth.getUser();if(!i){t({to:"/login"});return}const{data:n}=await j.from("profiles").select("is_admin").eq("id",i.id).maybeSingle();if(!n?.is_admin){t({to:"/app"});return}u(!0),r(!1);const p=new Date(Date.now()-30*864e5).toISOString(),[{data:w},{data:M}]=await Promise.all([j.from("ai_usage").select("created_at,user_id,tool_key,model,total_tokens,cost_brl,cost_usd,status").gte("created_at",p).order("created_at",{ascending:!1}).limit(5e3),j.from("user_subscriptions").select("user_id,plan_name,payment_status,payment_amount")]);V(w||[]),O(M||[]);const v=Array.from(new Set((w||[]).map(f=>f.user_id).filter(Boolean)));if(v.length){const{data:f}=await j.from("profiles").select("id,name,email").in("id",v),h={};(f||[]).forEach(m=>{h[m.id]={name:m.name,email:m.email}}),z(h)}G(!1)})()},[t]);const o=x.useMemo(()=>{const i=new Date;i.setHours(0,0,0,0);const n=i.toISOString(),p=d.filter(s=>s.created_at>=n).reduce((s,l)=>s+Number(l.cost_brl||0),0),w=d.reduce((s,l)=>s+Number(l.cost_brl||0),0),M=d.reduce((s,l)=>s+Number(l.total_tokens||0),0),v=d.filter(s=>s.status&&s.status!=="success").length,f=new Set(d.map(s=>s.user_id).filter(Boolean)).size,h={},m={},b={};for(const s of d){const l=s.model||"â",g=s.tool_key||"â",A=s.user_id||"â",L=Number(s.cost_brl||0);h[l]??={calls:0,cost:0},h[l].calls++,h[l].cost+=L,m[g]??={calls:0,cost:0},m[g].calls++,m[g].cost+=L,b[A]??={calls:0,cost:0},b[A].calls++,b[A].cost+=L}const B=P.filter(s=>s.payment_status==="confirmed"),k=B.reduce((s,l)=>s+ue(l),0),J=B.reduce((s,l)=>s+(b[l.user_id]?.cost||0),0),q=k-J,W=k>0?q/k:0,C=s=>Object.entries(s).sort((l,g)=>g[1].cost-l[1].cost);return{spendToday:p,spend30:w,tokens30:M,errors:v,activeUsers:f,revenue:k,profit:q,margin:W,topModels:C(h).slice(0,5),topTools:C(m).slice(0,5),topUsers:C(b).slice(0,10)}},[d,P]);if(a)return e.jsx("div",{className:"min-h-screen flex items-center justify-center bg-slate-950 text-slate-300",children:"Carregandoâ¦"});if(!c)return null;const K=o.topModels[0]?.[1].cost||1,Z=o.topTools[0]?.[1].cost||1,Q=o.topUsers[0]?.[1].cost||1;return e.jsxs("div",{className:"min-h-screen bg-gradient-to-br from-slate-950 via-slate-900 to-indigo-950 text-slate-100 pb-20",children:[e.jsx("header",{className:"sticky top-0 z-20 backdrop-blur-xl bg-slate-950/70 border-b border-white/5",children:e.jsxs("div",{className:"max-w-7xl mx-auto px-6 py-4 flex items-center justify-between",children:[e.jsxs(F,{to:"/adm",className:"flex items-center gap-2 text-sm text-slate-400 hover:text-white transition",children:[e.jsx(Y,{className:"w-4 h-4"})," Voltar ao Admin"]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(D,{className:"w-5 h-5 text-indigo-400"}),e.jsx("h1",{className:"text-lg font-bold tracking-tight",children:"Central IA"})]}),e.jsx("div",{className:"w-24"})]})}),e.jsxs("div",{className:"max-w-7xl mx-auto px-6 pt-10 space-y-10",children:[e.jsxs("section",{children:[e.jsx("p",{className:"text-xs font-semibold uppercase tracking-widest text-indigo-400",children:"Ãltimos 30 dias"}),e.jsx("h2",{className:"text-4xl md:text-5xl font-black mt-2 bg-gradient-to-r from-white via-slate-200 to-indigo-300 bg-clip-text text-transparent",children:"Visão consolidada de IA"}),e.jsx("p",{className:"text-slate-400 mt-3 max-w-2xl",children:"Custos, receita, margem, modelos e usuários â em uma única tela."})]}),e.jsxs("section",{className:"grid grid-cols-2 lg:grid-cols-4 gap-4",children:[e.jsx(_,{icon:e.jsx(de,{className:"w-5 h-5"}),label:"Gasto hoje",value:N(o.spendToday),tone:"indigo",loading:y}),e.jsx(_,{icon:e.jsx(ee,{className:"w-5 h-5"}),label:"Gasto 30 dias",value:N(o.spend30),tone:"cyan",loading:y}),e.jsx(_,{icon:e.jsx(se,{className:"w-5 h-5"}
1),label:"Usuários ativos",value:o.activeUsers.toLocaleString("pt-BR"),tone:"fuchsia",loading:y}),e.jsx(_,{icon:e.jsx(te,{className:"w-5 h-5"}),label:"Tokens (30d)",value:o.tokens30.toLocaleString("pt-BR"),tone:"amber",loading:y})]}),e.jsxs("section",{className:"grid md:grid-cols-3 gap-4",children:[e.jsx(T,{label:"Receita confirmada",value:N(o.revenue),tone:"emerald"}),e.jsx(T,{label:"Lucro (Receita â IA)",value:N(o.profit),tone:o.profit>=0?"emerald":"rose",icon:o.profit>=0?e.jsx(ae,{className:"w-4 h-4"}):e.jsx(oe,{className:"w-4 h-4"})}),e.jsx(T,{label:"Margem",value:`${(o.margin*100).toFixed(1)}%`,tone:o.margin>=.3?"emerald":o.margin>=0?"amber":"rose"})]}),e.jsxs("section",{className:"grid lg:grid-cols-2 gap-6",children:[e.jsxs(U,{title:"Top modelos",subtitle:"Por custo (R$)",children:[o.topModels.map(([i,n])=>e.jsx(R,{label:i,value:n.cost,sub:`${n.calls} chamadas`,pct:n.cost/K,color:"from-indigo-500 to-fuchsia-500"},i)),!o.topModels.length&&e.jsx(I,{})]}),e.jsxs(U,{title:"Top ferramentas",subtitle:"Por custo (R$)",children:[o.topTools.map(([i,n])=>e.jsx(R,{label:i,value:n.cost,sub:`${n.calls} chamadas`,pct:n.cost/Z,color:"from-cyan-500 to-emerald-400"},i)),!o.topTools.length&&e.jsx(I,{})]})]}),e.jsx("section",{children:e.jsxs(U,{title:"Top 10 usuários (custo IA)",subtitle:"Ãltimos 30 dias",children:[o.topUsers.map(([i,n])=>{const p=H[i];return e.jsx(R,{label:p?.name||p?.email||i.slice(0,8),sub:p?.email||`${n.calls} chamadas`,value:n.cost,pct:n.cost/Q,color:"from-amber-500 to-rose-500"},i)}),!o.topUsers.length&&e.jsx(I,{})]})}),e.jsxs("section",{className:"grid md:grid-cols-2 lg:grid-cols-4 gap-4",children:[e.jsx(S,{to:"/adm-consultar-ia",icon:e.jsx(D,{className:"w-5 h-5"}),title:"Consultar IA",desc:"Pergunte ao seu negócio. SQL primeiro, IA só quando precisa."}),e.jsx(S,{to:"/adm-monitor-ia",icon:e.jsx(re,{className:"w-5 h-5"}),title:"Monitor detalhado",desc:"Rankings, filtros por perÃodo e últimas 50 chamadas."}),e.jsx(S,{to:"/adm-financeiro-ia",icon:e.jsx(le,{className:"w-5 h-5"}),title:"Financeiro por plano",desc:"Receita à custo real por plano, melhores e piores clientes."}),e.jsx(S,{to:"/adm-simulador",icon:e.jsx(xe,{className:"w-5 h-5"}),title:"Simulador",desc:"Projete lucro, margem e break-even ajustando preço e modelo."})]})]})]})}function _({icon:t,label:a,value:r,tone:c,loading:u}){const d={indigo:"from-indigo-500/20 to-transparent border-indigo-400/20",cyan:"from-cyan-500/20 to-transparent border-cyan-400/20",fuchsia:"from-fuchsia-500/20 to-transparent border-fuchsia-400/20",amber:"from-amber-500/20 to-transparent border-amber-400/20"};return e.jsxs("div",{className:`relative overflow-hidden rounded-2xl border bg-gradient-to-br ${d[c]} bg-white/5 backdrop-blur p-5`,children:[e.jsxs("div",{className:"flex items-center justify-between text-slate-300",children:[e.jsx("span",{className:"text-xs uppercase tracking-wider font-semibold",children:a}),e.jsx("span",{className:"opacity-70",children:t})]}),e.jsx("div",{className:"mt-3 text-2xl font-black tracking-tight",children:u?e.jsx("span",{className:"inline-block h-7 w-24 rounded bg-white/10 animate-pulse"}):r})]})}function T({label:t,value:a,tone:r,icon:c}){const u={emerald:"text-emerald-300 border-emerald-400/20 from-emerald-500/10",rose:"text-rose-300 border-rose-400/20 from-rose-500/10",amber:"text-amber-300 border-amber-400/20 from-amber-500/10"}[r];return e.jsxs("div",{className:`rounded-2xl border bg-gradient-to-br to-transparent bg-white/5 backdrop-blur p-5 ${u}`,children:[e.jsxs("div",{className:"text-xs uppercase tracking-wider font-semibold text-slate-300 flex items-center gap-2",children:[t," ",c]}),e.jsx("div",{className:"mt-2 text-3xl font-black tracking-tight",children:a})]})}function U({title:t,subtitle:a,children:r}){return e.jsxs("div",{className:"rounded-2xl border border-white/10 bg-white/5 backdrop-blur p-5",children:[e.jsxs("div",{className:"mb-4",children:[e.jsx("h3",{className:"font-bold text-white",children:t}),a&&e.jsx("p",{className:"text-xs text-slate-400",children:a})]}),e.jsx("div",{className:"space-y-3",children:r})]})}function R({label:t,sub:a,value:r,pct:c,color:u}){return e.jsxs("div",{children:[e.jsxs("div",{className:"flex justify-between items-baseline text-sm",children:[e.jsxs("div",{className:"min-w-0 flex-1 pr-3",children:[e.jsx("div",{className:"font-semibold truncate",children:t}),a&&e.jsx("div",{className:"text-xs text-slate-400 truncate",children:a})]}),e.jsx("div",{className:"font-mono text-sm font-bold text-slate-100",children:N(r)})]}),e.jsx("div",{className:"mt-1.5 h-1.5 rounded-full bg-white/5 overflow-hidden",children:e.jsx("div",{className:`h-full rounded-full bg-gradient-to-r ${u}`,style:{width:`${Math.max(4,c*100)}%`}})})]})}function I(){return e.jsx("div",{className:"text-sm text-slate-500 italic py-6 text-center",children:"Sem dados ainda no perÃodo."})}
1function S({to:t,icon:a,title:r,desc:c}){return e.jsxs(F,{to:t,className:"group rounded-2xl border border-white/10 bg-white/5 hover:bg-white/10 backdrop-blur p-5 transition-all hover:-translate-y-0.5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("div",{className:"h-10 w-10 rounded-xl bg-indigo-500/20 text-indigo-300 flex items-center justify-center",children:a}),e.jsx(ie,{className:"w-4 h-4 text-slate-500 group-hover:text-white group-hover:translate-x-1 transition"})]}),e.jsx("div",{className:"mt-3 font-bold",children:r}),e.jsx("div",{className:"text-sm text-slate-400 mt-1",children:c})]})}export{ve as component};
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.