"use strict";(()=>{var e={};e.id=2819,e.ids=[2819],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")},87419:(e,t,r)=>{r.r(t),r.d(t,{originalPathname:()=>f,patchFetch:()=>h,requestAsyncStorage:()=>p,routeModule:()=>m,serverHooks:()=>w,staticGenerationAsyncStorage:()=>x});var a={};r.r(a),r.d(a,{GET:()=>d});var n=r(49303),s=r(88716),o=r(60670),i=r(87070),u=r(95456),c=r(8194),l=r(45239);async function d(e){try{let t=await (0,u.ts)();if(!t)return i.NextResponse.json({error:"Unauthorized"},{status:401});if(!(0,u.MH)(t.role,["owner","admin","manager"]))return i.NextResponse.json({error:"Forbidden"},{status:403});let r=await (0,l.F)(t.tenantId,"advanced_analytics");if(r)return r;let a=e.nextUrl.searchParams.get("period")||"30d",n=30;"7d"===a&&(n=7),"90d"===a&&(n=90);let s=new Date;s.setDate(s.getDate()-n);let o={tenantId:t.tenantId},d=t.tenantId,m=(await c._.$queryRawUnsafe(`SELECT
        DATE_FORMAT(c.createdAt, '%Y-%m') as month,
        COUNT(*) as newCustomers
      FROM customers c
      WHERE c.tenantId = ?
      GROUP BY DATE_FORMAT(c.createdAt, '%Y-%m')
      ORDER BY month ASC`,d)).map(e=>({month:e.month,newCustomers:Number(e.newCustomers)})),p=(await c._.customer.findMany({where:{...o,isActive:!0},select:{id:!0,name:!0,totalOrders:!0,totalSpent:!0,lastOrderAt:!0},orderBy:{totalSpent:"desc"},take:10})).map(e=>({id:e.id,name:e.name,totalOrders:e.totalOrders,totalSpent:Number(e.totalSpent),lastOrder:e.lastOrderAt?e.lastOrderAt.toISOString().slice(0,10):null})),x=await c._.customer.count({where:o}),w=await c._.customer.count({where:{...o,totalOrders:{gte:2}}}),[f,h,g]=await Promise.all([c._.customer.count({where:{...o,totalOrders:1}}),c._.customer.count({where:{...o,totalOrders:{gte:2,lte:9}}}),c._.customer.count({where:{...o,totalOrders:{gte:10}}})]);return i.NextResponse.json({acquisitionTrend:m,topCustomers:p,repeatRate:{total:x,repeat:w,percentage:x>0?Math.round(w/x*100):0},segmentation:{new:f,returning:h,vip:g}})}catch(e){return console.error("Customer analytics error:",e),i.NextResponse.json({error:"Internal server error"},{status:500})}}let m=new n.AppRouteRouteModule({definition:{kind:s.x.APP_ROUTE,page:"/api/analytics/customers/route",pathname:"/api/analytics/customers",filename:"route",bundlePath:"app/api/analytics/customers/route"},resolvedPagePath:"D:\\PROJECT\\resto-saas\\src\\app\\api\\analytics\\customers\\route.ts",nextConfigOutput:"",userland:a}),{requestAsyncStorage:p,staticGenerationAsyncStorage:x,serverHooks:w}=m,f="/api/analytics/customers/route";function h(){return(0,o.patchFetch)({serverHooks:w,staticGenerationAsyncStorage:x})}},95456:(e,t,r)=>{r.d(t,{Gv:()=>d,MH:()=>w,PT:()=>f,RA:()=>m,WX:()=>p,c_:()=>l,ts:()=>x});var a=r(41482),n=r.n(a),s=r(42023),o=r.n(s),i=r(71615),u=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 m(e){return n().sign(e,c,{expiresIn:"7d"})}function p(e){try{return n().verify(e,c)}catch{return null}}async function x(){let e=await (0,i.cookies)(),t=e.get("token")?.value;if(!t)return null;let r=p(t);return r?await u._.user.findUnique({where:{id:r.userId},include:{tenant:!0,store:!0}}):null}function w(e,t){return t.includes(e)}function f(e){return!["owner","admin"].includes(e.role)&&!!e.storeId&&!!e.store&&!e.store.isActive}},8194:(e,t,r)=>{r.d(t,{_:()=>n});let a=require("@prisma/client"),n=globalThis.prisma??new a.PrismaClient},45239:(e,t,r)=>{r.d(t,{F:()=>s});var a=r(87070),n=r(15230);async function s(e,t){let r=await (0,n.p6)(e);return(0,n.U6)(r.features,t)?null:a.NextResponse.json({error:"Fitur ini tidak tersedia pada paket Anda. Silakan upgrade."},{status:403})}},15230:(e,t,r)=>{r.d(t,{MN:()=>s,U6:()=>o,p6:()=>n});var a=r(8194);async function n(e){let t=await a._.subscription.findFirst({where:{tenantId:e,status:{in:["active","trial"]}},include:{plan:!0},orderBy:{createdAt:"desc"}});return t?{maxStores:t.plan.maxStores,maxStaff:t.plan.maxStaff,maxMenuItems:t.plan.maxMenuItems,maxOrders:t.plan.maxOrders,features:JSON.parse(t.plan.features)}:{maxStores:1,maxStaff:3,maxMenuItems:30,maxOrders:100,features:["pos","qr_menu"]}}async function s(e,t){let r=await n(e),s=0,o=0;switch(t){case"stores":s=await a._.store.count({where:{tenantId:e,isActive:!0}}),o=r.maxStores;break;case"staff":s=await a._.user.count({where:{tenantId:e,isActive:!0}}),o=r.maxStaff;break;case"menuItems":s=await a._.menuItem.count({where:{tenantId:e,isActive:!0}}),o=r.maxMenuItems;break;case"orders":let i=new Date;i.setDate(1),i.setHours(0,0,0,0),s=await a._.order.count({where:{tenantId:e,createdAt:{gte:i}}}),o=r.maxOrders}return 0===o?{allowed:!0,current:s,max:o}:{allowed:s<o,current:s,max:o}}function o(e,t){return e.includes(t)}}};var t=require("../../../../webpack-runtime.js");t.C(e);var r=e=>t(t.s=e),a=t.X(0,[8948,5972,4261,2023],()=>r(87419));module.exports=a})();