1import{j as e}from"./ui-E7cH9GT0.js";import{r as P}from"./vendor-Cpi2Ks0G.js";import{V as z,C as u,d as x,e as g,f as h,ay as X,p as Y,aM as Z,B as $,X as m,N as ee,aV as se,aW as L,b as _,a1 as Q,H as te,av as I,J as ae,O as N,Q as A}from"./index-BeEEDwOo.js";import{T as re}from"./textarea-Col2DGGw.js";import{T as le,a as ie,b as M,c as D}from"./tabs-CEbq2Q7W.js";import{S as ne}from"./switch-D35H7H5i.js";import{E as oe}from"./Editor-Deml7c3W.js";import{E as H}from"./eye-BEvqaTS6.js";import{E as ce}from"./eye-off-BMiZ_fDQ.js";import{S as de}from"./square-pen-BB3HS2G3.js";import"./router-CKghwl56.js";const me=()=>{const{data:f,isLoading:b}=z({queryKey:["blog-analytics"],queryFn:async()=>{const{data:t,error:r}=await m.from("blog_posts").select(` 2 id, 3 title, 4 slug, 5 created_at, 6 blog_post_stats ( 7 total_views, 8 unique_views, 9 country_stats 10 ) 11 `).eq("published",!0).order("created_at",{ascending:!1});if(r)throw r;return t.map(i=>({...i,total_views:i.blog_post_stats?.[0]?.total_views||0,unique_views:i.blog_post_stats?.[0]?.unique_views||0,country_stats:i.blog_post_stats?.[0]?.country_stats||{}}))}}),j=f?.reduce((t,r)=>t+(r.total_views||0),0)||0,C=f?.reduce((t,r)=>t+(r.unique_views||0),0)||0,l=f?.reduce((t,r)=>(Object.entries(r.country_stats||{}).forEach(([i,y])=>{t[i]||(t[i]={count:0,name:y.name}),t[i].count+=y.count}),t),{}),o=Object.entries(l||{}).sort(([,t],[,r])=>r.count-t.count).slice(0,5);return b?e.jsx("div",{className:"text-center py-8",children:"Loading analytics..."}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-6",children:[e.jsxs(u,{children:[e.jsxs(x,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(g,{className:"text-sm font-medium",children:"Total Views"}),e.jsx(H,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(h,{children:e.jsx("div",{className:"text-2xl font-bold",children:j.toLocaleString()})})]}),e.jsxs(u,{children:[e.jsxs(x,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(g,{className:"text-sm font-medium",children:"Unique Views"}),e.jsx(X,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(h,{children:e.jsx("div",{className:"text-2xl font-bold",children:C.toLocaleString()})})]}),e.jsxs(u,{children:[e.jsxs(x,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(g,{className:"text-sm font-medium",children:"Countries"}),e.jsx(Y,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(h,{children:e.jsx("div",{className:"text-2xl font-bold",children:Object.keys(l||{}).length})})]}),e.jsxs(u,{children:[e.jsxs(x,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(g,{className:"text-sm font-medium",children:"Posts"}),e.jsx(Z,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(h,{children:e.jsx("div",{className:"text-2xl font-bold",children:f?.length||0})})]})]}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[e.jsxs(u,{children:[e.jsx(x,{children:e.jsx(g,{children:"Top Countries"})}),e.jsx(h,{children:e.jsx("div",{className:"space-y-3",children:o.map(([t,r])=>e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx("span",{className:"text-lg",children:t}),e.jsx("span",{className:"text-sm text-muted-foreground",children:r.name})]}),e.jsxs($,{variant:"secondary",children:[r.count," views"]})]},t))})})]}),e.jsxs(u,{children:[e.jsx(x,{children:e.jsx(g,{children:"Most Viewed Posts"})}),e.jsx(h,{children:e.jsx("div",{className:"space-y-3",children:f?.sort((t,r)=>(r.total_views||0)-(t.total_views||0)).slice(0,5).map(t=>e.jsxs("div",{className:"flex justify-between items-start",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-sm font-medium truncate",children:t.title}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[t.unique_views," unique views"]})]}),e.jsxs($,{variant:"secondary",className:"ml-2",children:[t.total_views," views"]})]},t.id))})})]})]}),e.jsxs(u,{children:[e.jsx(x,{children:e.jsx(g,{children:"All Posts Performance"})}),e.jsx(h,{children:e.jsx("div",{className:"space-y-4",children:f?.map(t=>{const r=Object.entries(t.country_stats||{}).sort(([,i],[,y])=>y.count-i.count)[0];return e.jsxs("div",{className:"flex justify-between items-center p-4 border rounded-lg",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("h3",{className:"font-medium truncate",children:t.title}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Published ",new Date(t.created_at).toLocaleDateString()]}),r&&e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:["Top country: ",r[1].name,"
11(",r[1].count," views)"]})]}),e.jsxs("div",{className:"text-right",children:[e.jsxs("p",{className:"text-sm font-medium",children:[t.total_views," views"]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[t.unique_views," unique"]})]})]},t.id)})})})]})]})},_e=()=>{const[f,b]=P.useState("list"),[j,C]=P.useState(null),[l,o]=P.useState({title:"",body:"",slug:"",excerpt:"",banner_image_url:"",featured_image_url:"",published:!1}),[t,r]=P.useState(null),[i,y]=P.useState(null),{toast:v}=ee(),E=se(),{data:V=[],isLoading:R,error:B}=z({queryKey:["admin-blog-posts"],queryFn:async()=>{console.log("Fetching blog posts...");const{data:s,error:a}=await m.from("blog_posts").select("*").order("created_at",{ascending:!1});if(a)throw console.error("Error fetching blog posts:",a),a;return console.log("Blog posts fetched:",s),s},retry:3,retryDelay:1e3}),k=async(s,a)=>{const c=s.name.split(".").pop(),d=`${Date.now()}-${Math.random().toString(36).substring(2)}.${c}`,n=`${a}/${d}`,{data:p,error:w}=await m.storage.from("product-images").upload(n,s);if(w)throw w;const{data:T}=m.storage.from("product-images").getPublicUrl(n);return T.publicUrl},S=s=>s.toLowerCase().replace(/[^\w\s-]/g,"").replace(/\s+/g,"-").trim(),F=L({mutationFn:async s=>{console.log("Creating blog post with data:",s);let a=s.banner_image_url,c=s.featured_image_url;t&&(a=await k(t,"banners")),i&&(c=await k(i,"featured"));const d={...s,banner_image_url:a,featured_image_url:c,slug:s.slug||S(s.title),author_id:null};console.log("Final post data to insert:",d);const{data:n,error:p}=await m.from("blog_posts").insert([d]).select().single();if(p)throw console.error("Error creating blog post:",p),p;return n},onSuccess:()=>{E.invalidateQueries({queryKey:["admin-blog-posts"]}),v({title:"Blog post created successfully!"}),b("list"),q()},onError:s=>{console.error("Create blog post error:",s),v({title:"Error creating blog post",description:s.message,variant:"destructive"})}}),U=L({mutationFn:async({id:s,...a})=>{console.log("Updating blog post:",s,a);let c=a.banner_image_url,d=a.featured_image_url;t&&(c=await k(t,"banners")),i&&(d=await k(i,"featured"));const n={...a,banner_image_url:c,featured_image_url:d,slug:a.slug||S(a.title),updated_at:new Date().toISOString()},{data:p,error:w}=await m.from("blog_posts").update(n).eq("id",s).select().single();if(w)throw console.error("Error updating blog post:",w),w;return p},onSuccess:()=>{E.invalidateQueries({queryKey:["admin-blog-posts"]}),v({title:"Blog post updated successfully!"}),b("list"),q()},onError:s=>{console.error("Update blog post error:",s),v({title:"Error updating blog post",description:s.message,variant:"destructive"})}}),K=L({mutationFn:async s=>{console.log("Deleting blog post:",s);const{error:a}=await m.from("blog_posts").delete().eq("id",s);if(a)throw console.error("Error deleting blog post:",a),a},onSuccess:()=>{E.invalidateQueries({queryKey:["admin-blog-posts"]}),v({title:"Blog post deleted successfully!"})},onError:s=>{console.error("Delete blog post error:",s),v({title:"Error deleting blog post",description:s.message,variant:"destructive"})}}),q=()=>{o({title:"",body:"",slug:"",excerpt:"",banner_image_url:"",featured_image_url:"",published:!1}),C(null),r(null),y(null)},W=s=>{if(s.preventDefault(),!l.title.trim()){v({title:"Error",description:"Title is required",variant:"destructive"});return}const a={...l,slug:l.slug||S(l.title)};j?U.mutate({id:j.id,...a}):F.mutate(a)},G=s=>{C(s),o({title:s.title,body:s.body||"",slug:s.slug,excerpt:s.excerpt||"",banner_image_url:s.banner_image_url||"",featured_image_url:s.featured_image_url||"",published:s.published||!1}),b("form")};return B?(console.error("BlogPostManagement error:",B),e.jsxs("div",{className:"p-6",children:[e.jsxs("div",{className:"text-red-400",children:["Error loading blog posts: ",B.message]}),e.jsx(_,{onClick:()=>E.invalidateQueries({queryKey:["admin-blog-posts"]}),className:"mt-4 bg-cyan-500 hover:bg-cyan-600",children:"Retry"})]})):e.jsxs("div",{className:"p-6 max-w-7xl mx-auto",children:[e.jsxs("div",{className:"mb-6",children:[e.jsx("h1",{className:"text-3xl font-bold text-white mb-2",children:"Blog Post Management"}),e.jsx("p",{className:"text-gray-300",children:"Create and manage your blog content"})]}),e.jsxs(le,{value:f,onValueChange:b,children:[e.jsxs(ie,{className:"mb-6",children:[e.jsx(M,{value:"list",children:"Blog Posts"}),e.jsx(M,{value:"form",children:j?"Edit Post":"Create Post"}),e.jsx(M,{value:"analytics",children:"Analytics"})]}),e.jsx(D,{value:"list",children:e.jsxs(u,{className:"bg-slate-800/50 border-cyan-500/20",children:[e.jsxs(x,{className:"flex flex-row items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(g,{className:"text-white",children:"Blog Posts"}),e.jsx(Q,{className:"text-gray-300",children:"Manage your blog content"})]}),e.jsxs(_,{onClick:()=>{q(),b("form")},className:"bg-cyan-500 hover:bg-cyan-600",children:[e.jsx(te,{className:"h-4 w-4 mr-2"}),"Create Post"]})]}),e.jsx(h,{children:R?e.jsx("div",{className:"text-center py-8",children:e.jsx("div",{className:"text-white",children:"Loading blog posts..."})}):e.jsx("div",{className:"space-y-4",children:V.length>0?V.map(s=>e.jsx("div",{className:"border border-slate-600 rounded-lg p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center space-x-4",children:[e.jsx("div",{className:"w-16 h-16 bg-slate-700 rounded-lg flex items-center justify-center",children:s.banner_image_url?e.jsx("img",{src:s.banner_image_url,alt:s.title,className:"w-full h-full object-cover rounded-lg"}):e.jsx(I,{className:"h-6 w-6 text-gray-400"})}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-white font-medium",children:s.title}),e.jsxs("p",{className:"text-gray-400 text-sm",children:["Slug: /",s.slug]}),e.jsxs("div",{className:"flex items-center space-x-4 mt-2",children:[e.jsx($,{variant:s.published?"default":"secondary",children:s.published?e.jsxs(e.Fragment,{children:[e.jsx(H,{className:"h-3 w-3 mr-1"}),"Published"]}):e.jsxs(e.Fragment,{children:[e.jsx(ce,{className:"h-3 w-3 mr-1"}),"Draft"]})}),e.jsx("span",{className:"text-gray-400 text-sm",children:new Date(s.created_at).toLocaleDateString()})]})]})]}),e.jsxs("div",{className:"flex space-x-2",children:[e.jsx(_,{variant:"outline",size:"sm",onClick:()=>G(s),className:"border-cyan-500/50 text-cyan-400 hover:bg-cyan-500/10",children:e.jsx(de,{className:"h-4 w-4"})}),e.jsx(_,{variant:"outline",size:"sm",onClick:()=>K.mutate(s.id),className:"border-red-500/50 text-red-400 hover:bg-red-500/10",disabled:K.isPending,children:e.jsx(ae,{className:"h-4 w-4"})})]})]})},s.id)):e.jsxs("div",{className:"text-center py-8",children:[e.jsx(I,{className:"h-12 w-12 text-gray-400 mx-auto mb-4"}),e.jsx("p",{className:"text-gray-400",children:"No blog posts found. Create your first post!"})]})})})]})}),e.jsx(D,{value:"form",children:e.jsxs(u,{className:"bg-slate-800/50 border-cyan-500/20",children:[e.jsxs(x,{children:[e.jsx(g,{className:"text-white",children:j?"Edit Blog Post":"Create New Blog Post"}),e.jsx(Q,{className:"text-gray-300",children:j?"Update blog post content":"Create a new blog post for your audience"})]}),e.jsx(h,{children:e.jsxs("form",{onSubmit:W,className:"space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-6",children:[e.jsxs("div",{children:[e.jsx(N,{className:"block text-sm font-medium text-white mb-2",children:"Post Title *"}),e.jsx(A,{value:l.title,onChange:s=>{const a=s.target.value;o({...l,title:a,slug:l.slug||S(a)})},placeholder:"Enter post title",required:!0,className:"bg-slate-700 border-slate-600 text-white"})]}),e.jsxs("div",{children:[e.jsx(N,{className:"block text-sm font-medium text-white mb-2",children:"URL Slug"}),e.jsx(A,{value:l.slug,onChange:s=>o({...l,slug:s.target.value}),placeholder:"url-slug-for-post",className:"bg-slate-700 border-slate-600 text-white"})]})]}),e.jsxs("div",{children:[e.jsx(N,{className:"block text-sm font-medium text-white mb-2",children:"Banner Image"}),e.jsx("input",{type:"file",accept:"image/*",onChange:s=>
11r(s.target.files?.[0]||null),className:"block w-full text-sm text-gray-300 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-cyan-50 file:text-cyan-700 hover:file:bg-cyan-100"}),t&&e.jsxs("div",{className:"mt-2 text-green-400 text-sm",children:["Banner image selected: ",t.name]})]}),e.jsxs("div",{children:[e.jsx(N,{className:"block text-sm font-medium text-white mb-2",children:"Excerpt *"}),e.jsx(re,{value:l.excerpt,onChange:s=>o({...l,excerpt:s.target.value}),placeholder:"Brief description of the blog post (shown in previews)",rows:3,required:!0,className:"bg-slate-700 border-slate-600 text-white"})]}),e.jsxs("div",{children:[e.jsx(N,{className:"block text-sm font-medium text-white mb-2",children:"Featured Image"}),e.jsx("input",{type:"file",accept:"image/*",onChange:s=>y(s.target.files?.[0]||null),className:"block w-full text-sm text-gray-300 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-green-50 file:text-green-700 hover:file:bg-green-100"}),i&&e.jsxs("div",{className:"mt-2 text-green-400 text-sm",children:["Featured image selected: ",i.name]}),e.jsx("p",{className:"text-gray-400 text-sm mt-1",children:"This image shows in blog listings and social sharing"})]}),e.jsxs("div",{children:[e.jsx(N,{className:"block text-sm font-medium text-white mb-2",children:"Content * (WordPress-style Editor)"}),e.jsx("div",{className:"bg-white rounded-lg",children:e.jsx(oe,{apiKey:"no-api-key",value:l.body,onEditorChange:s=>o({...l,body:s}),init:{height:500,menubar:!0,plugins:["anchor","autolink","charmap","codesample","emoticons","image","link","lists","media","searchreplace","table","visualblocks","wordcount","checklist","mediaembed","casechange","formatpainter","pageembed","advtable","advcode","editimage","typography","inlinecss","markdown"],toolbar:"undo redo | blocks fontfamily fontsize | bold italic underline strikethrough | link image media table | typography | align lineheight | checklist numlist bullist indent outdent | emoticons charmap | removeformat",content_style:"body { font-family:Helvetica,Arial,sans-serif; font-size:14px }",images_upload_handler:async(s,a)=>new Promise(async(c,d)=>{try{const n=s.blob(),p=n.name?.split(".").pop()||"png",T=`blog-content/${`${Date.now()}-${Math.random().toString(36).substring(2)}.${p}`}`,{data:ue,error:O}=await m.storage.from("product-images").upload(T,n);if(O)throw O;const{data:J}=m.storage.from("product-images").getPublicUrl(T);c(J.publicUrl)}catch{d("Image upload failed")}}),automatic_uploads:!0,file_picker_types:"image media",media_live_embeds:!0,paste_data_images:!0}})})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(ne,{checked:l.published,onCheckedChange:s=>o({...l,published:s})}),e.jsx(N,{className:"text-white",children:"Publish immediately"})]}),e.jsxs("div",{className:"flex space-x-4",children:[e.jsx(_,{type:"submit",className:"bg-cyan-500 hover:bg-cyan-600",disabled:F.isPending||U.isPending,children:F.isPending||U.isPending?"Saving...":j?"Update Post":"Create Post"}),e.jsx(_,{type:"button",variant:"outline",onClick:()=>{q(),b("list")},className:"border-slate-600 text-gray-300 hover:bg-slate-700",children:"Cancel"})]})]})})]})}),e.jsx(D,{value:"analytics",children:e.jsx(me,{})})]})]})};export{_e 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.