1/* ANPG DEVTEAM - 23/09/2026, 12:53:51 */ 2import{j as e}from"./data-vendor-BRYI0K8G.js";import{r as a}from"./react-vendor-Bj9LQV-u.js";import{A as fe}from"./AdminLayout-Dwt36cjQ.js";import{bQ as je,cO as ve,cP as Ne,af as be,bV as Ce,b as d,ag as we,ah as ye,ai as Se,bf as ke,w as Ee,F as te,E as Ae,bh as l,cj as _e,bO as De,cQ as y}from"./index-cwV7SCbt.js";import{I as ae}from"./input-DXoiBR8B.js";import{T as Te}from"./textarea-DXuDKDDL.js";import{C as n,a as i}from"./card--xtz4eBf.js";import{B as h}from"./badge-DH3OcwR0.js";import{S as L,a as P,b as z,c as I,d as S}from"./select-DB2ezOdP.js";import{B as Oe}from"./bot-Dh7mXPIY.js";import{S as Be}from"./search-BiS5Q1yZ.js";import{P as se}from"./plus-DNnuudfk.js";import{U as Fe}from"./upload-BCjEN_Mp.js";import{T as re}from"./trash-2-DsWPE62z.js";import{B as Le}from"./book-open-YlczfV9G.js";import{M as Pe}from"./message-circle-gmfHcZDA.js";import{S as ze}from"./sparkles-BLjkhvzD.js";import{C as Ie}from"./clock-BrMjUDom.js";import{P as Re}from"./pencil-MGvtVn2j.js";import"./message-square-PMubaXRc.js";import"./newspaper-CHlewEV4.js";import"./tag-C9YMMS4O.js";import"./camera-BaCLsw_E.js";import"./shield-check-WsIH1iGp.js";import"./sliders-horizontal-BQ-86nOU.js";import"./user-check-C6J6ewlk.js";import"./network-O6WVq_gh.js";import"./gavel-PkPxUdh7.js";import"./presentation-CMVRwA7v.js";import"./star-DIWblV4s.js";import"./clipboard-check-CX6a16zF.js";import"./folder-open-Caf45xKb.js";import"./settings-2-Bi0RsXgx.js";import"./label-ZRKjaR0k.js";import"./rocket-C18tjCXj.js";import"./user-GGMDWiDQ.js";import"./arrow-left-BeqZSyXi.js";import"./circle-check-big-B8jnYIp7.js";import"./charts-vendor-Cs07Jtv5.js";import"./motion-vendor-DMc3mo8t.js";import"./i18n-vendor-BSIZ7LIS.js";import"./index-BdQq_4o_.js";import"./index-BatE7gkT.js";import"./index-D6PzSG9l.js";import"./chevron-up-BjRn6whD.js";function qe(o){if(!o)return"â";const c=o.includes("T")?o:o.replace(" ","T"),p=new Date(c);return isNaN(p.getTime())?o:p.toLocaleString("pt-PT",{day:"2-digit",month:"2-digit",year:"numeric",hour:"2-digit",minute:"2-digit"})}function k(o){return o==="auto:qa-cache"?"chat":o.startsWith("auto:")?"auto":"manual"}const Ue=[{value:"all",label:"Todas as origens"},{value:"chat",label:"Perguntas do Site"},{value:"auto",label:"Auto-Enriquecido"},{value:"manual",label:"Criado Manualmente"}],R=[{value:"general",label:"Geral"},{value:"institucional",label:"Institucional"},{value:"história",label:"História"},{value:"regulação",label:"Regulação"},{value:"licenciamento",label:"Licenciamento"},{value:"fiscalização",label:"Fiscalização"},{value:"licitações",label:"Licitações"},{value:"exploração",label:"Exploração"},{value:"produção",label:"Produção"},{value:"gás",label:"Gás"},{value:"dados",label:"Dados"},{value:"investidor",label:"Investidor"},{value:"sustentabilidade",label:"Sustentabilidade"},{value:"biocombustÃveis",label:"BiocombustÃveis"},{value:"compliance",label:"Compliance"},{value:"contactos",label:"Contactos"},{value:"faq",label:"FAQ"},{value:"oportunidades",label:"Oportunidades"},{value:"conteúdo local",label:"Conteúdo Local"}];function Bt(){const{user:o}=je(),[c,p]=a.useState([]),[q,le]=a.useState([]),[U,oe]=a.useState([]),[ne,G]=a.useState(!0),[g,ie]=a.useState(""),[E,ce]=a.useState("all"),[f,V]=a.useState("all"),[de,x]=a.useState(!1),[j,M]=a.useState(null),[u,v]=a.useState(""),[N,b]=a.useState(""),[A,_]=a.useState("general"),[D,T]=a.useState(!0),[Q,H]=a.useState(!1),[C,w]=a.useState(null),[O,B]=a.useState(!1),K=a.useRef(null),me=async t=>{if(t.size>20*1024*1024){l.error("Ficheiro demasiado grande (máx. 20MB)");return}B(!0);try{if(!_e()){l.error("Sessão expirada. Faça login novamente."),B(!1);return}const r=await De.extractKbDocument(t);if(w(r.url),!u.trim()){const m=(r.title||t.name.replace(/\.[^/.]+$/,"").replace(/[-_]/g," ")).trim();v(m)}r.text&&r.text.trim()?(b(m=>m.trim()?m+` 3 4`+r.text:r.text),l.success("Documento carregado e conteúdo extraÃdo")):r.extract_error?(l.warning("Documento carregado, mas não foi possÃvel extrair o texto. Cole o conteúdo manualmente.",{description:r.extract_error}),console.warn("Extract error:",r.extract_error)):l.warning("Documento carregado, mas sem texto extraÃvel (pode ser digitalizado). Cole o conteúdo manualmente.")}catch(s){console.error("Upload error:",s),l.error(s instanceof Error?s.message:"Erro ao carregar documento")}B(!1)},F=async()=>{G(!0);try{const t=await y.list({sort:"category,-created_at",limit:500});p(t.data||[])}catch(t){l.error("Erro ao carregar base de conhecimento"),console.error(t)}G(!1)};a.useEffect(()=>{F(),(async()=>{try{const[t,s]=await Promise.all([ve.list({sort:"-created_at",limit:500}),Ne.list({sort:"-created_at",limit:500})]);le(t.data||[]),oe(s.data||[])}catch(t){console.warn("SOBA quality metrics unavailable:",t)}})()},[]);const X=()=>{v(""),b(""),_("general"),T(!0),M(null),w(null)},xe=t=>{M(t),v(t.title),b(t.content),_(t.category),T(t.is_active),w(t.document_url),x(!0)},$=()=>{X(),x(!0)},ue=async()=>
4{if(!u.trim()||!N.trim()){l.error("TÃtulo e conteúdo são obrigatórios");return}if(H(!0),j)try{await y.update(j.id,{title:u,content:N,category:A,language:"pt",is_active:D,document_url:C}),l.success("Entrada atualizada com sucesso")}catch(t){l.error("Erro ao atualizar"),console.error(t)}else try{await y.create({title:u,content:N,category:A,language:"pt",is_active:D,created_by:o==null?void 0:o.id,document_url:C}),l.success("Entrada criada com sucesso")}catch(t){l.error("Erro ao criar entrada"),console.error(t)}H(!1),x(!1),X(),F()},he=async t=>{if(confirm("Tem a certeza que deseja eliminar esta entrada?"))try{await y.delete(t),l.success("Entrada eliminada"),F()}catch(s){l.error("Erro ao eliminar"),console.error(s)}},pe=c.filter(t=>k(t.category)==="chat").length,W=U.filter(t=>t.rating==="up").length,J=U.filter(t=>t.rating==="down").length,Y=W+J,Z=Y?Math.round(W/Y*100):null,ge=q.filter(t=>["fallback","rejected_generic","web_unverified"].includes(t.source)).length,ee=c.filter(t=>{const s=!g||t.title.toLowerCase().includes(g.toLowerCase())||t.content.toLowerCase().includes(g.toLowerCase()),r=E==="all"||t.category===E,m=f==="all"||k(t.category)===f;return s&&r&&m});return e.jsx(fe,{title:"Base de Conhecimento",subtitle:"Gerir conteúdo do SOBA",children:e.jsxs("main",{className:"container mx-auto px-4 py-8",children:[e.jsxs("div",{className:"flex items-center gap-3 mb-6",children:[e.jsx(Oe,{className:"h-8 w-8 text-primary"}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-foreground",children:"Base de Conhecimento â SOBA"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Gerir os conteúdos que alimentam o assistente virtual SOBA"})]})]}),e.jsxs("div",{className:"flex flex-col sm:flex-row gap-3 mb-6",children:[e.jsxs("div",{className:"relative flex-1",children:[e.jsx(Be,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(ae,{placeholder:"Pesquisar...",value:g,onChange:t=>ie(t.target.value),className:"pl-9"})]}),e.jsxs(L,{value:f,onValueChange:V,children:[e.jsx(P,{className:"w-[190px]",children:e.jsx(z,{placeholder:"Origem"})}),e.jsx(I,{children:Ue.map(t=>e.jsx(S,{value:t.value,children:t.label},t.value))})]}),e.jsxs(L,{value:E,onValueChange:ce,children:[e.jsx(P,{className:"w-[180px]",children:e.jsx(z,{placeholder:"Categoria"})}),e.jsxs(I,{children:[e.jsx(S,{value:"all",children:"Todas"}),R.map(t=>e.jsx(S,{value:t.value,children:t.label},t.value))]})]}),e.jsxs(be,{open:de,onOpenChange:x,children:[e.jsx(Ce,{asChild:!0,children:e.jsxs(d,{onClick:$,children:[e.jsx(se,{className:"h-4 w-4 mr-1"})," Nova Entrada"]})}),e.jsxs(we,{className:"max-w-2xl max-h-[90vh] overflow-y-auto",children:[e.jsxs(ye,{children:[e.jsx(Se,{children:j?"Editar Entrada":"Nova Entrada"}),e.jsx(ke,{children:"Carregue um documento (PDF, DOCX, TXT, CSV) para extrair o conteúdo automaticamente, ou preencha o tÃtulo e conteúdo manualmente."})]}),e.jsxs("div",{className:"space-y-4 mt-4",children:[e.jsxs("div",{className:"rounded-lg border-2 border-dashed border-muted-foreground/25 p-4",children:[e.jsx("input",{ref:K,type:"file",accept:".pdf,.txt,.doc,.docx,.csv",className:"hidden",onChange:t=>{var r;const s=(r=t.target.files)==null?void 0:r[0];s&&me(s),t.target.value=""}}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs("div",{className:"flex-1",children:[e.jsx("p",{className:"text-sm font-medium text-foreground",children:"Documento (opcional)"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Faça upload de um PDF ou ficheiro de texto. O conteúdo será extraÃdo automaticamente."})]}),e.jsx(d,{type:"button",variant:"outline",size:"sm",onClick:()=>{var t;return(t=K.current)==null?void 0:t.click()},disabled:O,children:O?e.jsxs(e.Fragment,{children:[e.jsx(Ee,{className:"h-4 w-4 mr-1 animate-spin"})," A processar..."]}):e.jsxs(e.Fragment,{children:[e.jsx(Fe,{className:"h-4 w-4 mr-1"})," Upload"]})})]}),C&&e.jsxs("div",{className:"mt-3 flex items-center gap-2 p-2 bg-muted rounded text-sm",children:[e.jsx(te,{className:"h-4 w-4 text-primary shrink-0"}),e.jsx("span",{className:"truncate flex-1 text-foreground",children:"Documento anexado"}),e.jsx("a",{href:C,target:"_blank",rel:"noopener noreferrer",className:"text-primary hover:underline shrink-0",children:e.jsx(Ae,{className:"h-3.5 w-3.5"})}),e.jsx(d,{type:"button",variant:"ghost",size:"icon",className:"h-6 w-6",onClick:()=>w(null),children:e.jsx(re,{className:"h-3.5 w-3.5 text-destructive"})})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-sm font-medium text-foreground",children:"TÃtulo *"}),e.jsx(ae,{value:u,onChange:t=>v(t.target.value),placeholder:"Ex: Processo de Licenciamento"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-sm font-medium text-foreground",children:"Conteúdo *"}),e.jsx(Te,{value:N,onChange:t=>b(t.target.value),placeholder:"Descreva o conteúdo que o SOBA deve saber sobre este tema...",rows:10})]}),e.jsx("div",{className:"grid grid-cols-2 gap-4",children:e.jsxs("div",{children:[e.jsx("label",{className:"text-sm font-medium text-foreground",children:"Categoria"}),e.jsxs(L,{value:A,onValueChange:_,children:[e.jsx(P,{children:e.jsx(z,{})}),e.jsx(I,{children:R.map(t=>e.jsx(S,{value:t.value,children:t.label},t.value))})]})]})}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("input",{type:"checkbox",id:"is_active",checked:D,onChange:t=>T(t.target.checked),className:"rounded border-input"}),e.jsx("label",{htmlFor:"is_active",className:"text-sm text-foreground",children:"Ativo (visÃvel para o chatbot)"})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[e.jsx(d,{variant:"outline",onClick:()=>x(!1),children:"Cancelar"}),e.jsx(d,{onClick:ue,disabled:Q||O,children:Q?"A guardar...":j?"Atualizar":"Criar"})]})]})]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 sm:grid-cols-4 xl:grid-cols-8 gap-4 mb-6",children:[e.jsx(n,{className:"hover:shadow-none hover:translate-y-0",children:e.jsxs(i,{className:"p-4 text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-foreground",children:c.length}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Total de Entradas"})]})}),e.jsx(n,{className:"hover:shadow-none hover:translate-y-0",children:e.jsxs(i,{className:"p-4 text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-foreground",children:q.length}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Conversas (amostra)"}
4),e.jsx("p",{className:"text-[10px] text-muted-foreground/70 mt-0.5",children:"Ãltimas 500 · total em Análise do SOBA"})]})}),e.jsx(n,{className:"hover:shadow-none hover:translate-y-0",children:e.jsxs(i,{className:"p-4 text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-primary",children:Z==null?"â":`${Z}%`}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Respostas Ãteis"}),e.jsx("p",{className:"text-[10px] text-muted-foreground/70 mt-0.5",children:"Na amostra"})]})}),e.jsx(n,{className:"hover:shadow-none hover:translate-y-0",children:e.jsxs(i,{className:"p-4 text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-destructive",children:J}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Não Ãteis"})]})}),e.jsx(n,{className:"hover:shadow-none hover:translate-y-0",children:e.jsxs(i,{className:"p-4 text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-foreground",children:ge}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Casos a Rever"}),e.jsx("p",{className:"text-[10px] text-muted-foreground/70 mt-0.5",children:"Na amostra"})]})}),e.jsx(n,{className:"hover:shadow-none hover:translate-y-0",children:e.jsxs(i,{className:"p-4 text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-primary",children:c.filter(t=>t.is_active).length}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Ativas"})]})}),e.jsx(n,{className:"hover:shadow-none hover:translate-y-0 cursor-pointer",onClick:()=>V(f==="chat"?"all":"chat"),children:e.jsxs(i,{className:"p-4 text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-blue-600 dark:text-blue-400",children:pe}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Perguntas do Site"})]})}),e.jsx(n,{className:"hover:shadow-none hover:translate-y-0",children:e.jsxs(i,{className:"p-4 text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-foreground",children:new Set(c.map(t=>t.category)).size}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Categorias"})]})}),e.jsx(n,{className:"hover:shadow-none hover:translate-y-0",children:e.jsxs(i,{className:"p-4 text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-foreground",children:c.filter(t=>t.document_url).length}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Com Documento"})]})})]}),ne?e.jsx("div",{className:"text-center py-12 text-muted-foreground",children:"A carregar..."}):ee.length===0?e.jsxs("div",{className:"text-center py-12",children:[e.jsx(Le,{className:"h-12 w-12 text-muted-foreground/40 mx-auto mb-3"}),e.jsx("p",{className:"text-muted-foreground",children:"Nenhuma entrada encontrada"}),e.jsxs(d,{onClick:$,variant:"outline",className:"mt-3",children:[e.jsx(se,{className:"h-4 w-4 mr-1"})," Adicionar primeira entrada"]})]}):e.jsx("div",{className:"space-y-3",children:ee.map(t=>{var s;return e.jsx(n,{className:"hover:shadow-none hover:translate-y-0",children:e.jsx(i,{className:"p-4",children:e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx("h3",{className:"font-medium text-foreground truncate",children:t.title}),e.jsx(h,{variant:t.is_active?"default":"secondary",className:"text-[10px]",children:t.is_active?"Ativo":"Inativo"}),k(t.category)==="chat"&&e.jsxs(h,{className:"text-[10px] bg-blue-600 hover:bg-blue-600 text-white border-transparent shrink-0",children:[e.jsx(Pe,{className:"h-3 w-3 mr-0.5"})," Pergunta do Site"]}),k(t.category)==="auto"&&e.jsxs(h,{variant:"secondary",className:"text-[10px] shrink-0",children:[e.jsx(ze,{className:"h-3 w-3 mr-0.5"})," Auto-Enriquecido"]})]}),e.jsx("p",{className:"text-sm text-muted-foreground line-clamp-2",children:t.content}),e.jsxs("div",{className:"flex items-center gap-2 mt-2",children:[e.jsx(h,{variant:"outline",className:"text-[10px]",children:((s=R.find(r=>r.value===t.category))==null?void 0:s.label)||t.category}),e.jsxs("span",{className:"inline-flex items-center gap-1 text-[10px] text-muted-foreground",children:[e.jsx(Ie,{className:"h-3 w-3"}),qe(t.created_at)]}),t.document_url&&e.jsx("a",{href:t.document_url,target:"_blank",rel:"noopener noreferrer",className:"inline-flex items-center gap-1",children:e.jsxs(h,{variant:"outline",className:"text-[10px] text-primary",children:[e.jsx(te,{className:"h-3 w-3 mr-0.5"})," Doc"]})})]})]}),e.jsxs("div",{className:"flex gap-1",children:[e.jsx(d,{variant:"ghost",size:"icon",onClick:()=>xe(t),children:e.jsx(Re,{className:"h-4 w-4"})}),e.jsx(d,{variant:"ghost",size:"icon",onClick:()=>he(t.id),children:e.jsx(re,{className:"h-4 w-4 text-destructive"})})]})]})})},t.id)})})]})})}export{Bt 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.