module.exports=[91603,a=>{"use strict";var b=a.i(50944),c=a.i(3629);a.s(["useAppRouter",0,function(){let a=(0,b.useRouter)(),{startLoading:d}=(0,c.useLoader)();return{...a,push:(b,c)=>{console.log("[Loader] Navigation started:",b),d&&d(),a.push(b,c)},replace:(b,c)=>{console.log("[Loader] Navigation started:",b),d&&d(),a.replace(b,c)}}}])},3512,a=>{"use strict";let b=(0,a.i(64831).default)("plus",[["path",{d:"M5 12h14",key:"1ays0h"}],["path",{d:"M12 5v14",key:"s699le"}]]);a.s(["Plus",0,b],3512)},40315,a=>{"use strict";var b=a.i(29562);let c=new class{async fetchCoupons(a){let{data:c,error:d}=await b.supabase.from("coupons").select("*").eq("vendor_id",a).order("created_at",{ascending:!1});if(d)throw console.error("Error fetching coupons:",d),d;return c||[]}async fetchCouponDetails(a){let{data:c,error:d}=await b.supabase.from("coupons").select("*").eq("id",a).single();if(d)throw d;let e=[],f=[];if("category"===c.apply_type){let{data:c}=await b.supabase.from("coupon_categories").select(`
          category_id,
          categories (
            name
          )
        `).eq("coupon_id",a);e=c||[]}else if("product"===c.apply_type){let{data:c}=await b.supabase.from("coupon_products").select(`
          product_id,
          products (
            product_name
          )
        `).eq("coupon_id",a);f=c||[]}return{...c,selectedCategories:e,selectedProducts:f}}async createCoupon(a,c){let{data:d,error:e}=await b.supabase.from("coupons").insert([a]).select().single();if(e){if("23505"===e.code)throw Error("This coupon code has already been created. Please try a different code or generate a new one.");throw e}let f=d.id;if("category"===a.apply_type&&c.categories?.length>0){let a=c.categories.map(a=>({coupon_id:f,category_id:a}));await b.supabase.from("coupon_categories").insert(a)}else if("product"===a.apply_type&&c.products?.length>0){let a=c.products.map(a=>({coupon_id:f,product_id:a}));await b.supabase.from("coupon_products").insert(a)}return d}async updateCouponStatus(a,c){let{data:d,error:e}=await b.supabase.from("coupons").update({is_active:c}).eq("id",a).select().single();if(e)throw e;return d}async deleteCoupon(a){let{error:c}=await b.supabase.from("coupons").delete().eq("id",a);if(c)throw c;return!0}async fetchVendorProducts(a){let{data:c,error:d}=await b.supabase.from("products").select("id, product_name, category_id").eq("vendor_id",a).order("product_name");return d?(console.warn("Failed to fetch products:",d.message),[]):c||[]}async fetchCategories(){let{data:a,error:c}=await b.supabase.from("categories").select("id, name").order("name");return c?(console.warn("Failed to fetch categories:",c.message),[]):a||[]}async validateCoupon(a,c,d){let{subtotal:e,products:f}=c,{data:g,error:h}=await b.supabase.from("coupons").select("*").eq("code",a).single();if(h||!g)return{isValid:!1,error:"Invalid coupon code"};if(!g.is_active)return{isValid:!1,error:"This coupon is no longer active"};let i=new Date;if(new Date(g.start_date)>i)return{isValid:!1,error:"This coupon is not yet valid"};if(new Date(g.end_date)<i)return{isValid:!1,error:"This coupon has expired"};if(g.usage_limit>0&&g.used_count>=g.usage_limit)return{isValid:!1,error:"Coupon usage limit has been reached"};if(d){let{count:a}=await b.supabase.from("coupon_usages").select("*",{count:"exact",head:!0}).eq("coupon_id",g.id).eq("user_id",d);if(a>=g.per_user_limit)return{isValid:!1,error:"You have reached your usage limit for this coupon"}}if(g.min_order_amount&&e<g.min_order_amount)return{isValid:!1,error:`Minimum order amount is ₹${g.min_order_amount}`};let j=!1,k=0;if("all"===g.apply_type)k=e,j=!0;else if("category"===g.apply_type){let{data:a}=await b.supabase.from("coupon_categories").select("category_id").eq("coupon_id",g.id),c=a.map(a=>a.category_id);for(let a of f)c.includes(a.category_id)&&(k+=a.price*a.quantity,j=!0)}else if("product"===g.apply_type){let{data:a}=await b.supabase.from("coupon_products").select("product_id").eq("coupon_id",g.id),c=a.map(a=>a.product_id);for(let a of f)c.includes(a.id)&&(k+=a.price*a.quantity,j=!0)}if(!j)return{isValid:!1,error:"Your cart does not contain any items eligible for this coupon."};let l=0;return"percentage"===g.type?(l=k*(g.value/100),g.max_discount&&l>g.max_discount&&(l=g.max_discount)):"fixed"===g.type&&(l=g.value),l=Math.min(l,k),{isValid:!0,couponId:g.id,discountAmount:l}}};a.s(["couponsRepository",0,c])},45122,a=>{a.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"})},12848,a=>{"use strict";var b=a.i(87924),c=a.i(72131),d=a.i(3512),e=a.i(33540),f=a.i(31247),g=a.i(41476),h=a.i(64831);let i=(0,h.default)("activity",[["path",{d:"M22 12h-2.48a2 2 0 0 0-1.93 1.46l-2.35 8.36a.25.25 0 0 1-.48 0L9.24 2.18a.25.25 0 0 0-.48 0l-2.35 8.36A2 2 0 0 1 4.49 12H2",key:"169zse"}]]);var j=a.i(79362);let k=(0,h.default)("circle-alert",[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["line",{x1:"12",x2:"12",y1:"8",y2:"12",key:"1pkeuh"}],["line",{x1:"12",x2:"12.01",y1:"16",y2:"16",key:"4dfq90"}]]);var l=a.i(91603),m=a.i(40315),n=a.i(29562),o=a.i(45122),p=a.i(31470);a.s(["default",0,function(){let a=(0,l.useAppRouter)(),[h,q]=(0,c.useState)(!1);(0,c.useEffect)(()=>{q(!0)},[]);let{data:r,analytics:s,loading:t,error:u,filters:v,pagination:w,actions:x}=function(){let[a,b]=(0,c.useState)([]),[d,e]=(0,c.useState)(!0),[f,g]=(0,c.useState)(null),[h,i]=(0,c.useState)(""),[j,k]=(0,c.useState)("all"),[l,o]=(0,c.useState)("all"),[p,q]=(0,c.useState)("all"),[r,s]=(0,c.useState)(1);(0,c.useEffect)(()=>{t()},[]);let t=async()=>{try{e(!0);let{data:{user:a}}=await n.supabase.auth.getUser();if(!a)throw Error("Not authenticated");let{data:c}=await n.supabase.from("vendor_profiles").select("id").eq("user_id",a.id).single();if(!c)throw Error("Vendor profile not found");let d=await m.couponsRepository.fetchCoupons(c.id);b(d),g(null)}catch(a){console.warn("Failed to load coupons:",a.message),g(`Failed to load coupons: ${a.message}`),b([])}finally{e(!1)}},u=async a=>{try{return await m.couponsRepository.deleteCoupon(a),b(b=>b.filter(b=>b.id!==a)),!0}catch(a){throw console.warn("Failed to delete coupon:",a.message),a}},v=async(a,c)=>{try{let d=!c;return await m.couponsRepository.updateCouponStatus(a,d),b(b=>b.map(b=>b.id===a?{...b,is_active:d}:b)),d}catch(a){throw console.warn("Failed to toggle status:",a.message),a}},w=(0,c.useMemo)(()=>a.filter(a=>{let b=a.code?.toLowerCase().includes(h.toLowerCase()),c=a.is_active,d=new Date(a.end_date)<new Date,e=!0;"active"===j&&(e=c&&!d),"inactive"===j&&(e=!c),"expired"===j&&(e=d);let f=!0;"all"!==l&&(f=a.type===l);let g=!0;return"all"!==p&&(g=a.apply_type===p),b&&e&&f&&g}),[a,h,j,l,p]),x=Math.ceil(w.length/10);return{data:w.slice((r-1)*10,10*r),analytics:(0,c.useMemo)(()=>{let b=a.length,c=0,d=0,e=0,f=0,g=new Date;return a.forEach(a=>{a.is_active&&new Date(a.end_date)>g?c++:new Date(a.end_date)<g&&d++,e+=a.used_count||0,f+=a.total_discount_given||0}),{total:b,active:c,expired:d,redemptions:e,totalDiscount:f}},[a]),loading:d,error:f,filters:{searchQuery:h,setSearchQuery:i,statusFilter:j,setStatusFilter:k,typeFilter:l,setTypeFilter:o,applyScopeFilter:p,setApplyScopeFilter:q},pagination:{currentPage:r,setCurrentPage:s,totalPages:x},actions:{deleteCoupon:u,toggleStatus:v,refresh:t}}}();if(!h)return null;if(t&&0===r.length)return(0,b.jsx)("div",{className:o.default.container,style:{display:"flex",justifyContent:"center",alignItems:"center",minHeight:"60vh"},children:(0,b.jsx)(p.default,{text:"Loading Coupons..."})});let y=async a=>{window.confirm("Are you sure you want to delete this coupon? This action cannot be undone.")&&await x.deleteCoupon(a)};return(0,b.jsxs)("div",{className:o.default.container,children:[(0,b.jsxs)("div",{className:o.default.header,children:[(0,b.jsxs)("div",{children:[(0,b.jsx)("h1",{className:o.default.title,children:"Coupons"}),(0,b.jsx)("p",{style:{color:"var(--color-text-secondary)",marginTop:"4px"},children:"Manage discounts, deals, and promotional campaigns."})]}),(0,b.jsxs)("button",{className:o.default.primaryButton,onClick:()=>a.push("/coupons/create"),children:[(0,b.jsx)(d.Plus,{size:18}),"Create Coupon"]})]}),u&&(0,b.jsx)("div",{className:o.default.errorText,children:u}),(0,b.jsxs)("div",{className:o.default.analyticsGrid,children:[(0,b.jsxs)("div",{className:o.default.analyticsCard,children:[(0,b.jsxs)("div",{className:o.default.cardTitle,children:[(0,b.jsx)(f.Tag,{size:16,style:{display:"inline",marginRight:8,verticalAlign:"middle"}}),"Total Coupons"]}),(0,b.jsx)("div",{className:o.default.cardValue,children:s.total})]}),(0,b.jsxs)("div",{className:o.default.analyticsCard,children:[(0,b.jsxs)("div",{className:o.default.cardTitle,children:[(0,b.jsx)(j.CheckCircle,{size:16,style:{display:"inline",marginRight:8,verticalAlign:"middle",color:"var(--color-success)"}}),"Active Coupons"]}),(0,b.jsx)("div",{className:o.default.cardValue,children:s.active})]}),(0,b.jsxs)("div",{className:o.default.analyticsCard,children:[(0,b.jsxs)("div",{className:o.default.cardTitle,children:[(0,b.jsx)(k,{size:16,style:{display:"inline",marginRight:8,verticalAlign:"middle",color:"var(--color-danger)"}}),"Expired Coupons"]}),(0,b.jsx)("div",{className:o.default.cardValue,children:s.expired})]}),(0,b.jsxs)("div",{className:o.default.analyticsCard,children:[(0,b.jsxs)("div",{className:o.default.cardTitle,children:[(0,b.jsx)(i,{size:16,style:{display:"inline",marginRight:8,verticalAlign:"middle"}}),"Total Redemptions"]}),(0,b.jsx)("div",{className:o.default.cardValue,children:s.redemptions})]}),(0,b.jsxs)("div",{className:o.default.analyticsCard,children:[(0,b.jsxs)("div",{className:o.default.cardTitle,children:[(0,b.jsx)(g.Settings,{size:16,style:{display:"inline",marginRight:8,verticalAlign:"middle"}}),"Total Discount Given"]}),(0,b.jsxs)("div",{className:o.default.cardValue,children:["₹",s.totalDiscount]})]})]}),(0,b.jsxs)("div",{className:o.default.actionsBar,children:[(0,b.jsxs)("div",{style:{position:"relative",flex:1,minWidth:"250px"},children:[(0,b.jsx)(e.Search,{size:18,style:{position:"absolute",left:12,top:12,color:"var(--color-text-secondary)"}}),(0,b.jsx)("input",{type:"text",placeholder:"Search by coupon code...",className:o.default.searchInput,style:{paddingLeft:40,width:"100%"},value:v.searchQuery,onChange:a=>v.setSearchQuery(a.target.value)})]}),(0,b.jsxs)("select",{className:o.default.selectInput,value:v.statusFilter,onChange:a=>v.setStatusFilter(a.target.value),children:[(0,b.jsx)("option",{value:"all",children:"All Status"}),(0,b.jsx)("option",{value:"active",children:"Active"}),(0,b.jsx)("option",{value:"inactive",children:"Inactive"}),(0,b.jsx)("option",{value:"expired",children:"Expired"})]}),(0,b.jsxs)("select",{className:o.default.selectInput,value:v.typeFilter,onChange:a=>v.setTypeFilter(a.target.value),children:[(0,b.jsx)("option",{value:"all",children:"All Types"}),(0,b.jsx)("option",{value:"percentage",children:"Percentage"}),(0,b.jsx)("option",{value:"fixed",children:"Fixed Amount"})]}),(0,b.jsxs)("select",{className:o.default.selectInput,value:v.applyScopeFilter,onChange:a=>v.setApplyScopeFilter(a.target.value),children:[(0,b.jsx)("option",{value:"all",children:"All Scopes"}),(0,b.jsx)("option",{value:"all_products",children:"Entire Store"}),(0,b.jsx)("option",{value:"category",children:"Categories"}),(0,b.jsx)("option",{value:"product",children:"Specific Products"})]})]}),(0,b.jsxs)("div",{className:o.default.tableContainer,children:[(0,b.jsxs)("table",{className:o.default.table,children:[(0,b.jsx)("thead",{children:(0,b.jsxs)("tr",{children:[(0,b.jsx)("th",{children:"CODE"}),(0,b.jsx)("th",{children:"TYPE"}),(0,b.jsx)("th",{children:"DISCOUNT"}),(0,b.jsx)("th",{children:"USAGE"}),(0,b.jsx)("th",{children:"VALIDITY"}),(0,b.jsx)("th",{children:"STATUS"}),(0,b.jsx)("th",{children:"ACTIONS"})]})}),(0,b.jsx)("tbody",{children:0===r.length?(0,b.jsx)("tr",{children:(0,b.jsx)("td",{colSpan:"7",style:{textAlign:"center",padding:"32px"},children:(0,b.jsx)("p",{style:{color:"var(--color-text-secondary)"},children:"No coupons found matching your criteria."})})}):r.map(c=>{let d=new Date(c.end_date)<new Date,e=o.default.statusInactive,f="Inactive";return c.is_active&&!d?(e=o.default.statusActive,f="Active"):d&&(e=o.default.statusExpired,f="Expired"),(0,b.jsxs)("tr",{children:[(0,b.jsx)("td",{children:(0,b.jsx)("strong",{children:c.code})}),(0,b.jsx)("td",{style:{textTransform:"capitalize"},children:c.type}),(0,b.jsx)("td",{children:"percentage"===c.type?`${c.value}%`:`₹${c.value}`}),(0,b.jsxs)("td",{children:[c.used_count||0," / ",0===c.usage_limit?"∞":c.usage_limit]}),(0,b.jsxs)("td",{children:[new Date(c.start_date).toLocaleDateString()," - ",(0,b.jsx)("br",{}),new Date(c.end_date).toLocaleDateString()]}),(0,b.jsx)("td",{children:(0,b.jsx)("span",{className:`${o.default.statusBadge} ${e}`,children:f})}),(0,b.jsx)("td",{children:(0,b.jsxs)("div",{className:o.default.actionLinks,children:[(0,b.jsx)("button",{className:o.default.actionLink,onClick:()=>a.push(`/coupons/${c.id}`),children:"Edit"}),(0,b.jsx)("button",{className:o.default.actionLink,onClick:()=>x.toggleStatus(c.id,c.is_active),children:c.is_active?"Deactivate":"Activate"}),(0,b.jsx)("button",{className:`${o.default.actionLink} ${o.default.deleteLink}`,onClick:()=>y(c.id),children:"Delete"})]})})]},c.id)})})]}),w.totalPages>1&&(0,b.jsxs)("div",{className:o.default.pagination,children:[(0,b.jsxs)("span",{style:{color:"var(--color-text-secondary)",fontSize:14},children:["Showing Page ",w.currentPage," of ",w.totalPages]}),(0,b.jsxs)("div",{className:o.default.pageControls,children:[(0,b.jsx)("button",{className:o.default.pageButton,disabled:1===w.currentPage,onClick:()=>w.setCurrentPage(a=>a-1),children:"Previous"}),(0,b.jsx)("button",{className:o.default.pageButton,disabled:w.currentPage===w.totalPages,onClick:()=>w.setCurrentPage(a=>a+1),children:"Next"})]})]})]})]})}],12848)}];

//# sourceMappingURL=_1v82cn6._.js.map