1import{j as t}from"./ui-vendor-DSl4G1OJ.js";import{j as re,cc as h,p as $,q as y,r as f,au as R,D as ie,e as le,f as ce,g as oe,h as de,i as pe,B as A,R as ue}from"./index-iv8p4bGb.js";import{a as l}from"./react-vendor-JYAUlju1.js";import{B as F}from"./badge-BmcgN26q.js";import{T as me,a as xe,b as he,c as z}from"./tabs-Fwe9v5pI.js";import{T as U,a as H,b as w,c as o,d as Y,e as d}from"./table-Dfj6cfpN.js";import{E as ge}from"./ExpiryPicker-CnR7ATtZ.js";import{p as v}from"./partnerAdminUrls-B8hM0sol.js";import{B as je}from"./book-open-DtepqIpV.js";import{N as ye}from"./newspaper-CNavWVwr.js";import{M as fe}from"./megaphone-eKmCVPjV.js";import{U as we}from"./users-DbO-ebqj.js";import{C as ve}from"./calendar-days-BVXeHu32.js";import{U as _e}from"./utensils-crossed-DN5plLEW.js";import{F as q}from"./file-text-BAux0Bto.js";import"./input-Cbpm_RuX.js";import"./addWeeks-DxCx_8Kd.js";import"./supabaseFunctions-Xsh4_ii9.js";const be="https://kzimtscphksfkimrwneu.supabase.co",Ne={knowledge:q,meal:_e,event:ve,spotlight:we,announcement:fe,news:ye,article:je},Ee={knowledge:"bg-blue-500/10 text-blue-400",meal:"bg-orange-500/10 text-orange-400",event:"bg-purple-500/10 text-purple-400",spotlight:"bg-yellow-500/10 text-yellow-400",announcement:"bg-red-500/10 text-red-400",news:"bg-green-500/10 text-green-400",article:"bg-cyan-500/10 text-cyan-400"},Ce={knowledge:"Knowledge",meal:"Meal",event:"Event",spotlight:"Spotlight",announcement:"Announcement",news:"News",article:"Article"};async function _(b,N={},r){const n=new URLSearchParams({entity:b,...N}).toString(),m={"Content-Type":"application/json"};r&&(m.Authorization=`Bearer ${r}`);const g=await h(`${be}/functions/v1/admin-portal?${n}`,{headers:m});return g.ok?g.json():{items:[]}}function Ke({expiredFiles:b,onRefresh:N,portalId:r,authToken:n}){const{toast:m}=re(),[g,K]=l.useState("all"),[J,G]=l.useState([]),[Q,V]=l.useState([]),[W,X]=l.useState([]),[E,Z]=l.useState([]),[I,ee]=l.useState(!0),[p,j]=l.useState(null),[D,C]=l.useState(null),[k,M]=l.useState(!1),S=l.useCallback(async()=>{try{const e=n?{headers:{"Content-Type":"application/json",Authorization:`Bearer ${n}`}}:{credentials:"include"},[s,i]=await Promise.all([h(v("partner-planner",r,{type:"meals",status:"expired"}),e),h(v("partner-planner",r,{type:"events",status:"expired"}),e)]);if(s.ok){const u=await s.json();G(u.items||[])}if(i.ok){const u=await i.json();V(u.items||[])}}catch(e){console.error("Failed to fetch expired items:",e)}},[n,r]),B=l.useCallback(async()=>{if(!(!r||!n))try{const[e,s,i,u]=await Promise.all([_("spotlights",{portal_id:r},n),_("announcements",{portal_id:r},n),_("news",{portal_id:r},n),_("articles",{portal_id:r},n)]),x=[];(e.items||[]).filter(a=>a.status!=="active").forEach(a=>x.push({id:a.id,title:a.person_name,type:"spotlight",created_at:a.created_at,expires_at:null,status:a.status})),(s.items||[]).filter(a=>a.expires_at&&new Date(a.expires_at)<new Date).forEach(a=>x.push({id:a.id,title:a.title,type:"announcement",created_at:a.created_at,expires_at:a.expires_at,status:"expired"})),(i.items||[]).filter(a=>a.status!=="active").forEach(a=>x.push({id:a.id,title:a.title,type:"news",created_at:a.created_at,expires_at:a.expires_at,status:a.status})),(u.items||[]).filter(a=>a.status!=="published").forEach(a=>x.push({id:a.id,title:a.title,type:"article",created_at:a.created_at,expires_at:null,status:a.status})),X(x)}catch(e){console.error("Failed to fetch portal content:",e)}},[r,n]),L=l.useCallback(async()=>{try{const e=n?{headers:{"Content-Type":"application/json",Authorization:`Bearer ${n}`}}:{credentials:"include"},s=await h(v("partner-planner",r,{type:"audit"}),e);if(s.ok){const i=await s.json();Z(i.items||[])}}catch(e){console.error("Failed to fetch audit log:",e)}},[n,r]);l.useEffect(()=>{Promise.all([S(),L(),B()]).finally(()=>ee(!1))},[S,L,B]);const T=[...b.map(e=>({id:e.id,title:e.title,type:"knowledge",created_at:e.created_at,expires_at:e.expires_at,status:e.status})),...J.map(e=>({id:e.id,title:e.title,type:"meal",created_at:e.created_at,expires_at:e.expires_at,status:e.status,meta:{meal_type:e.meal_type}})),...Q.map(e=>({id:e.id,title:e.title,type:"event",created_at:e.created_at,expires_at:e.expires_at,status:e.status,meta:{category:e.category}})),...W],O=[...T].sort((e,s)=>new Date(s.expires_at||s.created_at).getTime()-new Date(e.expires_at||e.created_at).getTime()),c={};
1T.forEach(e=>{c[e.type]=(c[e.type]||0)+1});const te=e=>e==="all"?O:O.filter(s=>s.type===e),se=e=>["knowledge","meal","event"].includes(e),ae=async()=>{if(p){M(!0);try{const e={knowledge:"knowledge",meal:"meals",event:"events"},s=n?{headers:{"Content-Type":"application/json",Authorization:`Bearer ${n}`}}:{credentials:"include",headers:{"Content-Type":"application/json"}},i=await h(v("partner-planner",r,{action:"restore",type:e[p.type],id:p.id}),{method:"POST",...s,body:JSON.stringify({expires_at:D})});if(i.ok)m({title:"Restored",description:`"${p.title}" is active again`}),j(null),C(null),N(),S();else{const u=await i.json();throw new Error(u.error||"Restore failed")}}catch(e){m({title:"Error",description:e.message,variant:"destructive"})}finally{M(!1)}}};if(I)return t.jsx("div",{className:"flex justify-center py-8",children:t.jsx($,{className:"h-6 w-6 animate-spin text-muted-foreground"})});const P=[{value:"all",label:"All",count:T.length},{value:"meal",label:"Meals",count:c.meal||0},{value:"event",label:"Events",count:c.event||0},{value:"knowledge",label:"Knowledge",count:c.knowledge||0},{value:"spotlight",label:"Spotlights",count:c.spotlight||0},{value:"announcement",label:"Announcements",count:c.announcement||0},{value:"news",label:"News",count:c.news||0},{value:"article",label:"Articles",count:c.article||0},{value:"audit",label:"Audit Log",count:E.length}].filter(e=>e.count>0||e.value==="all"||e.value==="audit"),ne=e=>e.length===0?t.jsx(y,{children:t.jsx(f,{className:"py-8 text-center text-muted-foreground",children:"No expired items."})}):t.jsx(y,{children:t.jsx(f,{className:"p-0",children:t.jsxs(U,{children:[t.jsx(H,{children:t.jsxs(w,{children:[t.jsx(o,{children:"Title"}),t.jsx(o,{children:"Type"}),t.jsx(o,{children:"Created"}),t.jsx(o,{children:"Expired"}),t.jsx(o,{className:"text-right",children:"Action"})]})}),t.jsx(Y,{children:e.map(s=>{const i=Ne[s.type]||q;return t.jsxs(w,{children:[t.jsx(d,{className:"font-medium",children:t.jsxs("div",{className:"flex items-center gap-2",children:[t.jsx(i,{className:"h-4 w-4 text-muted-foreground shrink-0"}),t.jsx("span",{className:"truncate max-w-[200px]",children:s.title})]})}),t.jsx(d,{children:t.jsx(F,{variant:"secondary",className:`text-xs ${Ee[s.type]}`,children:Ce[s.type]})}),t.jsx(d,{className:"text-sm text-muted-foreground",children:R(new Date(s.created_at),"EEE, d MMM yyyy")}),t.jsx(d,{className:"text-sm text-muted-foreground",children:s.expires_at?R(new Date(s.expires_at),"EEE, d MMM yyyy"):"â"}),t.jsx(d,{className:"text-right",children:se(s.type)?t.jsxs(A,{variant:"outline",size:"sm",className:"gap-1",onClick:()=>{j(s),C(null)},children:[t.jsx(ue,{className:"h-3 w-3"})," Restore"]}):t.jsx("span",{className:"text-xs text-muted-foreground",children:s.status})})]},`${s.type}-${s.id}`)})})]})})});return t.jsxs("div",{className:"space-y-4",children:[t.jsxs(me,{value:g,onValueChange:K,children:[t.jsx(xe,{className:"flex-wrap h-auto gap-1",children:P.map(e=>t.jsxs(he,{value:e.value,children:[e.label," (",e.count,")"]},e.value))}),P.filter(e=>e.value!=="audit").map(e=>t.jsx(z,{value:e.value,children:ne(te(e.value))},e.value)),t.jsx(z,{value:"audit",children:E.length===0?t.jsx(y,{children:t.jsx(f,{className:"py-8 text-center text-muted-foreground",children:"No audit log entries yet."})}):t.jsx(y,{children:t.jsx(f,{className:"p-0",children:t.jsxs(U,{children:[t.jsx(H,{children:t.jsxs(w,{children:[t.jsx(o,{children:"Action"}),t.jsx(o,{children:"Type"}),t.jsx(o,{children:"Details"}),t.jsx(o,{children:"Date"})]})}),t.jsx(Y,{children:E.map(e=>t.jsxs(w,{children:[t.jsx(d,{children:t.jsx(F,{variant:"outline",className:`capitalize ${e.action==="create"?"border-green-500/50 text-green-600":e.action==="delete"?"border-destructive/50 text-destructive":""}`,children:e.action})}),t.jsx(d,{className:"text-sm text-muted-foreground capitalize",children:e.entity_type.replace(/_/g," ")}),t.jsxs(d,{className:"text-sm text-muted-foreground max-w-[300px]",children:[t.jsx("p",{className:"truncate font-medium",children:e.summary?.title||e.entity_id.slice(0,8)}),e.summary?.changes&&t.jsxs("div",{className:"mt-1 space-y-0.5",children:[Object.entries(e.summary.changes).slice(0,3).map(([s,i])=>
1t.jsxs("p",{className:"text-xs text-muted-foreground/70 truncate",children:[t.jsxs("span",{className:"font-medium",children:[s.replace(/_/g," "),":"]})," ",t.jsx("span",{className:"line-through opacity-60",children:String(i.from??"â").slice(0,30)})," â ",t.jsx("span",{children:String(i.to??"â").slice(0,30)})]},s)),Object.keys(e.summary.changes).length>3&&t.jsxs("p",{className:"text-xs text-muted-foreground/50",children:["+",Object.keys(e.summary.changes).length-3," more fields"]})]})]}),t.jsx(d,{className:"text-sm text-muted-foreground",children:R(new Date(e.created_at),"EEE, d MMM yyyy, h:mm a")})]},e.id))})]})})})})]}),t.jsx(ie,{open:!!p,onOpenChange:e=>{e||j(null)},children:t.jsxs(le,{children:[t.jsxs(ce,{children:[t.jsxs(oe,{children:['Restore "',p?.title,'"']}),t.jsx(de,{children:"This will make the item active again. Set a new expiry if needed."})]}),t.jsx(ge,{value:D,onChange:C,label:p?.type==="knowledge"?"Set new expiry (optional)":"Set custom expiry (or leave for default)"}),t.jsxs(pe,{children:[t.jsx(A,{variant:"outline",onClick:()=>j(null),children:"Cancel"}),t.jsxs(A,{onClick:ae,disabled:k,children:[k?t.jsx($,{className:"h-4 w-4 animate-spin mr-2"}):null,"Restore"]})]})]})})]})}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.