"use strict";(()=>{var e={};e.id=9867,e.ids=[9867],e.modules={72934:e=>{e.exports=require("next/dist/client/components/action-async-storage.external.js")},54580:e=>{e.exports=require("next/dist/client/components/request-async-storage.external.js")},45869:e=>{e.exports=require("next/dist/client/components/static-generation-async-storage.external.js")},20399:e=>{e.exports=require("next/dist/compiled/next-server/app-page.runtime.prod.js")},30517:e=>{e.exports=require("next/dist/compiled/next-server/app-route.runtime.prod.js")},78893:e=>{e.exports=require("buffer")},84770:e=>{e.exports=require("crypto")},76162:e=>{e.exports=require("stream")},21764:e=>{e.exports=require("util")},91554:(e,t,r)=>{r.r(t),r.d(t,{originalPathname:()=>h,patchFetch:()=>v,requestAsyncStorage:()=>p,routeModule:()=>d,serverHooks:()=>x,staticGenerationAsyncStorage:()=>m});var n={};r.r(n),r.d(n,{GET:()=>l});var s=r(49303),a=r(88716),o=r(60670),u=r(87070),i=r(8194),c=r(95456);async function l(e){try{let t=await (0,c.ts)();if(!t)return u.NextResponse.json({error:"Unauthorized"},{status:401});let r=e.nextUrl.searchParams.get("period")||"7d",n=e.nextUrl.searchParams.get("storeId"),s=7;"30d"===r&&(s=30),"90d"===r&&(s=90);let a=new Date;a.setDate(a.getDate()-s);let o={tenantId:t.tenantId,createdAt:{gte:a},paymentStatus:"paid"};n&&(o.storeId=n),t.storeId&&(o.storeId=t.storeId);let l=await i._.order.findMany({where:o,include:{store:{select:{name:!0}},items:{include:{menuItem:{select:{name:!0,categoryId:!0}}}}},orderBy:{createdAt:"asc"}}),d=l.reduce((e,t)=>e+Number(t.total),0),p=l.length,m={};l.forEach(e=>{let t=e.createdAt.toISOString().slice(0,10);m[t]=(m[t]||0)+Number(e.total)});let x=Object.entries(m).map(([e,t])=>({date:e,revenue:t})),h={};l.forEach(e=>{e.items.forEach(e=>{let t=e.menuItemId;h[t]||(h[t]={name:e.menuItem.name,quantity:0,revenue:0}),h[t].quantity+=e.quantity,h[t].revenue+=Number(e.subtotal)})});let v=Object.values(h).sort((e,t)=>t.revenue-e.revenue).slice(0,10),f={};l.forEach(e=>{let t=e.storeId;f[t]||(f[t]={name:e.store.name,revenue:0,orders:0}),f[t].revenue+=Number(e.total),f[t].orders+=1});let g={};return l.forEach(e=>{let t=e.paymentMethod||"other";g[t]=(g[t]||0)+Number(e.total)}),u.NextResponse.json({summary:{totalRevenue:d,totalOrders:p,avgOrderValue:p>0?d/p:0,totalTax:l.reduce((e,t)=>e+Number(t.tax),0)},revenueChart:x,topItems:v,storeRevenue:Object.values(f),paymentMethods:g})}catch(e){return console.error("Reports error:",e),u.NextResponse.json({error:"Server error"},{status:500})}}let d=new s.AppRouteRouteModule({definition:{kind:a.x.APP_ROUTE,page:"/api/reports/route",pathname:"/api/reports",filename:"route",bundlePath:"app/api/reports/route"},resolvedPagePath:"D:\\PROJECT\\resto-saas\\src\\app\\api\\reports\\route.ts",nextConfigOutput:"",userland:n}),{requestAsyncStorage:p,staticGenerationAsyncStorage:m,serverHooks:x}=d,h="/api/reports/route";function v(){return(0,o.patchFetch)({serverHooks:x,staticGenerationAsyncStorage:m})}},95456:(e,t,r)=>{r.d(t,{Gv:()=>d,MH:()=>h,PT:()=>v,RA:()=>p,WX:()=>m,c_:()=>l,ts:()=>x});var n=r(41482),s=r.n(n),a=r(42023),o=r.n(a),u=r(71615),i=r(8194);let c=process.env.JWT_SECRET||"fallback-secret-change-me";async function l(e){return o().hash(e,12)}async function d(e,t){return o().compare(e,t)}function p(e){return s().sign(e,c,{expiresIn:"7d"})}function m(e){try{return s().verify(e,c)}catch{return null}}async function x(){let e=await (0,u.cookies)(),t=e.get("token")?.value;if(!t)return null;let r=m(t);return r?await i._.user.findUnique({where:{id:r.userId},include:{tenant:!0,store:!0}}):null}function h(e,t){return t.includes(e)}function v(e){return!["owner","admin"].includes(e.role)&&!!e.storeId&&!!e.store&&!e.store.isActive}},8194:(e,t,r)=>{r.d(t,{_:()=>s});let n=require("@prisma/client"),s=globalThis.prisma??new n.PrismaClient}};var t=require("../../../webpack-runtime.js");t.C(e);var r=e=>t(t.s=e),n=t.X(0,[8948,5972,4261,2023],()=>r(91554));module.exports=n})();