"use strict";(()=>{var e={};e.id=4003,e.ids=[4003],e.modules={2934:e=>{e.exports=require("next/dist/client/components/action-async-storage.external.js")},4580:e=>{e.exports=require("next/dist/client/components/request-async-storage.external.js")},5869:e=>{e.exports=require("next/dist/client/components/static-generation-async-storage.external.js")},517:e=>{e.exports=require("next/dist/compiled/next-server/app-route.runtime.prod.js")},7147:e=>{e.exports=require("fs")},1017:e=>{e.exports=require("path")},6426:(e,t,r)=>{r.r(t),r.d(t,{headerHooks:()=>_,originalPathname:()=>f,patchFetch:()=>h,requestAsyncStorage:()=>u,routeModule:()=>l,serverHooks:()=>p,staticGenerationAsyncStorage:()=>v,staticGenerationBailout:()=>m});var o={};r.r(o),r.d(o,{GET:()=>d});var s=r(5419),i=r(9108),n=r(9678),a=r(8070),c=r(2509);async function d(e){try{let t=await (0,c.e)(),{data:{user:r},error:o}=await t.auth.getUser();if(o||!r)return a.Z.json({error:{code:"UNAUTHORIZED",message:"Authentication required"}},{status:401});let s=new URL(e.url),i=s.searchParams.get("site_id"),n=parseInt(s.searchParams.get("limit")||"50",10),d=parseInt(s.searchParams.get("offset")||"0",10);if(!i)return a.Z.json({error:{code:"MISSING_REQUIRED_FIELD",message:"site_id is required"}},{status:400});let l=(0,c.createAdminClient)(),{data:u,error:v}=await l.from("sites").select("tenant_id").eq("id",i).single();if(v||!u)return a.Z.json({error:{code:"SITE_NOT_FOUND",message:"Site not found"}},{status:404});let{count:p,error:_}=await l.from("conversations").select("*",{count:"exact",head:!0}).eq("site_id",i);if(_)throw console.error("Conversations: Count error:",_),_;console.log("Conversations: Total count for site",i,":",p);let{data:m,error:f}=await l.from("conversations").select(`
        id,
        conversation_id,
        site_id,
        visitor_id,
        last_message_at,
        message_count,
        created_at
      `).eq("site_id",i).order("last_message_at",{ascending:!1,nullsFirst:!1}).range(d,d+n-1);if(f)throw console.error("Conversations: Query error:",f),f;let h=m?.filter(e=>e.visitor_id).map(e=>e.visitor_id)||[],g={};if(h.length>0){let{data:e,error:t}=await l.from("visitors").select("id, visitor_id, first_seen_at, last_seen_at").in("id",h);!t&&e&&e.forEach(e=>{g[e.id]=e})}console.log("Conversations: Query result",{siteId:i,limit:n,offset:d,totalCount:p,found:m?.length||0,visitorIdsCount:h.length,visitorsMapSize:Object.keys(g).length,sample:m?.[0]||null});let E=(m||[]).map(e=>{let t=e.visitor_id&&g[e.visitor_id]?{visitor_id:g[e.visitor_id].visitor_id,first_seen_at:g[e.visitor_id].first_seen_at,last_seen_at:g[e.visitor_id].last_seen_at}:null;return{id:e.id,conversation_id:e.conversation_id,site_id:e.site_id,visitor_id:e.visitor_id,last_message_at:e.last_message_at,message_count:e.message_count||0,created_at:e.created_at,visitor:t}});return a.Z.json({conversations:E,pagination:{limit:n,offset:d,total:E.length}})}catch(e){return console.error("Conversations GET error:",e),a.Z.json({error:{code:"INTERNAL_ERROR",message:e instanceof Error?e.message:"Failed to fetch conversations"}},{status:500})}}let l=new s.AppRouteRouteModule({definition:{kind:i.x.APP_ROUTE,page:"/api/conversations/route",pathname:"/api/conversations",filename:"route",bundlePath:"app/api/conversations/route"},resolvedPagePath:"C:\\xampp\\htdocs\\AI Woo Chat\\src\\app\\api\\conversations\\route.ts",nextConfigOutput:"standalone",userland:o}),{requestAsyncStorage:u,staticGenerationAsyncStorage:v,serverHooks:p,headerHooks:_,staticGenerationBailout:m}=l,f="/api/conversations/route";function h(){return(0,n.patchFetch)({serverHooks:p,staticGenerationAsyncStorage:v})}},2509:(e,t,r)=>{r.d(t,{createAdminClient:()=>a,e:()=>n});var o=r(6874),s=r(7439),i=r(3950);async function n(){let e=await (0,s.cookies)();return(0,o.lx)("https://drmuwsxyvvfivdfsyydy.supabase.co","eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImRybXV3c3h5dnZmaXZkZnN5eWR5Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3Njg0NjAwMDgsImV4cCI6MjA4NDAzNjAwOH0.kMsFG-VLNqwzIgl3cFW-owiRCWHcB47M6V6Ti4O4alY",{cookies:{get:t=>e.get(t)?.value,set(t,r,o){try{e.set({name:t,value:r,...o})}catch(e){}},remove(t,r){try{e.set({name:t,value:"",...r})}catch(e){}}}})}function a(){return(0,i.eI)("https://drmuwsxyvvfivdfsyydy.supabase.co",process.env.SUPABASE_SERVICE_ROLE_KEY,{auth:{autoRefreshToken:!1,persistSession:!1}})}(0,r(4113).C)()},4113:(e,t,r)=>{r.d(t,{C:()=>n});var o=r(7147),s=r(1017);let i=!1;function n(){if(!i)try{let e=process.cwd(),t=[(0,s.join)(e,".env.production"),(0,s.join)(e,"..",".env.production"),(0,s.join)(e,"..","..",".env.production"),(0,s.join)(e,"app",".env.production")],r=null,n=null;for(let e of t)try{n=(0,o.readFileSync)(e,"utf-8"),r=e,console.log("[EnvLoader] Found .env.production at:",e);break}catch{continue}if(!n||!r)throw Error(`Could not find .env.production. Tried: ${t.join(", ")}`);for(let e of n.split("\n")){let t=e.trim();if(!t||t.startsWith("#"))continue;let r=t.match(/^([^=]+)=(.*)$/);if(r){let e=r[1].trim(),t=r[2].trim();(t.startsWith('"')&&t.endsWith('"')||t.startsWith("'")&&t.endsWith("'"))&&(t=t.slice(1,-1)),process.env[e]||(process.env[e]=t)}}i=!0,console.log("[EnvLoader] Loaded environment variables from .env.production"),console.log("[EnvLoader] SUPABASE_DB_URL:",process.env.SUPABASE_DB_URL?"SET":"NOT SET")}catch(e){"ENOENT"===e.code?console.warn("[EnvLoader] .env.production file not found at:",envPath):console.warn("[EnvLoader] Could not load .env.production:",e.message)}}}};var t=require("../../../webpack-runtime.js");t.C(e);var r=e=>t(t.s=e),o=t.X(0,[1638,9989,6206],()=>r(6426));module.exports=o})();