1import{r as d,j as e}from"./vendor-react-DIA7vK4g.js";import{F as P}from"./FinanceiroLayout-BFNsfPwP.js";import{u as de}from"./useSandboxConfig-H_pzAYpM.js";import{b5 as ce,b6 as me,b7 as F,aa as h,bb as g,bd as p,c7 as E,be as f,bF as ue,bg as j,_ as xe,eK as be,eL as y,a$ as he,X as ge,av as pe,bf as I,bP as k,bz as K,bm as C,U as fe,aA as je,at as ye,ax as ke,P as _e,aY as ve,aV as we,bq as L,br as M,bs as $,bt as R,bu as Q,bv as W,bw as U,bx as H,b9 as r,ba as a}from"./index-DtUVjOr4.js";import{P as Ne}from"./PageHeader-DbudfU8v.js";import{B as Se}from"./StatusBadge-37q_xsvK.js";import{W as Ce}from"./webhook-4OnrnzHB.js";import{E as De}from"./eye-DZWf3ZF4.js";import{p as qe}from"./vendor-utils-DWHjTIse.js";import"./vendor-docs-BA9xu9ak.js";import"./GlobalSearch-CILhhYPj.js";import"./command-8X5Fvwln.js";import"./repeat-sulmqLMn.js";import"./billing-formatters-uNs-HNzw.js";import"./corner-down-left-BYgMx1Bm.js";import"./clock-x8uDBefL.js";import"./vendor-supabase-_bOAMmkC.js";function Xe(){const{config:l,isLoading:X,isSandboxEnabled:o,updateConfig:D,isUpdating:q}=de(),{user:c}=ce(),m=me(),[A,O]=d.useState(""),[_,z]=d.useState(!1),[T,B]=d.useState({mock_omie:!0,mock_pagarme:!0,mock_hubspot:!0,mock_pontua_api:!0,mock_email:!0,mock_whatsapp:!0}),[V,Y]=d.useState(!1),[G,v]=d.useState(!1),[J,w]=d.useState(!1),[N,S]=d.useState(""),[x,n]=d.useState(null);l&&!V&&(O(l.stripe_test_api_key||""),B({mock_omie:l.mock_omie,mock_pagarme:l.mock_pagarme,mock_hubspot:l.mock_hubspot,mock_pontua_api:l.mock_pontua_api,mock_email:l.mock_email,mock_whatsapp:l.mock_whatsapp}),Y(!0));const{data:i,isLoading:Z}=F({queryKey:["sandbox-counts"],queryFn:async()=>{const[s,t,u,le]=await Promise.all([a.from("customers").select("id",{count:"exact",head:!0}).eq("is_sandbox",!0),a.from("subscriptions").select("id",{count:"exact",head:!0}).eq("is_sandbox",!0),a.from("invoices").select("id",{count:"exact",head:!0}).eq("is_sandbox",!0),a.from("webhook_events").select("id",{count:"exact",head:!0}).eq("is_sandbox",!0)]);return{customers:s.count||0,subscriptions:t.count||0,invoices:u.count||0,webhooks:le.count||0}}}),{data:b}=F({queryKey:["sandbox-webhook-log"],queryFn:async()=>{const{data:s}=await a.from("webhook_events").select("*").eq("is_sandbox",!0).order("created_at",{ascending:!1}).limit(50);return s||[]}}),ee=async()=>{const s=!o,t=s?"SANDBOX":"PRODUCAO";if(N!==t){r.error(`Digite "${t}" para confirmar`);return}D({is_sandbox_enabled:s,...s?{enabled_by:c==null?void 0:c.id,enabled_at:new Date().toISOString()}:{disabled_by:c==null?void 0:c.id,disabled_at:new Date().toISOString()}}),v(!1),S(""),r.success(s?"Modo Sandbox ativado!":"Modo Sandbox desativado!")},se=()=>{D({stripe_test_api_key:A||null,...T}),r.success("Configuração salva!")},ae=async()=>{n("create");try{const{data:s,error:t}=await a.from("customers").insert({company_name:`Sandbox Test ${Date.now()}`,email:`sandbox-${Date.now()}@test.com`,document:"00000000000",document_type:"cpf",status:"active",is_test:!0}).select().single();if(t)throw t;await a.from("subscriptions").insert({customer_id:s.id,status:"active",billing_cycle:"monthly",start_date:new Date().toISOString().split("T")[0]}),await a.from("licenses").insert({customer_id:s.id,status:"active",max_users:10}),await a.from("invoices").insert({customer_id:s.id,status:"pending",amount:29900,due_date:new Date(Date.now()+30*864e5).toISOString().split("T")[0]}),r.success("Cliente de teste criado com sucesso!"),m.invalidateQueries({queryKey:["sandbox-counts"]})}catch(s){r.error("Erro: "+s.message)}finally{n(null)}},te=async()=>{n("billing");try{const{data:s}=await a.from("subscriptions").select("id, customer_id").eq("is_sandbox",!0).eq("status","active");if(!(s!=null&&s.length)){r.error("Nenhuma assinatura sandbox ativa");return}for(const t of s)await a.from("invoices").insert({customer_id:t.customer_id,subscription_id:t.id,status:"pending",amount:29900,due_date:new Date(Date.now()+30*864e5).toISOString().split("T")[0]});r.success(`${s.length} faturas criadas!`),m.invalidateQueries({queryKey:["sandbox-counts"]})}catch(s){r.error("Erro: "+s.message)}finally{n(null)}},re=async()=>{n("payment");try{const{data:s}=await a.from("invoices").select("id").eq("is_sandbox",!0).eq("status","pen
1ding").limit(1);if(!(s!=null&&s.length)){r.error("Nenhuma fatura sandbox pendente");return}await a.from("invoices").update({status:"paid",paid_at:new Date().toISOString(),payment_method:"sandbox_mock"}).eq("id",s[0].id),r.success("Pagamento simulado com sucesso!"),m.invalidateQueries({queryKey:["sandbox-counts"]})}catch(s){r.error("Erro: "+s.message)}finally{n(null)}},oe=async()=>{n("overdue");try{const{data:s,error:t}=await a.from("invoices").select("id").eq("is_sandbox",!0).eq("status","pending");if(t)throw t;if(!(s!=null&&s.length)){r.error("Nenhuma fatura sandbox pendente");return}for(const u of s)await a.from("invoices").update({status:"overdue",due_date:new Date(Date.now()-7*864e5).toISOString().split("T")[0]}).eq("id",u.id);r.success(`${s.length} faturas marcadas como inadimplentes!`),m.invalidateQueries({queryKey:["sandbox-counts"]})}catch(s){r.error("Erro: "+s.message)}finally{n(null)}},ie=async()=>{n("clean");try{await a.from("invoices").delete().eq("is_sandbox",!0),await a.from("subscription_addons").delete().eq("is_sandbox",!0),await a.from("licenses").delete().eq("is_sandbox",!0),await a.from("subscriptions").delete().eq("is_sandbox",!0),await a.from("negotiations").delete().eq("is_sandbox",!0),await a.from("webhook_events").delete().eq("is_sandbox",!0),await a.from("customers").delete().eq("is_sandbox",!0),r.success("Dados sandbox removidos!"),m.invalidateQueries({queryKey:["sandbox-counts"]}),m.invalidateQueries({queryKey:["sandbox-webhook-log"]}),w(!1)}catch(s){r.error("Erro: "+s.message)}finally{n(null)}};if(X)return e.jsx(P,{children:e.jsx("div",{className:"flex items-center justify-center h-64",children:e.jsx(h,{className:"h-8 w-8 animate-spin text-muted-foreground"})})});const ne=[{key:"mock_omie",label:"Omie (ERP/Boletos/NFe)"},{key:"mock_pagarme",label:"Pagar.me (Pagamentos)"},{key:"mock_hubspot",label:"HubSpot (CRM Sync)"},{key:"mock_pontua_api",label:"API Pontua (Webhooks/Licenças)"},{key:"mock_email",label:"E-mail (Notificações)"},{key:"mock_whatsapp",label:"WhatsApp (Mensagens)"}];return e.jsxs(P,{children:[e.jsxs("div",{className:"space-y-6",children:[e.jsx(Ne,{title:"Modo Sandbox",children:e.jsx("p",{className:"text-sm text-muted-foreground",children:"Teste billing, gateways e webhooks sem impactar produção"})}),e.jsxs(g,{className:o?"border-yellow-500/50 bg-yellow-50/30 dark:bg-yellow-950/10":"",children:[e.jsx(p,{children:e.jsx("div",{className:"flex items-center justify-between",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:`rounded-full p-1 ${o?"ring-4 ring-emerald-200 dark:ring-emerald-900":"ring-4 ring-muted"}`,children:e.jsx(E,{checked:o,onCheckedChange:()=>{S(""),v(!0)}})}),e.jsx(f,{className:"text-lg",children:o?"Sandbox Ativo":"Produção"}),e.jsx(ue,{variant:o?"default":"secondary",children:o?"SANDBOX":"LIVE"})]})})}),o&&e.jsx(j,{children:e.jsxs("div",{className:"rounded-lg bg-gradient-to-r from-amber-50 to-yellow-50 dark:from-amber-950/30 dark:to-yellow-950/30 border border-amber-200 dark:border-amber-800 p-4 flex items-start gap-3",children:[e.jsx(xe,{className:"h-5 w-5 text-amber-600 shrink-0 mt-0.5"}),e.jsxs("div",{className:"text-sm text-amber-800 dark:text-amber-300 space-y-1",children:[e.jsx("p",{className:"font-medium",children:"Modo Sandbox ativo"}),e.jsxs("ul",{className:"list-disc list-inside space-y-0.5 text-amber-700 dark:text-amber-400",children:[e.jsx("li",{children:"Usa Stripe Test API Key (se configurada)"}),e.jsx("li",{children:"Simula integrações externas"}),e.jsxs("li",{children:["Dados marcados com ",e.jsx("code",{className:"bg-amber-200/50 dark:bg-amber-900/50 px-1 rounded text-xs",children:"is_sandbox = true"})]}),e.jsx("li",{children:"Bloqueia envio real de e-mails e WhatsApp"})]})]})]})})]}),e.jsx("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:Z?Array.from({length:4}).map((s,t)=>e.jsx(be,{},t)):e.jsxs(e.Fragment,{children:[e.jsx(y,{title:"Clientes",value:(i==null?void 0:i.customers)||0,icon:he,iconColor:"text-blue-600",iconBg:"bg-blue-50 dark:bg-blue-950/40"}),e.jsx(y,{title:"Assinaturas",value:(i==null?void 0:i.subscriptions)||0,icon:ge,iconColor:"text-violet-600",iconBg:"bg-violet-50 dark:bg-violet-950/40"}),e.jsx(y,{title:"Faturas",value:(i==null?void 0:i.invoices)||0,icon:pe,iconColor:"text-amber-600",iconBg:"bg-amber-50 dark:bg-amber-950/40"}),e.jsx(y,{title:"Webhooks",value:(i==null?void 0:i.webhooks)||0,icon:Ce,iconColor:"text-slate-600",iconBg:"bg-slate-100 dar
1k:bg-slate-800/40"})]})}),e.jsxs(g,{children:[e.jsxs(p,{children:[e.jsx(f,{className:"text-lg",children:"Configuração"}),e.jsx(I,{children:"Defina chaves de teste e serviços mockados"})]}),e.jsxs(j,{className:"space-y-6",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(k,{children:"Stripe Test API Key"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(K,{type:_?"text":"password",value:A,onChange:s=>O(s.target.value),placeholder:"sk_test_...",className:"font-mono text-sm"}),e.jsx(C,{variant:"outline",size:"icon",onClick:()=>z(!_),children:_?e.jsx(fe,{className:"h-4 w-4"}):e.jsx(De,{className:"h-4 w-4"})})]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx(k,{children:"Serviços Mockados"}),e.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:ne.map(s=>e.jsxs("div",{className:"flex items-center justify-between rounded-lg border p-3",children:[e.jsx(k,{htmlFor:s.key,className:"text-sm font-normal cursor-pointer",children:s.label}),e.jsx(E,{id:s.key,checked:T[s.key],onCheckedChange:t=>B(u=>({...u,[s.key]:!!t}))})]},s.key))})]}),e.jsxs(C,{onClick:se,disabled:q,children:[q?e.jsx(h,{className:"h-4 w-4 mr-2 animate-spin"}):e.jsx(je,{className:"h-4 w-4 mr-2"}),"Salvar Configuração"]})]})]}),e.jsxs(g,{children:[e.jsx(p,{children:e.jsx(f,{className:"text-lg",children:"Ações Rápidas"})}),e.jsxs(j,{className:"space-y-4",children:[e.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:[{key:"create",label:"Criar Cliente de Teste",icon:ye,iconBg:"bg-blue-50 dark:bg-blue-950/40",iconColor:"text-blue-600",handler:ae},{key:"billing",label:"Simular Ciclo de Faturamento",icon:ke,iconBg:"bg-violet-50 dark:bg-violet-950/40",iconColor:"text-violet-600",handler:te},{key:"payment",label:"Simular Pagamento",icon:_e,iconBg:"bg-emerald-50 dark:bg-emerald-950/40",iconColor:"text-emerald-600",handler:re},{key:"overdue",label:"Simular Inadimplência",icon:ve,iconBg:"bg-amber-50 dark:bg-amber-950/40",iconColor:"text-amber-600",handler:oe}].map(s=>e.jsxs("button",{onClick:s.handler,disabled:!!x,className:"flex items-center gap-3 rounded-xl border p-4 text-left hover:bg-muted/50 transition-colors disabled:opacity-50",children:[e.jsx("div",{className:`rounded-lg p-2 ${s.iconBg}`,children:x===s.key?e.jsx(h,{className:`h-5 w-5 animate-spin ${s.iconColor}`}):e.jsx(s.icon,{className:`h-5 w-5 ${s.iconColor}`})}),e.jsx("span",{className:"text-sm font-medium",children:s.label})]},s.key))}),e.jsxs(C,{variant:"destructive",onClick:()=>w(!0),disabled:!!x,children:[e.jsx(we,{className:"h-4 w-4 mr-2"}),"Limpar Dados Sandbox"]})]})]}),e.jsxs(g,{children:[e.jsxs(p,{children:[e.jsx(f,{className:"text-lg",children:"Log de Webhooks Sandbox"}),e.jsx(I,{children:"Ãltimos 50 eventos com is_sandbox = true"})]}),e.jsx(j,{children:b!=null&&b.length?e.jsx("div",{className:"border rounded-lg overflow-hidden",children:e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-muted/50",children:e.jsxs("tr",{children:[e.jsx("th",{className:"text-left p-3 font-medium text-muted-foreground",children:"Data"}),e.jsx("th",{className:"text-left p-3 font-medium text-muted-foreground",children:"Evento"}),e.jsx("th",{className:"text-left p-3 font-medium text-muted-foreground",children:"Origem"}),e.jsx("th",{className:"text-left p-3 font-medium text-muted-foreground",children:"Status"})]})}),e.jsx("tbody",{className:"divide-y",children:b.map(s=>e.jsxs("tr",{className:"hover:bg-muted/30",children:[e.jsx("td",{className:"p-3 text-muted-foreground font-mono text-xs tabular-nums",children:s.created_at?qe(new Date(s.created_at),"dd/MM HH:mm"):"-"}),e.jsx("td",{className:"p-3 font-mono text-xs",children:s.event_type||"-"}),e.jsx("td",{className:"p-3",children:s.source||"-"}),e.jsx("td",{className:"p-3",children:e.jsx(Se,{status:s.status==="processed"?"paid":s.status||"pending"})})]},s.id))})]})})}):e.jsx("p",{className:"text-sm text-muted-foreground text-center py-8",children:"Nenhum evento sandbox registrado"})})]})]}),e.jsx(L,{open:G,onOpenChange:v,children:e.jsxs(M,{children:[e.jsxs($,{children:[e.jsx(R,{children:o?"Desativar Modo Sandbox?":"Ativar Modo Sandbox?"}),e.jsxs(Q,{className:"space-y-3",children:[e.jsx("p",{children:o?"Ao desativar, todas as operações voltarão a usar integrações reais.":"Ao ativar, integrações externas serão simuladas e dados marcados como sandbox."}),e.jsxs("div",{children:[e.jsxs(k,{className:"text-sm",children:["Digite ",e.jsx("strong",{children:o?"PRODUCAO":"SANDBOX"})," para confirmar:"]}),e.jsx(K,{value:N,onChange:s=>S(s.target.value),className:"mt-2",autoFocus:!0})]})]})]}),e.jsxs(W,{children:[e.jsx(U,{children:"Cancelar"}),e.jsx(H,{onClick:ee,disabled:N!==(o?"PRODUCAO":"SANDBOX"),children:"Confirmar"})]})]})}),e.jsx(L,{open:J,onOpenChange:w,children:e.jsxs(M,{children:[e.jsxs($,{children:[e.jsx(R,{children:"Limpar Todos os Dados Sandbox?"}),e.jsx(Q,{children:"Todos os clientes, assinaturas, faturas, licenças e webhooks com is_sandbox = true serão removidos permanentemente."})]}),e.jsxs(W,{children:[e.jsx(U,{children:"Cancelar"}),e.jsxs(H,{onClick:ie,className:"bg-destructive text-destructive-foreground hover:bg-destructive/90",children:[x==="clean"?e.jsx(h,{className:"h-4 w-4 mr-2 animate-spin"}):null,"Sim, Limpar Tudo"]})]})]})})]})}export{Xe 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.