import{$ as r,K as e,g as y,p as N,o as b,a9 as i}from"./index-CqfREQhZ.js";import{P as v,C as w,B as C}from"./AdminShell-Dht4Lyqh.js";import{R as k}from"./rotate-ccw-C_nThO4u.js";import{C as E}from"./check-DhiioKnT.js";import{T as R}from"./trash-2-4jKaeX1p.js";import"./admin-B-WQz-EM.js";import"./products-Q1MHi1iw.js";import"./shopping-bag-CIMOvT4N.js";import"./package-D58BUA8K.js";import"./users-Bozo9HJ_.js";import"./star-DW-815Mn.js";import"./settings-Bm4D1Whm.js";import"./plus-DKTwDupB.js";const M={contact:y},_={contact:"#002D72"};function H(){const[m,n]=r.useState([]),[x,p]=r.useState(!0),[d,o]=r.useState(!1),[f,u]=r.useState(null);async function h(){p(!0),u(null);try{const t=await N("/api/sql/notifications?eq.type.contact&order=created_at.desc");n(Array.isArray(t)?t:[])}catch(t){u(t instanceof Error?t.message:"Failed to load messages.")}finally{p(!1)}}r.useEffect(()=>{h()},[]);async function g(t){o(!0);try{const s=await b(`/api/sql/notifications/${encodeURIComponent(t)}`,{method:"PATCH",headers:{"content-type":"application/json"},body:JSON.stringify({is_read:1})});if(!s.ok)throw new Error("Unable to update message");const a=await s.json();n(c=>c.map(l=>l.id===t?{...l,is_read:1}:l)),i.success(`Marked "${a.title}" as read.`)}catch(s){i.error(s instanceof Error?s.message:"Unable to update message.")}finally{o(!1)}}async function j(t){if(confirm("Delete this message? This cannot be undone.")){o(!0);try{if(!(await b(`/api/sql/notifications/${encodeURIComponent(t)}`,{method:"DELETE"})).ok)throw new Error("Unable to delete message");n(a=>a.filter(c=>c.id!==t)),i.success("Message deleted.")}catch(s){i.error(s instanceof Error?s.message:"Unable to delete message.")}finally{o(!1)}}}return e.jsxs(e.Fragment,{children:[e.jsx(v,{title:"Contact Messages",subtitle:"Review inbound customer messages, mark them read, or delete handled items.",actions:e.jsxs("button",{onClick:h,disabled:x||d,className:"inline-flex items-center gap-2 bg-card border border-border px-4 h-10 rounded-full text-sm font-medium hover:bg-secondary transition disabled:opacity-60",children:[e.jsx(k,{className:"w-4 h-4"})," Refresh"]})}),e.jsx(w,{className:"overflow-hidden",children:x?e.jsx("div",{className:"p-8 text-center text-sm text-muted-foreground",children:"Loading messages…"}):f?e.jsx("div",{className:"p-8 text-center text-sm text-destructive",children:f}):m.length===0?e.jsx("div",{className:"p-8 text-center text-sm text-muted-foreground",children:"No contact messages yet."}):e.jsx("ul",{children:m.map(t=>{const s=M.contact??C,a=_.contact;return e.jsxs("li",{className:`flex flex-col gap-4 px-5 py-5 border-b border-border last:border-0 ${t.is_read?"bg-transparent":"bg-[#002D72]/5"}`,children:[e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-start sm:justify-between gap-3",children:[e.jsxs("div",{className:"flex items-start gap-4",children:[e.jsx("div",{className:"w-11 h-11 rounded-xl grid place-items-center shrink-0",style:{background:`${a}1a`,color:a},children:e.jsx(s,{className:"w-5 h-5"})}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("h4",{className:"font-semibold text-sm",children:t.title}),!t.is_read&&e.jsx("span",{className:"w-2 h-2 rounded-full bg-[#FF7A00]"})]}),e.jsx("div",{className:"text-xs text-muted-foreground mt-1",children:t.time})]})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[e.jsxs("button",{disabled:t.is_read||d,onClick:()=>{g(t.id)},className:"inline-flex items-center gap-2 border border-border bg-card px-3 py-2 rounded-full text-xs font-medium hover:bg-secondary transition disabled:opacity-60",children:[e.jsx(E,{className:"w-3.5 h-3.5"})," ",t.is_read?"Read":"Mark read"]}),e.jsxs("button",{disabled:d,onClick:()=>{j(t.id)},className:"inline-flex items-center gap-2 border border-destructive text-destructive bg-[#ffeaea] px-3 py-2 rounded-full text-xs font-medium hover:bg-[#ffe0e0] transition disabled:opacity-60",children:[e.jsx(R,{className:"w-3.5 h-3.5"})," Delete"]})]})]}),e.jsx("p",{className:"text-sm text-muted-foreground whitespace-pre-wrap",children:t.body})]},t.id)})})})]})}export{H as component};
