(globalThis.TURBOPACK||(globalThis.TURBOPACK=[])).push(["object"==typeof document?document.currentScript:void 0,11241,e=>{"use strict";let a=(0,e.i(56420).default)("arrow-left",[["path",{d:"m12 19-7-7 7-7",key:"1l729n"}],["path",{d:"M19 12H5",key:"x3x0zl"}]]);e.s(["ArrowLeft",0,a],11241)},83353,e=>{"use strict";var a=e.i(18566),t=e.i(2169);e.s(["useAppRouter",0,function(){let e=(0,a.useRouter)(),{startLoading:s}=(0,t.useLoader)();return{...e,push:(a,t)=>{console.log("[Loader] Navigation started:",a),s&&s(),e.push(a,t)},replace:(a,t)=>{console.log("[Loader] Navigation started:",a),s&&s(),e.replace(a,t)}}}])},73474,e=>{"use strict";let a=(0,e.i(56420).default)("trash-2",[["path",{d:"M10 11v6",key:"nco0om"}],["path",{d:"M14 11v6",key:"outv1u"}],["path",{d:"M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6",key:"miytrc"}],["path",{d:"M3 6h18",key:"d0wm0j"}],["path",{d:"M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2",key:"e791ji"}]]);e.s(["Trash2",0,a],73474)},12498,e=>{"use strict";var a=e.i(65464);let t=new class{async fetchCoupons(e){let{data:t,error:s}=await a.supabase.from("coupons").select("*").eq("vendor_id",e).order("created_at",{ascending:!1});if(s)throw console.error("Error fetching coupons:",s),s;return t||[]}async fetchCouponDetails(e){let{data:t,error:s}=await a.supabase.from("coupons").select("*").eq("id",e).single();if(s)throw s;let o=[],i=[];if("category"===t.apply_type){let{data:t}=await a.supabase.from("coupon_categories").select(`
          category_id,
          categories (
            name
          )
        `).eq("coupon_id",e);o=t||[]}else if("product"===t.apply_type){let{data:t}=await a.supabase.from("coupon_products").select(`
          product_id,
          products (
            product_name
          )
        `).eq("coupon_id",e);i=t||[]}return{...t,selectedCategories:o,selectedProducts:i}}async createCoupon(e,t){let{data:s,error:o}=await a.supabase.from("coupons").insert([e]).select().single();if(o){if("23505"===o.code)throw Error("This coupon code has already been created. Please try a different code or generate a new one.");throw o}let i=s.id;if("category"===e.apply_type&&t.categories?.length>0){let e=t.categories.map(e=>({coupon_id:i,category_id:e}));await a.supabase.from("coupon_categories").insert(e)}else if("product"===e.apply_type&&t.products?.length>0){let e=t.products.map(e=>({coupon_id:i,product_id:e}));await a.supabase.from("coupon_products").insert(e)}return s}async updateCouponStatus(e,t){let{data:s,error:o}=await a.supabase.from("coupons").update({is_active:t}).eq("id",e).select().single();if(o)throw o;return s}async deleteCoupon(e){let{error:t}=await a.supabase.from("coupons").delete().eq("id",e);if(t)throw t;return!0}async fetchVendorProducts(e){let{data:t,error:s}=await a.supabase.from("products").select("id, product_name, category_id").eq("vendor_id",e).order("product_name");return s?(console.warn("Failed to fetch products:",s.message),[]):t||[]}async fetchCategories(){let{data:e,error:t}=await a.supabase.from("categories").select("id, name").order("name");return t?(console.warn("Failed to fetch categories:",t.message),[]):e||[]}async validateCoupon(e,t,s){let{subtotal:o,products:i}=t,{data:l,error:r}=await a.supabase.from("coupons").select("*").eq("code",e).single();if(r||!l)return{isValid:!1,error:"Invalid coupon code"};if(!l.is_active)return{isValid:!1,error:"This coupon is no longer active"};let d=new Date;if(new Date(l.start_date)>d)return{isValid:!1,error:"This coupon is not yet valid"};if(new Date(l.end_date)<d)return{isValid:!1,error:"This coupon has expired"};if(l.usage_limit>0&&l.used_count>=l.usage_limit)return{isValid:!1,error:"Coupon usage limit has been reached"};if(s){let{count:e}=await a.supabase.from("coupon_usages").select("*",{count:"exact",head:!0}).eq("coupon_id",l.id).eq("user_id",s);if(e>=l.per_user_limit)return{isValid:!1,error:"You have reached your usage limit for this coupon"}}if(l.min_order_amount&&o<l.min_order_amount)return{isValid:!1,error:`Minimum order amount is ₹${l.min_order_amount}`};let c=!1,n=0;if("all"===l.apply_type)n=o,c=!0;else if("category"===l.apply_type){let{data:e}=await a.supabase.from("coupon_categories").select("category_id").eq("coupon_id",l.id),t=e.map(e=>e.category_id);for(let e of i)t.includes(e.category_id)&&(n+=e.price*e.quantity,c=!0)}else if("product"===l.apply_type){let{data:e}=await a.supabase.from("coupon_products").select("product_id").eq("coupon_id",l.id),t=e.map(e=>e.product_id);for(let e of i)t.includes(e.id)&&(n+=e.price*e.quantity,c=!0)}if(!c)return{isValid:!1,error:"Your cart does not contain any items eligible for this coupon."};let u=0;return"percentage"===l.type?(u=n*(l.value/100),l.max_discount&&u>l.max_discount&&(u=l.max_discount)):"fixed"===l.type&&(u=l.value),u=Math.min(u,n),{isValid:!0,couponId:l.id,discountAmount:u}}};e.s(["couponsRepository",0,t])},86682,e=>{e.v({actionLink:"coupons-module__6TITaa__actionLink",actionLinks:"coupons-module__6TITaa__actionLinks",actionsBar:"coupons-module__6TITaa__actionsBar",analyticsCard:"coupons-module__6TITaa__analyticsCard",analyticsGrid:"coupons-module__6TITaa__analyticsGrid",cardTitle:"coupons-module__6TITaa__cardTitle",cardValue:"coupons-module__6TITaa__cardValue",container:"coupons-module__6TITaa__container",deleteLink:"coupons-module__6TITaa__deleteLink",errorText:"coupons-module__6TITaa__errorText",formControl:"coupons-module__6TITaa__formControl",formGrid:"coupons-module__6TITaa__formGrid",formGroup:"coupons-module__6TITaa__formGroup",formLabel:"coupons-module__6TITaa__formLabel",formSection:"coupons-module__6TITaa__formSection",header:"coupons-module__6TITaa__header",inputAddon:"coupons-module__6TITaa__inputAddon",inputWithAddon:"coupons-module__6TITaa__inputWithAddon",pageButton:"coupons-module__6TITaa__pageButton",pageControls:"coupons-module__6TITaa__pageControls",pagination:"coupons-module__6TITaa__pagination",primaryButton:"coupons-module__6TITaa__primaryButton",searchInput:"coupons-module__6TITaa__searchInput",secondaryButton:"coupons-module__6TITaa__secondaryButton",sectionTitle:"coupons-module__6TITaa__sectionTitle",selectInput:"coupons-module__6TITaa__selectInput",statusActive:"coupons-module__6TITaa__statusActive",statusBadge:"coupons-module__6TITaa__statusBadge",statusExpired:"coupons-module__6TITaa__statusExpired",statusInactive:"coupons-module__6TITaa__statusInactive",table:"coupons-module__6TITaa__table",tableContainer:"coupons-module__6TITaa__tableContainer",title:"coupons-module__6TITaa__title"})},44944,e=>{"use strict";let a=(0,e.i(56420).default)("power",[["path",{d:"M12 2v10",key:"mnfbl"}],["path",{d:"M18.4 6.6a9 9 0 1 1-12.77.04",key:"obofu9"}]]);e.s(["Power",0,a],44944)},8936,e=>{"use strict";var a=e.i(43476),t=e.i(71645),s=e.i(11241),o=e.i(73474),i=e.i(44944),l=e.i(83353),r=e.i(12498),d=e.i(86682);e.s(["default",0,function({id:e}){let c=(0,l.useAppRouter)(),[n,u]=(0,t.useState)(null),[p,_]=(0,t.useState)(!0),[m,f]=(0,t.useState)(null),[h,y]=(0,t.useState)(!1);(0,t.useEffect)(()=>{y(!0),T()},[e]);let T=async()=>{try{_(!0);let a=await r.couponsRepository.fetchCouponDetails(e);u(a)}catch(e){console.error(e),f("Failed to load coupon details.")}finally{_(!1)}},x=async()=>{try{await r.couponsRepository.updateCouponStatus(e,!n.is_active),u({...n,is_active:!n.is_active})}catch(e){alert("Failed to update status")}},g=async()=>{if(window.confirm("Are you sure you want to delete this coupon? This action cannot be undone."))try{await r.couponsRepository.deleteCoupon(e),c.push("/coupons")}catch(e){alert("Failed to delete coupon")}};if(!h)return null;if(p)return(0,a.jsx)("div",{className:d.default.container,children:"Loading details..."});if(m)return(0,a.jsx)("div",{className:d.default.container,children:m});if(!n)return(0,a.jsx)("div",{className:d.default.container,children:"Coupon not found."});let v=new Date(n.end_date)<new Date,j=d.default.statusInactive,N="Inactive";return n.is_active&&!v?(j=d.default.statusActive,N="Active"):v&&(j=d.default.statusExpired,N="Expired"),(0,a.jsxs)("div",{className:d.default.container,children:[(0,a.jsxs)("div",{className:d.default.header,children:[(0,a.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:16},children:[(0,a.jsx)("button",{className:d.default.secondaryButton,style:{padding:"8px"},onClick:()=>c.push("/coupons"),children:(0,a.jsx)(s.ArrowLeft,{size:18})}),(0,a.jsxs)("div",{children:[(0,a.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:12},children:[(0,a.jsx)("h1",{className:d.default.title,children:n.code}),(0,a.jsx)("span",{className:`${d.default.statusBadge} ${j}`,children:N})]}),(0,a.jsxs)("p",{style:{color:"var(--color-text-secondary)",marginTop:"4px"},children:["Created on ",new Date(n.created_at).toLocaleDateString()]})]})]}),(0,a.jsxs)("div",{style:{display:"flex",gap:12},children:[(0,a.jsxs)("button",{className:d.default.secondaryButton,onClick:x,children:[(0,a.jsx)(i.Power,{size:18,style:{marginRight:8,display:"inline"}}),n.is_active?"Deactivate":"Activate"]}),(0,a.jsxs)("button",{className:d.default.secondaryButton,onClick:g,style:{color:"var(--color-danger)"},children:[(0,a.jsx)(o.Trash2,{size:18,style:{marginRight:8,display:"inline"}}),"Delete"]})]})]}),(0,a.jsxs)("div",{className:d.default.formGrid,children:[(0,a.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:24},children:[(0,a.jsxs)("div",{className:d.default.formSection,children:[(0,a.jsx)("h2",{className:d.default.sectionTitle,children:"Coupon Details"}),(0,a.jsxs)("div",{style:{display:"grid",gridTemplateColumns:"1fr 1fr",gap:16},children:[(0,a.jsxs)("div",{children:[(0,a.jsx)("p",{className:d.default.cardTitle,children:"Type"}),(0,a.jsx)("p",{className:d.default.formLabel,style:{textTransform:"capitalize"},children:n.type})]}),(0,a.jsxs)("div",{children:[(0,a.jsx)("p",{className:d.default.cardTitle,children:"Discount Value"}),(0,a.jsx)("p",{className:d.default.formLabel,children:"percentage"===n.type?`${n.value}%`:`₹${n.value}`})]}),(0,a.jsxs)("div",{children:[(0,a.jsx)("p",{className:d.default.cardTitle,children:"Min Order Amount"}),(0,a.jsx)("p",{className:d.default.formLabel,children:n.min_order_amount?`₹${n.min_order_amount}`:"None"})]}),(0,a.jsxs)("div",{children:[(0,a.jsx)("p",{className:d.default.cardTitle,children:"Max Discount"}),(0,a.jsx)("p",{className:d.default.formLabel,children:n.max_discount?`₹${n.max_discount}`:"None"})]})]})]}),(0,a.jsxs)("div",{className:d.default.formSection,children:[(0,a.jsx)("h2",{className:d.default.sectionTitle,children:"Schedule & Usage"}),(0,a.jsxs)("div",{style:{display:"grid",gridTemplateColumns:"1fr 1fr",gap:16},children:[(0,a.jsxs)("div",{children:[(0,a.jsx)("p",{className:d.default.cardTitle,children:"Start Date"}),(0,a.jsx)("p",{className:d.default.formLabel,children:new Date(n.start_date).toLocaleString()})]}),(0,a.jsxs)("div",{children:[(0,a.jsx)("p",{className:d.default.cardTitle,children:"End Date"}),(0,a.jsx)("p",{className:d.default.formLabel,children:new Date(n.end_date).toLocaleString()})]}),(0,a.jsxs)("div",{children:[(0,a.jsx)("p",{className:d.default.cardTitle,children:"Usage Limit"}),(0,a.jsx)("p",{className:d.default.formLabel,children:0===n.usage_limit?"Unlimited":n.usage_limit})]}),(0,a.jsxs)("div",{children:[(0,a.jsx)("p",{className:d.default.cardTitle,children:"Per User Limit"}),(0,a.jsx)("p",{className:d.default.formLabel,children:n.per_user_limit})]})]})]})]}),(0,a.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:24},children:[(0,a.jsxs)("div",{className:d.default.formSection,children:[(0,a.jsx)("h2",{className:d.default.sectionTitle,children:"Scope"}),(0,a.jsxs)("div",{style:{marginBottom:16},children:[(0,a.jsx)("p",{className:d.default.cardTitle,children:"Apply Type"}),(0,a.jsxs)("p",{className:d.default.formLabel,children:["all"===n.apply_type&&"Entire Store","category"===n.apply_type&&"Specific Categories","product"===n.apply_type&&"Specific Products"]})]}),"category"===n.apply_type&&(0,a.jsxs)("div",{children:[(0,a.jsx)("p",{className:d.default.cardTitle,style:{marginBottom:8},children:"Selected Categories"}),(0,a.jsx)("ul",{style:{paddingLeft:20,color:"var(--color-text-primary)"},children:n.selectedCategories.map(e=>(0,a.jsx)("li",{children:e.categories?.name||e.category_id},e.category_id))})]}),"product"===n.apply_type&&(0,a.jsxs)("div",{children:[(0,a.jsx)("p",{className:d.default.cardTitle,style:{marginBottom:8},children:"Selected Products"}),(0,a.jsx)("ul",{style:{paddingLeft:20,color:"var(--color-text-primary)"},children:n.selectedProducts.map(e=>(0,a.jsx)("li",{children:e.products?.product_name||e.product_id},e.product_id))})]})]}),(0,a.jsxs)("div",{className:d.default.formSection,children:[(0,a.jsx)("h2",{className:d.default.sectionTitle,children:"Redemption Statistics"}),(0,a.jsxs)("div",{className:d.default.analyticsGrid,style:{gridTemplateColumns:"1fr 1fr"},children:[(0,a.jsxs)("div",{className:d.default.analyticsCard,style:{padding:16},children:[(0,a.jsx)("div",{className:d.default.cardTitle,children:"Used Count"}),(0,a.jsx)("div",{className:d.default.cardValue,children:n.used_count||0})]}),(0,a.jsxs)("div",{className:d.default.analyticsCard,style:{padding:16},children:[(0,a.jsx)("div",{className:d.default.cardTitle,children:"Remaining"}),(0,a.jsx)("div",{className:d.default.cardValue,children:0===n.usage_limit?"∞":n.usage_limit-(n.used_count||0)})]})]})]})]})]})]})}])}]);