1import{a as W,u as A,b as _,j as e}from"./vendor-query-skQMsM6X.js";import{r as v}from"./vendor-react-DyYvSSk1.js";import{aJ as $,p as j,at as J,au as U,av as V,aw as Y,ax as X,ab as S,aK as Z,am as ee,an as ae,ao as se,ap as ie,aq as d,I as f,aL as L,as as re,aB as ne,aC as te,aD as le,aE as ce,aF as de,aG as oe,aH as ue,aI as me,b as t,c as o}from"./index-BGwtrXIa.js";import{T as xe,a as he,b as M,c as w,d as pe,e as C}from"./table-DnRDtd-j.js";import{A as je,a as fe}from"./alert-BptgmgmS.js";import{ag as ge,i as ye,L as D,bo as _e,t as ve,u as O,bp as we}from"./vendor-ui-CCmu0bzQ.js";import"./vendor-supabase-BfoV2C50.js";const Ce={feature_key:"",display_name:"",description:null,display_order:0,is_dynamic:!1,dynamic_field:null,dynamic_suffix:null,is_active:!0};function ke(){const g=W(),[i,n]=v.useState(null),[P,u]=v.useState(!1),[y,F]=v.useState(!1),[m,N]=v.useState(null),{data:c,isLoading:K}=A({staleTime:6e4,refetchOnWindowFocus:!1,queryKey:["plan-features"],queryFn:async()=>{const{data:a,error:s}=await t.from("plan_features").select("*").order("display_order");if(s)throw s;return a}}),{data:x}=A({staleTime:6e4,refetchOnWindowFocus:!1,queryKey:["subscription-plans"],queryFn:async()=>{const{data:a,error:s}=await t.from("subscription_plans").select("id, code, name").order("display_order");if(s)throw s;return a}}),{data:h,isLoading:z}=A({staleTime:6e4,refetchOnWindowFocus:!1,queryKey:["plan-feature-assignments"],queryFn:async()=>{const{data:a,error:s}=await t.from("plan_feature_assignments").select("*");if(s)throw s;return a}}),b=()=>{g.invalidateQueries({queryKey:["plan-features"]}),g.invalidateQueries({queryKey:["plan-feature-assignments"]}),g.invalidateQueries({queryKey:["platform-metrics"]})},E=_({mutationFn:async a=>{const{data:s,error:r}=await t.from("plan_features").insert(a).select().single();if(r)throw r;return s},onSuccess:()=>{b(),o.success("Feature criada com sucesso"),u(!1),F(!1)},onError:a=>{o.error(`Erro ao criar feature: ${a.message}`)}}),q=_({mutationFn:async a=>{const{error:s}=await t.from("plan_features").update({display_name:a.display_name,description:a.description,is_dynamic:a.is_dynamic,dynamic_field:a.dynamic_field,dynamic_suffix:a.dynamic_suffix,is_active:a.is_active}).eq("id",a.id);if(s)throw s},onSuccess:()=>{b(),o.success("Feature atualizada"),u(!1)},onError:()=>{o.error("Erro ao atualizar feature")}}),k=_({mutationFn:async a=>{const{error:s}=await t.from("plan_feature_assignments").delete().eq("feature_id",a);if(s)throw s;const{error:r}=await t.from("plan_features").delete().eq("id",a);if(r)throw r},onSuccess:()=>{b(),o.success("Feature excluÃda"),N(null)},onError:a=>{o.error(`Erro ao excluir feature: ${a.message}`)}}),I=_({mutationFn:async({planId:a,featureId:s,isIncluded:r})=>{const p=h==null?void 0:h.find(l=>l.plan_id===a&&l.feature_id===s);if(p){const{error:l}=await t.from("plan_feature_assignments").update({is_included:r}).eq("id",p.id);if(l)throw l}else{const{error:l}=await t.from("plan_feature_assignments").insert({plan_id:a,feature_id:s,is_included:r});if(l)throw l}},onSuccess:()=>{g.invalidateQueries({queryKey:["plan-feature-assignments"]})},onError:()=>{o.error("Erro ao atualizar atribuição")}}),H=(a,s)=>{const r=h==null?void 0:h.find(p=>p.plan_id===a&&p.feature_id===s);return(r==null?void 0:r.is_included)??!1},Q=a=>{n(a),F(!1),u(!0)},B=()=>{const a=c!=null&&c.length?Math.max(...c.map(s=>s.display_order))+1:1;n({...Ce,display_order:a,id:""}),F(!0),u(!0)},G=()=>{if(i)if(y){const{id:a,...s}=i;E.mutate(s)}else q.mutate(i)},T=E.isPending||q.isPending,R=K||z;return e.jsx($,{children:e.jsxs("div",{className:"p-6 space-y-6",children:[e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold",children:"Features dos Planos"}),e.jsx("p",{className:"text-muted-foreground",children:"Gerencie as features exibidas nos cards de preços"})]}),e.jsxs(j,{onClick:B,children:[e.jsx(ge,{className:"w-4 h-4 mr-2"}),"Adicionar Feature"]})]}),e.jsxs(je,{variant:"default",className:"border-warning/50 bg-warning/10",children:[e.jsx(ye,{className:"h-4 w-4 text-warning"}),e.jsxs(fe,{className:"text-sm",children:[e.jsx("strong",{children:"Atenção:"})," Esta página controla apenas a ",e.jsx("strong",{children:"exibição visual"})," nos cards de pricing. O controle de acesso real à s funcionalidades é feito pelos campos booleanos em"," ",e.jsx("strong",{children:"Assinaturas â Planos"})," (",e.jsx("code",{children:"subscription_plans"}),"). Alterações aqui ",e.jsx("strong",{children:"não afetam"})," o backend."]})]}),R?e.jsx("div",{className:"flex items-center justify-center h-64",children:e.jsx(D,{className:"w-8 h-8 animate-spin"})}):e.jsxs(J,{children:[e.jsxs(U,{children:[e.jsx(V,{children:"Matriz de Features"}),e.jsx(Y,{children:"Marque quais features estão incluÃdas em cada plano. Features desmarcadas aparecem com â e riscadas."})]}),e.jsx(X,{children:e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(xe,{children:[e.jsx(he,{children:e.jsxs(M,{children:[e.jsx(w,{className:"w-[250px]",children:"Feature"}),e.jsx(w,{className:"w-[80px]",children:"Ativa"}),x==null?void 0:x.map(a=>e.jsx(w,{className:"text-center min-w-[100px]",children:a.name},a.id)),e.jsx(w,{className:"w-[100px]",children:"Ações"})]})}),e.jsx(pe,{children:c==null?void 0:c.map(a=>e.jsxs(M,{children:[e.jsx(C,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(_e,{className:"w-4 h-4 text-muted-foreground
1/50"}),e.jsxs("div",{children:[e.jsx("span",{className:"font-medium",children:a.display_name}),a.is_dynamic&&e.jsx(S,{variant:"secondary",className:"ml-2 text-xs",children:"Dinâmica"}),a.description&&e.jsx("p",{className:"text-xs text-muted-foreground",children:a.description})]})]})}),e.jsx(C,{children:e.jsx(S,{variant:a.is_active?"default":"secondary",children:a.is_active?"Sim":"Não"})}),x==null?void 0:x.map(s=>e.jsx(C,{className:"text-center",children:e.jsx(Z,{checked:H(s.id,a.id),onCheckedChange:r=>{I.mutate({planId:s.id,featureId:a.id,isIncluded:r===!0})}})},s.id)),e.jsx(C,{children:e.jsxs("div",{className:"flex gap-1",children:[e.jsx(j,{variant:"ghost",size:"icon",onClick:()=>Q(a),children:e.jsx(ve,{className:"w-4 h-4"})}),e.jsx(j,{variant:"ghost",size:"icon",onClick:()=>N(a),className:"text-destructive hover:text-destructive",children:e.jsx(O,{className:"w-4 h-4"})})]})})]},a.id))})]})})})]}),e.jsx(ee,{open:P,onOpenChange:u,children:e.jsxs(ae,{children:[e.jsx(se,{children:e.jsx(ie,{children:y?"Adicionar Feature":"Editar Feature"})}),i&&e.jsxs("div",{className:"space-y-4",children:[y&&e.jsxs("div",{children:[e.jsx(d,{children:"Chave (feature_key)"}),e.jsx(f,{value:i.feature_key,onChange:a=>n({...i,feature_key:a.target.value}),placeholder:"ex: new_feature"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Identificador único, sem espaços"})]}),e.jsxs("div",{children:[e.jsx(d,{children:"Nome de Exibição"}),e.jsx(f,{value:i.display_name,onChange:a=>n({...i,display_name:a.target.value})})]}),e.jsxs("div",{children:[e.jsx(d,{children:"Descrição (opcional)"}),e.jsx(f,{value:i.description||"",onChange:a=>n({...i,description:a.target.value||null})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(L,{checked:i.is_dynamic,onCheckedChange:a=>n({...i,is_dynamic:a})}),e.jsx(d,{children:"Feature Dinâmica (usa valor do plano)"})]}),i.is_dynamic&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{children:[e.jsx(d,{children:"Campo Dinâmico"}),e.jsx(f,{value:i.dynamic_field||"",onChange:a=>n({...i,dynamic_field:a.target.value||null}),placeholder:"ex: ai_tokens_monthly"})]}),e.jsxs("div",{children:[e.jsx(d,{children:"Sufixo"}),e.jsx(f,{value:i.dynamic_suffix||"",onChange:a=>n({...i,dynamic_suffix:a.target.value||null}),placeholder:"ex: tokens IA/mês"})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(L,{checked:i.is_active,onCheckedChange:a=>n({...i,is_active:a})}),e.jsx(d,{children:"Feature Ativa"})]})]}),e.jsxs(re,{children:[e.jsx(j,{variant:"outline",onClick:()=>u(!1),children:"Cancelar"}),e.jsxs(j,{onClick:G,disabled:T,children:[T?e.jsx(D,{className:"w-4 h-4 mr-2 animate-spin"}):e.jsx(we,{className:"w-4 h-4 mr-2"}),y?"Criar":"Salvar"]})]})]})}),e.jsx(ne,{open:!!m,onOpenChange:a=>!a&&N(null),children:e.jsxs(te,{children:[e.jsxs(le,{children:[e.jsx(ce,{children:"Excluir Feature"}),e.jsxs(de,{children:["Tem certeza que deseja excluir a feature ",e.jsxs("strong",{children:['"',m==null?void 0:m.display_name,'"']}),"? Isso removerá todas as atribuições de planos associadas. Esta ação não pode ser desfeita."]})]}),e.jsxs(oe,{children:[e.jsx(ue,{children:"Cancelar"}),e.jsxs(me,{onClick:()=>m&&k.mutate(m.id),className:"bg-destructive text-destructive-foreground hover:bg-destructive/90",children:[k.isPending?e.jsx(D,{className:"w-4 h-4 mr-2 animate-spin"}):e.jsx(O,{className:"w-4 h-4 mr-2"}),"Excluir"]})]})]})})]})})}export{ke 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.