import{a as f,j as e}from"./vendor-react-BHG7lGYR.js";import{a as k,l as v,p as I,L as M,C as m,b as p,c as u,v as _,e as h,B as R}from"./index-BWcMVITa.js";import{S as q}from"./select-laDQECdR.js";import{q as E}from"./vendor-IYr-MHu4.js";import{u as B}from"./vendor-router-D-s5vIeO.js";import{ab as T,a3 as b,O as w,D as V,af as C,V as D,aS as $,aT as H}from"./vendor-icons-DaGlTw4_.js";import"./vendor-tanstack-BzWBL1hV.js";import"./vendor-utils-CgOSfOkx.js";import"./dropdown-BYn_8IId.js";import"./input-BGBI2Wze.js";async function U(s=30,o,d){try{const t={days:s.toString()},r=await k.get("/analytics",{params:t});return r.data?.data?r.data.data:await O(s,o,d)}catch(t){if(v.error("Failed to fetch analytics data",{error:t instanceof Error?t.message:String(t),stack:t instanceof Error?t.stack:void 0,days:s}),t instanceof E&&(t.code==="ECONNABORTED"||t.code==="ETIMEDOUT"||!t.response||t.response.status>=500||t.response.status===404))try{return await O(s,o,d)}catch(r){return v.error("Fallback analytics aggregation also failed",{error:r instanceof Error?r.message:String(r),stack:r instanceof Error?r.stack:void 0,days:s}),S(s,o,d)}if(t instanceof E)throw new Error(t.response?.data?.error||t.message||"Failed to fetch analytics data");return S(s,o,d)}}async function O(s,o,d){try{const t=await k.get("/api/v1/tracks",{params:{limit:1e3}}),r=await k.get("/api/v1/playlists",{params:{limit:1e3}}),i=t.data?.data?.tracks||[],x=r.data?.data?.playlists||[],g=i.reduce((a,n)=>a+(n.play_count||0),0),y=i.reduce((a,n)=>a+(n.like_count||0),0),N=i.reduce((a,n)=>a+(n.download_count||0),0),c=[...i].sort((a,n)=>(n.play_count||0)-(a.play_count||0)).slice(0,5).map(a=>({id:a.id,title:a.title,play_count:a.play_count||0,like_count:a.like_count||0})),l=x.reduce((a,n)=>a+(n.play_count||0),0),j=x.reduce((a,n)=>a+(n.like_count||0),0),A=x.reduce((a,n)=>a+(n.share_count||0),0),F=[...x].sort((a,n)=>(n.play_count||0)-(a.play_count||0)).slice(0,5).map(a=>({id:a.id,name:a.name,play_count:a.play_count||0,like_count:a.like_count||0})),L=d?new Date(d):new Date,P=o?new Date(o):(()=>{const a=new Date;return a.setDate(a.getDate()-s),a})();return{tracks:{total_tracks:i.length,total_plays:g,total_likes:y,total_downloads:N,average_play_count:i.length>0?g/i.length:0,top_tracks:c},playlists:{total_playlists:x.length,total_plays:l,total_likes:j,total_shares:A,average_play_count:x.length>0?l/x.length:0,top_playlists:F},period:{start_date:P.toISOString(),end_date:L.toISOString(),days:s}}}catch(t){return v.error("Failed to aggregate analytics",{error:t instanceof Error?t.message:String(t),stack:t instanceof Error?t.stack:void 0,days:s}),S(s)}}function S(s,o,d){const t=d?new Date(d):new Date,r=o?new Date(o):(()=>{const i=new Date;return i.setDate(i.getDate()-s),i})();return{tracks:{total_tracks:0,total_plays:0,total_likes:0,total_downloads:0,average_play_count:0,top_tracks:[]},playlists:{total_playlists:0,total_plays:0,total_likes:0,total_shares:0,average_play_count:0,top_playlists:[]},period:{start_date:r.toISOString(),end_date:t.toISOString(),days:s}}}function se(){const[s,o]=f.useState(null),[d,t]=f.useState(!0),[r,i]=f.useState(null),[x,g]=f.useState(30),y=B();f.useEffect(()=>{N()},[x]);const N=async()=>{t(!0),i(null);try{const l=await U(x);o(l)}catch(l){const j=I(l);v.error("Failed to load analytics",{error:j.message,period:x}),i(j.message)}finally{t(!1)}},c=l=>l>=1e6?`${(l/1e6).toFixed(1)}M`:l>=1e3?`${(l/1e3).toFixed(1)}K`:l.toString();return d?e.jsx("div",{className:"container mx-auto px-4 py-8",children:e.jsx("div",{className:"flex items-center justify-center h-[400px]",children:e.jsx(M,{})})}):r?e.jsx("div",{className:"container mx-auto px-4 py-8",children:e.jsxs(m,{children:[e.jsxs(p,{children:[e.jsx(u,{children:"Erreur"}),e.jsx(_,{children:r})]}),e.jsx(h,{children:e.jsx(R,{onClick:N,children:"Réessayer"})})]})}):s?e.jsxs("div",{className:"container mx-auto px-4 py-8",children:[e.jsxs("div",{className:"mb-6",children:[e.jsx("h1",{className:"text-3xl font-bold mb-2",children:"Analytics"}),e.jsx("p",{className:"text-muted-foreground",children:"Statistiques et métriques de performance pour vos pistes et playlists"})]}),e.jsxs("div",{className:"mb-6 flex items-center gap-4",children:[e.jsx("label",{htmlFor:"period",className:"text-sm font-medium",children:"Période:"}),e.jsx(q,{options:[{value:"7",label:"7 derniers jours"},{value:"30",label:"30 derniers jours"},{value:"90",label:"90 derniers jours"},{value:"365",label:"1 an"}],value:x.toString(),onChange:l=>g(parseInt(l,10)),placeholder:"Sélectionner une période",className:"w-[180px]"})]}),e.jsxs("div",{className:"mb-8",children:[e.jsxs("h2",{className:"text-2xl font-semibold mb-4 flex items-center gap-2",children:[e.jsx(T,{className:"h-6 w-6"}),"Statistiques des pistes"]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 mb-6",children:[e.jsxs(m,{children:[e.jsxs(p,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(u,{className:"text-sm font-medium",children:"Total des pistes"}),e.jsx(T,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(h,{children:e.jsx("div",{className:"text-2xl font-bold",children:c(s.tracks.total_tracks)})})]}),e.jsxs(m,{children:[e.jsxs(p,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(u,{className:"text-sm font-medium",children:"Total des lectures"}),e.jsx(b,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(h,{children:e.jsx("div",{className:"text-2xl font-bold",children:c(s.tracks.total_plays)})})]}),e.jsxs(m,{children:[e.jsxs(p,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(u,{className:"text-sm font-medium",children:"Total des likes"}),e.jsx(w,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(h,{children:e.jsx("div",{className:"text-2xl font-bold",children:c(s.tracks.total_likes)})})]}),e.jsxs(m,{children:[e.jsxs(p,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(u,{className:"text-sm font-medium",children:"Total des téléchargements"}),e.jsx(V,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(h,{children:e.jsx("div",{className:"text-2xl font-bold",children:c(s.tracks.total_downloads)})})]})]}),s.tracks.top_tracks.length>0&&e.jsxs(m,{children:[e.jsxs(p,{children:[e.jsxs(u,{className:"flex items-center gap-2",children:[e.jsx(C,{className:"h-5 w-5"}),"Top 5 pistes"]}),e.jsx(_,{children:"Vos pistes les plus écoutées"})]}),e.jsx(h,{children:e.jsx("div",{className:"space-y-4",children:s.tracks.top_tracks.map((l,j)=>e.jsx("div",{className:"flex items-center justify-between p-4 border rounded-lg hover:bg-accent cursor-pointer",onClick:()=>y(`/tracks/${l.id}`),children:e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx("div",{className:"flex items-center justify-center w-8 h-8 rounded-full bg-primary/10 text-primary font-semibold",children:j+1}),e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:l.title}),e.jsxs("div",{className:"flex items-center gap-4 text-sm text-muted-foreground",children:[e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(b,{className:"h-3 w-3"}),c(l.play_count)]}),e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(w,{className:"h-3 w-3"}),c(l.like_count)]})]})]})]})},l.id))})})]})]}),e.jsxs("div",{className:"mb-8",children:[e.jsxs("h2",{className:"text-2xl font-semibold mb-4 flex items-center gap-2",children:[e.jsx(D,{className:"h-6 w-6"}),"Statistiques des playlists"]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 mb-6",children:[e.jsxs(m,{children:[e.jsxs(p,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(u,{className:"text-sm font-medium",children:"Total des playlists"}),e.jsx(D,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(h,{children:e.jsx("div",{className:"text-2xl font-bold",children:c(s.playlists.total_playlists)})})]}),e.jsxs(m,{children:[e.jsxs(p,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(u,{className:"text-sm font-medium",children:"Total des lectures"}),e.jsx(b,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(h,{children:e.jsx("div",{className:"text-2xl font-bold",children:c(s.playlists.total_plays)})})]}),e.jsxs(m,{children:[e.jsxs(p,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(u,{className:"text-sm font-medium",children:"Total des likes"}),e.jsx(w,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(h,{children:e.jsx("div",{className:"text-2xl font-bold",children:c(s.playlists.total_likes)})})]}),e.jsxs(m,{children:[e.jsxs(p,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(u,{className:"text-sm font-medium",children:"Total des partages"}),e.jsx($,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(h,{children:e.jsx("div",{className:"text-2xl font-bold",children:c(s.playlists.total_shares)})})]})]}),s.playlists.top_playlists.length>0&&e.jsxs(m,{children:[e.jsxs(p,{children:[e.jsxs(u,{className:"flex items-center gap-2",children:[e.jsx(C,{className:"h-5 w-5"}),"Top 5 playlists"]}),e.jsx(_,{children:"Vos playlists les plus écoutées"})]}),e.jsx(h,{children:e.jsx("div",{className:"space-y-4",children:s.playlists.top_playlists.map((l,j)=>e.jsx("div",{className:"flex items-center justify-between p-4 border rounded-lg hover:bg-accent cursor-pointer",onClick:()=>y(`/playlists/${l.id}`),children:e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx("div",{className:"flex items-center justify-center w-8 h-8 rounded-full bg-primary/10 text-primary font-semibold",children:j+1}),e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:l.name}),e.jsxs("div",{className:"flex items-center gap-4 text-sm text-muted-foreground",children:[e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(b,{className:"h-3 w-3"}),c(l.play_count)]}),e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(w,{className:"h-3 w-3"}),c(l.like_count)]})]})]})]})},l.id))})})]})]}),e.jsxs(m,{children:[e.jsxs(p,{children:[e.jsxs(u,{className:"flex items-center gap-2",children:[e.jsx(H,{className:"h-5 w-5"}),"Résumé"]}),e.jsx(_,{children:"Vue d'ensemble de vos performances"})]}),e.jsx(h,{children:e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-1",children:"Lectures moyennes par piste"}),e.jsx("p",{className:"text-2xl font-bold",children:Math.round(s.tracks.average_play_count)})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-1",children:"Lectures moyennes par playlist"}),e.jsx("p",{className:"text-2xl font-bold",children:Math.round(s.playlists.average_play_count)})]})]})})]})]}):null}export{se as AnalyticsPage};