1import{r as e,j as t}from"./vendor-react-67OMGnKf.js";import{a as s}from"./index-DQlxSk6-.js";import{g as a,o as l,J as r,w as i,Q as n,q as d,c as o,b as c,f as x,d as m,k as h}from"./vendor-firebase-CB4OK_uH.js";import{W as u,Y as p,G as g,Z as b,a as v,N as j,I as f,E as N,V as y,_ as w,$ as k}from"./vendor-icons-CASoZnXC.js";import{b as _}from"./vendor-router-BaIBfPlq.js";import"./vendor-D17qM6Ff.js";import"./vendor-google-9VDdVTee.js";const C=()=>{const{userRole:C,logout:S}=s(),T=_(),A=a(),[E,L]=e.useState([]),[D,I]=e.useState(!0),[R,F]=e.useState(""),[G,P]=e.useState("all"),[U,$]=e.useState(null),[z,V]=e.useState(!0),[M,K]=e.useState(!1),[O,W]=e.useState(null),[Y,q]=e.useState(!1),[B,J]=e.useState(!1),Q={login:{label:"Login",icon:k,color:"text-blue-600",bg:"bg-blue-50"},ai_conversation_start:{label:"AI Chat Started",icon:w,color:"text-green-600",bg:"bg-green-50"},review_enter:{label:"Review Page",icon:f,color:"text-purple-600",bg:"bg-purple-50"},start_over:{label:"Start Over",icon:v,color:"text-orange-600",bg:"bg-orange-50"},save_favorite:{label:"Saved Favorite",icon:f,color:"text-pink-600",bg:"bg-pink-50"},enter_kitchen:{label:"Entered Kitchen",icon:w,color:"text-green-700",bg:"bg-green-100"},video_generation:{label:"Video Generated",icon:y,color:"text-red-600",bg:"bg-red-50"},credit_purchase:{label:"Credits Purchased",icon:N,color:"text-yellow-600",bg:"bg-yellow-50"},recipe_generation:{label:"Recipe Generated",icon:f,color:"text-indigo-600",bg:"bg-indigo-50"},page_navigation:{label:"Page Navigation",icon:f,color:"text-gray-600",bg:"bg-gray-50"}},Z=async(e=!1)=>{I(!0);try{const t=[l("timestamp","desc"),r(50)];"all"!==G&&t.unshift(i("eventType","==",G)),e&&U&&t.push(n(U));const s=d(o(A,"userActivity"),...t),a=await c(s),x=a.docs.map(e=>({id:e.id,...e.data(),timestamp:e.data().timestamp?.toDate()||new Date}));if(R){const t=x.filter(e=>e.userEmail?.toLowerCase().includes(R.toLowerCase())||e.userId?.includes(R));L(e?[...E,...t]:t)}else L(e?[...E,...x]:x);$(a.docs[a.docs.length-1]||null),V(50===a.docs.length)}catch(t){}finally{I(!1)}};e.useEffect(()=>{"admin"===C&&Z()},[C,G]);const H=e=>{if(!e)return"N/A";return`${Math.floor(e/60)}m ${e%60}s`};return"admin"!==C?t.jsx("div",{className:"min-h-screen flex items-center justify-center text-slate-500",children:"Access Restricted"}):t.jsxs("div",{className:"min-h-screen bg-slate-50 text-slate-900 pb-20",children:[t.jsx("div",{className:"bg-white border-b border-slate-200 sticky top-0 z-20 px-6 py-4 shadow-sm",children:t.jsxs("div",{className:"max-w-6xl mx-auto flex items-center justify-between",children:[t.jsxs("div",{className:"flex items-center gap-3",children:[t.jsx("div",{className:"p-2 bg-green-100 rounded-lg text-green-700",children:t.jsx(u,{className:"w-5 h-5"})}),t.jsxs("div",{children:[t.jsx("h1",{className:"text-xl font-bold text-slate-900 leading-none",children:"Activity Logs"}),t.jsx("p",{className:"text-xs text-slate-500 mt-1",children:"Real-time user event tracking"})]})]}),t.jsx("div",{className:"flex items-center gap-3",children:t.jsxs("button",{onClick:()=>T("/admin"),className:"text-sm font-medium text-slate-600 hover:text-slate-900 px-3 py-2 hover:bg-slate-100 rounded-lg transition-colors flex items-center gap-2",children:[t.jsx(p,{className:"w-4 h-4"}),"Back to Admin"]})})]})}),t.jsxs("main",{className:"max-w-6xl mx-auto px-6 py-8 space-y-6",children:[t.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[t.jsxs("div",{className:"bg-white rounded-xl p-4 border border-slate-200 shadow-sm",children:[t.jsx("p",{className:"text-xs font-medium text-slate-500 mb-1",children:"Total Events"}),t.jsx("p",{className:"text-2xl font-bold text-slate-900",children:E.length})]}),t.jsxs("div",{className:"bg-white rounded-xl p-4 border border-slate-200 shadow-sm",children:[t.jsx("p",{className:"text-xs font-medium text-slate-500 mb-1",children:"Videos Generated"}),t.jsx("p",{className:"text-2xl font-bold text-red-600",children:E.filter(e=>"video_generation"===e.eventType).length})]}),t.jsxs("div",{className:"bg-white rounded-xl p-4 border border-slate-200 shadow-sm",children:[t.jsx("p",{className:"text-xs font-medium text-slate-500 mb-1",children:"Credits Sold"}),t.jsx("p",{className:"text-2xl font-bold text-yellow-600",children:E.filter(e=>"credit_purchase"===e.eventType).reduce((e,t)=>
1e+(t.metadata?.creditCount||0),0)})]}),t.jsxs("div",{className:"bg-white rounded-xl p-4 border border-slate-200 shadow-sm",children:[t.jsx("p",{className:"text-xs font-medium text-slate-500 mb-1",children:"Active Users"}),t.jsx("p",{className:"text-2xl font-bold text-blue-600",children:new Set(E.map(e=>e.userId)).size})]})]}),t.jsxs("div",{className:"bg-white rounded-xl p-4 border border-slate-200 shadow-sm flex flex-col md:flex-row gap-4 items-end",children:[t.jsxs("div",{className:"flex-1 w-full relative",children:[t.jsx("label",{className:"block text-xs font-semibold text-slate-500 mb-1",children:"Search User"}),t.jsxs("div",{className:"relative",children:[t.jsx(g,{className:"absolute left-3 top-2.5 w-4 h-4 text-slate-400"}),t.jsx("input",{type:"text",placeholder:"Email or User ID",value:R,onChange:e=>F(e.target.value),className:"w-full pl-9 pr-3 py-2 border border-slate-200 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-green-500 bg-slate-50"})]})]}),t.jsxs("div",{className:"flex-1 w-full",children:[t.jsx("label",{className:"block text-xs font-semibold text-slate-500 mb-1",children:"Event Type"}),t.jsxs("div",{className:"relative",children:[t.jsx(b,{className:"absolute left-3 top-2.5 w-4 h-4 text-slate-400"}),t.jsxs("select",{value:G,onChange:e=>P(e.target.value),className:"w-full pl-9 pr-3 py-2 border border-slate-200 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-green-500 bg-slate-50 appearance-none",children:[t.jsx("option",{value:"all",children:"All Events"}),t.jsx("option",{value:"login",children:"Login"}),t.jsx("option",{value:"ai_conversation_start",children:"AI Chat Started"}),t.jsx("option",{value:"review_enter",children:"Review Page"}),t.jsx("option",{value:"start_over",children:"Start Over"}),t.jsx("option",{value:"save_favorite",children:"Saved Favorite"}),t.jsx("option",{value:"enter_kitchen",children:"Entered Kitchen"}),t.jsx("option",{value:"video_generation",children:"Video Generation"}),t.jsx("option",{value:"credit_purchase",children:"Credit Purchase"}),t.jsx("option",{value:"recipe_generation",children:"Recipe Generation"})]})]})]}),t.jsxs("div",{className:"flex gap-2",children:[t.jsxs("button",{onClick:()=>Z(),disabled:D,className:"px-4 py-2 bg-slate-900 hover:bg-slate-800 text-white rounded-lg font-medium text-sm disabled:opacity-50 flex items-center gap-2",children:[t.jsx(v,{className:"w-4 h-4 "+(D?"animate-spin":"")}),"Refresh"]}),t.jsxs("button",{onClick:()=>{q(!0)},disabled:D||0===E.length,className:"px-4 py-2 bg-red-50 text-red-600 hover:bg-red-100 border border-red-200 rounded-lg font-medium text-sm disabled:opacity-50 flex items-center gap-2",children:[t.jsx(j,{className:"w-4 h-4"}),"Clear"]})]})]}),t.jsxs("div",{className:"bg-white rounded-xl border border-slate-200 shadow-sm overflow-hidden",children:[t.jsx("div",{className:"overflow-x-auto",children:t.jsxs("table",{className:"w-full",children:[t.jsx("thead",{className:"bg-slate-50 border-b border-slate-100",children:t.jsxs("tr",{children:[t.jsx("th",{className:"px-6 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider",children:"Time"}),t.jsx("th",{className:"px-6 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider",children:"User"}),t.jsx("th",{className:"px-6 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider",children:"Event"}),t.jsx("th",{className:"px-6 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider",children:"Details"}),t.jsx("th",{className:"px-6 py-3 text-right text-xs font-semibold text-slate-500 uppercase tracking-wider",children:" "})]})}),t.jsx("tbody",{className:"divide-y divide-slate-100",children:D&&0===E.length?t.jsx("tr",{children:t.jsx("td",{colSpan:"5",className:"px-6 py-12 text-center text-slate-500",children:t.jsxs("div",{className:"flex flex-col items-center",children:[t.jsx(f,{className:"w-8 h-8 text-slate-200 animate-spin mb-2"}),t.jsx("p",{children:"Loading logs..."})]})})}):0===E.length?t.jsx("tr",{children:t.jsx("td",{colSpan:"5",className:"px-6 py-12 text-center text-slate-500",children:"No activity found matching your filters."})}):E.map(e=>{const s=Q[e.eventType]||{label:e.eventType,icon:f,color:"text-slate-500",bg:"bg-slate-100"},a=s.icon;return t.jsxs("tr",{className:"hover:bg-slate-50/80 transition-colors",children:[t.jsx("td",{className:"px-6 py-4 text-xs text-slate-500
1whitespace-nowrap font-mono",children:(l=e.timestamp,l?new Intl.DateTimeFormat("en-US",{month:"short",day:"numeric",hour:"numeric",minute:"2-digit",hour12:!0}).format(l):"N/A")}),t.jsxs("td",{className:"px-6 py-4",children:[t.jsx("div",{className:"text-sm font-medium text-slate-900",children:e.userEmail||"Unknown"}),t.jsxs("div",{className:"text-xs text-slate-400 font-mono",children:[e.userId?.substring(0,8),"..."]})]}),t.jsx("td",{className:"px-6 py-4",children:t.jsxs("span",{className:`inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium ${s.bg} ${s.color}`,children:[t.jsx(a,{className:"w-3 h-3"}),s.label]})}),t.jsxs("td",{className:"px-6 py-4 text-sm text-slate-600",children:["video_generation"===e.eventType&&t.jsxs("div",{className:"text-xs space-y-0.5",children:[t.jsx("div",{className:"font-medium text-slate-900",children:e.metadata?.dishName}),t.jsxs("div",{children:[e.metadata?.sceneCount," scenes · ",H(e.metadata?.generationTimeSeconds)]}),t.jsx("div",{className:"success"===e.metadata?.status?"text-green-600":"text-red-600",children:e.metadata?.status})]}),"credit_purchase"===e.eventType&&t.jsxs("div",{className:"text-xs space-y-0.5",children:[t.jsxs("div",{className:"font-medium text-green-600",children:["+",e.metadata?.creditCount," Credits"]}),t.jsxs("div",{children:["$",e.metadata?.amountPaid]})]}),["review_enter","enter_kitchen","save_favorite","recipe_generation"].includes(e.eventType)&&t.jsx("div",{className:"text-xs font-medium text-slate-900",children:e.metadata?.dishName||"-"}),"ai_conversation_start"===e.eventType&&t.jsxs("div",{className:"text-xs italic text-slate-500 break-words max-w-xs",children:['"',e.metadata?.initialMessage,'"']}),"start_over"===e.eventType&&t.jsxs("div",{className:"text-xs text-slate-500",children:["Source: ",e.metadata?.source]}),!e.metadata?.dishName&&!e.metadata?.initialMessage&&!e.metadata?.creditCount&&"start_over"!==e.eventType&&t.jsx("span",{className:"text-xs text-slate-400",children:e.metadata?.action||"-"})]}),t.jsx("td",{className:"px-6 py-4 text-right",children:t.jsx("button",{onClick:()=>{return t=e.id,W(t),void K(!0);var t},className:"text-slate-400 hover:text-red-600 p-1.5 hover:bg-red-50 rounded-lg transition-colors",title:"Delete entry",children:t.jsx(j,{className:"w-4 h-4"})})})]},e.id);var l})})]})}),z&&E.length>0&&t.jsx("div",{className:"p-4 border-t border-slate-100 bg-slate-50/50 text-center",children:t.jsx("button",{onClick:()=>Z(!0),disabled:D,className:"text-sm text-slate-600 hover:text-slate-900 font-medium hover:underline disabled:opacity-50",children:D?"Loading...":"Load older records"})})]}),M&&t.jsx("div",{className:"fixed inset-0 bg-slate-900/40 backdrop-blur-sm flex items-center justify-center z-50",children:t.jsxs("div",{className:"bg-white rounded-xl p-6 max-w-sm w-full mx-4 shadow-xl",children:[t.jsx("h3",{className:"text-lg font-bold text-slate-900 mb-2",children:"Delete Log Entry?"}),t.jsx("p",{className:"text-sm text-slate-500 mb-6",children:"This action cannot be undone."}),t.jsxs("div",{className:"flex gap-3 justify-end",children:[t.jsx("button",{onClick:()=>{K(!1),W(null)},className:"px-4 py-2 bg-slate-100 hover:bg-slate-200 text-slate-700 rounded-lg font-medium text-sm",children:"Cancel"}),t.jsx("button",{onClick:async()=>{if(O)try{await x(m(A,"userActivity",O)),L(E.filter(e=>e.id!==O)),K(!1),W(null)}catch(e){alert("Failed to delete log entry: "+e.message)}},className:"px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg font-medium text-sm",children:"Delete"})]})]})}),Y&&t.jsx("div",{className:"fixed inset-0 bg-slate-900/40 backdrop-blur-sm flex items-center justify-center z-50",children:t.jsxs("div",{className:"bg-white rounded-xl p-6 max-w-sm w-full mx-4 shadow-xl",children:[t.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[t.jsx("div",{className:"p-2 bg-red-100 rounded-full text-red-600",children:t.jsx(j,{className:"w-5 h-5"})}),t.jsx("h3",{className:"text-lg font-bold text-slate-900",children:"Clear Logs?"})]}),t.jsxs("p",{className:"text-sm text-slate-500 mb-6",children:["Are you sure you want to delete ",t.jsxs("strong",{children:["ALL ",E.length," activity logs"]}),"? This is a destructive action."]}),t.jsxs("div",{className:"flex gap-3 justify-end",children:[t.jsx("button",{onClick:()=>q(!1),className:"px-4 py-2 bg-slate-100 hover:bg-slate-200 text-slate-700 rounded-lg font-medium text-sm",children:"Cancel"}),t.jsx("button",{onClick:()=>{q(!1),J(!0)},className:"px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg font-medium text-sm",children:"Continue"})]})]})}),B&&t.jsx("div",{className:"fixed inset-0 bg-slate-900/60 backdrop-blur-sm flex items-center justify-center z-50",children:t.jsxs("div",{className:"bg-white rounded-xl p-6 max-w-sm w-full mx-4 shadow-2xl border-2 border-red-500 relative overflow-hidden",children:[t.jsx("div",{className:"absolute top-0 left-0 w-full h-1 bg-gradient-to-r from-red-500 via-orange-500 to-red-500"}),t.jsx("h3",{className:"text-lg font-bold text-red-600 mb-2",children:"â ï¸ Final Warning"}),t.jsxs("p",{className:"text-sm text-slate-600 mb-6",children:["This will ",t.jsx("strong",{children:"permanently delete all activity history"}),". There is no undo."]}),t.jsxs("div",{className:"flex gap-3 justify-end",children:[t.jsx("button",{onClick:()=>J(!1),className:"px-4 py-2 bg-slate-100 hover:bg-slate-200 text-slate-700 rounded-lg font-medium text-sm",children:"Cancel"}),t.jsx("button",{onClick:async()=>{I(!0),J(!1);try{const e=h(A);
1E.forEach(t=>{e.delete(m(A,"userActivity",t.id))}),await e.commit(),L([]),alert("All activity logs cleared successfully")}catch(e){alert("Failed to clear logs: "+e.message)}finally{I(!1)}},className:"px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg font-bold text-sm shadow-lg shadow-red-200",children:"Yes, Delete Everything"})]})]})})]})]})};export{C 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.