"use strict";(()=>{var e={};e.id=979,e.ids=[979],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")},5900:e=>{e.exports=require("pg")},7147:e=>{e.exports=require("fs")},1017:e=>{e.exports=require("path")},8143:(e,t,o)=>{o.r(t),o.d(t,{headerHooks:()=>v,originalPathname:()=>h,patchFetch:()=>y,requestAsyncStorage:()=>p,routeModule:()=>u,serverHooks:()=>g,staticGenerationAsyncStorage:()=>_,staticGenerationBailout:()=>m});var r={};o.r(r),o.d(r,{GET:()=>d,PUT:()=>l});var s=o(5419),a=o(9108),n=o(9678),i=o(8070),c=o(2509);async function d(e){try{let t=await (0,c.e)(),{data:{user:r},error:s}=await t.auth.getUser();if(s||!r)return i.Z.json({error:{code:"UNAUTHORIZED",message:"Authentication required"}},{status:401});let a=new URL(e.url).searchParams.get("site_id");if(!a)return i.Z.json({error:{code:"MISSING_REQUIRED_FIELD",message:"site_id is required"}},{status:400});let{createAdminClient:n}=await Promise.resolve().then(o.bind(o,2509)),d=n(),{data:l,error:u}=await d.from("sites").select("tenant_id").eq("id",a).single();if(u||!l)return i.Z.json({error:{code:"SITE_NOT_FOUND",message:"Site not found"}},{status:404});let{data:p,error:_}=await d.from("settings").select("value").eq("site_id",a).eq("key","chat").eq("is_active",!0).order("version",{ascending:!1}).limit(1).single();if(_||!p)return i.Z.json({title:"AI Assistant",welcome_message:"Hello! I am your AI assistant. How can I help you today?",input_placeholder:"Type your message...",send_button_text:"Send",avatar_url:null,primary_color:"#667eea",secondary_color:"#764ba2",use_gradient:!0,bubble_position:"bottom-right",delay_seconds:0});let g=p.value;return i.Z.json({title:g.title||"AI Assistant",welcome_message:g.welcome_message||"Hello! I am your AI assistant. How can I help you today?",input_placeholder:g.input_placeholder||"Type your message...",send_button_text:g.send_button_text||"Send",avatar_url:g.avatar_url||null,primary_color:g.primary_color||"#667eea",secondary_color:g.secondary_color||"#764ba2",use_gradient:!1!==g.use_gradient,bubble_position:g.bubble_position||"bottom-right",delay_seconds:g.delay_seconds??0})}catch(e){return console.error("Chat settings GET error:",e),i.Z.json({error:{code:"INTERNAL_ERROR",message:e instanceof Error?e.message:"Failed to fetch chat settings"}},{status:500})}}async function l(e){try{let t=await (0,c.e)(),{data:{user:r},error:s}=await t.auth.getUser();if(s||!r)return i.Z.json({error:{code:"UNAUTHORIZED",message:"Authentication required"}},{status:401});let{site_id:a,title:n,welcome_message:d,input_placeholder:l,send_button_text:u,avatar_url:p,primary_color:_,secondary_color:g,use_gradient:v,bubble_position:m,delay_seconds:h}=await e.json();if(!a)return i.Z.json({error:{code:"MISSING_REQUIRED_FIELD",message:"site_id is required"}},{status:400});let{createAdminClient:y}=await Promise.resolve().then(o.bind(o,2509)),f=y(),{data:E,error:w}=await f.from("sites").select("tenant_id").eq("id",a).single();if(w||!E)return i.Z.json({error:{code:"SITE_NOT_FOUND",message:"Site not found"}},{status:404});let{data:I}=await f.from("settings").select("value, version").eq("site_id",a).eq("key","chat").eq("is_active",!0).order("version",{ascending:!1}),b=I&&I.length>0?I[0]:null,A=b?.version||0,R=b?.value||{},j={...R,title:void 0!==n?n:R.title,welcome_message:void 0!==d?d:R.welcome_message,input_placeholder:void 0!==l?l:R.input_placeholder,send_button_text:void 0!==u?u:R.send_button_text,avatar_url:void 0!==p?p:R.avatar_url,primary_color:void 0!==_?_:R.primary_color||"#667eea",secondary_color:void 0!==g?g:R.secondary_color||"#764ba2",use_gradient:void 0!==v?v:!1!==R.use_gradient,bubble_position:void 0!==m?m:R.bubble_position||"bottom-right",delay_seconds:void 0!==h?h:R.delay_seconds??0};if(I&&I.length>0){let{query:e}=await o.e(321).then(o.bind(o,321));try{let t=await e(`UPDATE settings 
           SET is_active = false, updated_at = NOW()
           WHERE site_id = $1 AND key = $2 AND is_active = true`,[a,"chat"]);console.log("Deactivated old settings:",t.rowCount,"rows")}catch(e){throw console.error("Failed to deactivate old settings (SQL):",e),Error(`Failed to deactivate old settings: ${e instanceof Error?e.message:"Unknown error"}`)}}let{error:x}=await f.from("settings").insert({site_id:a,tenant_id:E.tenant_id,key:"chat",value:j,version:A+1,is_active:!0});if(x)throw Error(`Failed to update chat settings: ${x.message}`);return i.Z.json({success:!0})}catch(e){return console.error("Chat settings PUT error:",e),i.Z.json({error:{code:"INTERNAL_ERROR",message:e instanceof Error?e.message:"Failed to update chat settings"}},{status:500})}}let u=new s.AppRouteRouteModule({definition:{kind:a.x.APP_ROUTE,page:"/api/chat/settings/route",pathname:"/api/chat/settings",filename:"route",bundlePath:"app/api/chat/settings/route"},resolvedPagePath:"C:\\xampp\\htdocs\\AI Woo Chat\\src\\app\\api\\chat\\settings\\route.ts",nextConfigOutput:"standalone",userland:r}),{requestAsyncStorage:p,staticGenerationAsyncStorage:_,serverHooks:g,headerHooks:v,staticGenerationBailout:m}=u,h="/api/chat/settings/route";function y(){return(0,n.patchFetch)({serverHooks:g,staticGenerationAsyncStorage:_})}},2509:(e,t,o)=>{o.d(t,{createAdminClient:()=>i,e:()=>n});var r=o(6874),s=o(7439),a=o(3950);async function n(){let e=await (0,s.cookies)();return(0,r.lx)("https://drmuwsxyvvfivdfsyydy.supabase.co","eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImRybXV3c3h5dnZmaXZkZnN5eWR5Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3Njg0NjAwMDgsImV4cCI6MjA4NDAzNjAwOH0.kMsFG-VLNqwzIgl3cFW-owiRCWHcB47M6V6Ti4O4alY",{cookies:{get:t=>e.get(t)?.value,set(t,o,r){try{e.set({name:t,value:o,...r})}catch(e){}},remove(t,o){try{e.set({name:t,value:"",...o})}catch(e){}}}})}function i(){return(0,a.eI)("https://drmuwsxyvvfivdfsyydy.supabase.co",process.env.SUPABASE_SERVICE_ROLE_KEY,{auth:{autoRefreshToken:!1,persistSession:!1}})}(0,o(4113).C)()},4113:(e,t,o)=>{o.d(t,{C:()=>n});var r=o(7147),s=o(1017);let a=!1;function n(){if(!a)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")],o=null,n=null;for(let e of t)try{n=(0,r.readFileSync)(e,"utf-8"),o=e,console.log("[EnvLoader] Found .env.production at:",e);break}catch{continue}if(!n||!o)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 o=t.match(/^([^=]+)=(.*)$/);if(o){let e=o[1].trim(),t=o[2].trim();(t.startsWith('"')&&t.endsWith('"')||t.startsWith("'")&&t.endsWith("'"))&&(t=t.slice(1,-1)),process.env[e]||(process.env[e]=t)}}a=!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 o=e=>t(t.s=e),r=t.X(0,[1638,9989,6206],()=>o(8143));module.exports=r})();