"use strict";(()=>{var e={};e.id=7570,e.ids=[7570,2331],e.modules={53524:e=>{e.exports=require("@prisma/client")},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")},23738:(e,a,t)=>{t.r(a),t.d(a,{originalPathname:()=>h,patchFetch:()=>k,requestAsyncStorage:()=>l,routeModule:()=>p,serverHooks:()=>g,staticGenerationAsyncStorage:()=>c});var r={};t.r(r),t.d(r,{POST:()=>m});var i=t(49303),s=t(88716),o=t(60670),n=t(87070),d=t(72331);let u=d.prisma;async function m(e){try{let a=e.headers.get("x-user-uuid");if(!a)return n.NextResponse.json({success:!1,message:"Unauthorized"},{status:401});let{code:t}=await e.json();if(!t||"string"!=typeof t||0===t.trim().length)return n.NextResponse.json({success:!1,message:"Kode promo tidak boleh kosong"},{status:400});let r=t.trim().toUpperCase(),i=await u.promoCode.findUnique({where:{code:r},include:{usages:{where:{userId:a}}}});if(!i)return n.NextResponse.json({success:!1,message:"Kode promo tidak ditemukan atau tidak valid"},{status:404});if(!i.isActive)return n.NextResponse.json({success:!1,message:"Kode promo sudah tidak aktif"},{status:400});if(i.expiresAt&&new Date(i.expiresAt)<new Date)return n.NextResponse.json({success:!1,message:"Kode promo sudah kadaluarsa"},{status:400});if(null!==i.maxUses&&i.usedCount>=i.maxUses)return n.NextResponse.json({success:!1,message:"Kode promo sudah habis digunakan"},{status:400});if(i.usages.length>0)return n.NextResponse.json({success:!1,message:"Kamu sudah pernah menggunakan kode promo ini"},{status:400});let s=await d.prisma.user.findUnique({where:{id:a},select:{id:!0,premiumTier:!0,premiumExpiresAt:!0,totalCoins:!0,totalPoints:!0}});if(!s)return n.NextResponse.json({success:!1,message:"User tidak ditemukan"},{status:404});let o=new Date,m=i.rewardType||"premium",p=i.rewardAmount||0,l=i.durationDays,c={},g="",h="";await d.prisma.$transaction(async e=>{if(await e.promoCodeUsage.create({data:{promoCodeId:i.id,userId:a}}),await e.promoCode.update({where:{id:i.id},data:{usedCount:{increment:1}}}),"premium"===m){let t=o;"free"!==s.premiumTier&&s.premiumExpiresAt&&new Date(s.premiumExpiresAt)>o&&(t=new Date(s.premiumExpiresAt));let n=new Date(t.getTime()+864e5*l),d=n.toLocaleDateString("id-ID",{day:"numeric",month:"long",year:"numeric"});await e.user.update({where:{id:a},data:{premiumTier:"monthly",premiumExpiresAt:n}}),await e.premiumSubscription.create({data:{userId:a,planType:`promo_${l}d`,amount:0,currency:"IDR",paymentProvider:"promo_code",paymentId:i.id,startsAt:o,expiresAt:n,isActive:!0,autoRenew:!1}}),c={reward_type:"premium",duration_days:l,expires_at:n.toISOString(),expires_formatted:d},g="\uD83C\uDF89 Kode Promo Berhasil! Premium Aktif",h=`Selamat! Kode promo **${r}** berhasil digunakan.

Akses Premium **${l} Hari** telah aktif hingga **${d}**.

Nikmati semua fitur premium:
• 7 seksi prediksi eksklusif
• Analisis AI setiap pertandingan
• Misi premium dengan hadiah lebih besar

Selamat berprediksi! 🏆`}else if("coins"===m){let t=(s.totalCoins||0)+p;await e.user.update({where:{id:a},data:{totalCoins:t}}),await e.coinsLedger.create({data:{userId:a,amount:p,type:"credit",source:"promo_code",referenceId:i.id,description:`Kode promo ${r}`,balanceAfter:t}}),c={reward_type:"coins",reward_amount:p,new_balance:t},g="\uD83E\uDE99 Kode Promo Berhasil! Koin Diterima",h=`Selamat! Kode promo **${r}** berhasil digunakan.

**${p.toLocaleString("id-ID")} Koin** telah ditambahkan ke akunmu!

Saldo koin kamu sekarang: **${t.toLocaleString("id-ID")} Koin**

Gunakan koin di Marketplace untuk item eksklusif! 🛒`}else if("points"===m){let t=(s.totalPoints||0)+p;await e.user.update({where:{id:a},data:{totalPoints:t}}),await e.pointsLedger.create({data:{userId:a,amount:p,type:"credit",source:"promo_code",referenceId:i.id,description:`Kode promo ${r}`,balanceAfter:t}}),c={reward_type:"points",reward_amount:p,new_balance:t},g="⭐ Kode Promo Berhasil! Poin Diterima",h=`Selamat! Kode promo **${r}** berhasil digunakan.

**${p.toLocaleString("id-ID")} Poin** telah ditambahkan ke akunmu!

Saldo poin kamu sekarang: **${t.toLocaleString("id-ID")} Poin**

Gunakan poin di Marketplace untuk item eksklusif! 🛒`}await e.message.create({data:{threadId:`promo_${i.id}_${a}`,senderType:"system",senderId:null,recipientId:a,subject:g,content:h,isRead:!1}}),await e.auditLog.create({data:{userId:a,action:"promo_code_redeemed",entityType:"promo_code",entityId:i.id,metadata:{code:r,reward_type:m,reward_amount:p,duration_days:"premium"===m?l:null}}})});let k="";return"premium"===m?k=`Kode promo berhasil! Premium ${l} hari aktif hingga ${c.expires_formatted}.`:"coins"===m?k=`Kode promo berhasil! ${p.toLocaleString("id-ID")} koin telah ditambahkan.`:"points"===m&&(k=`Kode promo berhasil! ${p.toLocaleString("id-ID")} poin telah ditambahkan.`),n.NextResponse.json({success:!0,message:k,data:c})}catch(e){return console.error("Promo redeem error:",e),n.NextResponse.json({success:!1,message:"Gagal memproses kode promo"},{status:500})}}let p=new i.AppRouteRouteModule({definition:{kind:s.x.APP_ROUTE,page:"/api/promo/redeem/route",pathname:"/api/promo/redeem",filename:"route",bundlePath:"app/api/promo/redeem/route"},resolvedPagePath:"C:\\xampp\\htdocs\\Bolafest\\app\\api\\promo\\redeem\\route.ts",nextConfigOutput:"standalone",userland:r}),{requestAsyncStorage:l,staticGenerationAsyncStorage:c,serverHooks:g}=p,h="/api/promo/redeem/route";function k(){return(0,o.patchFetch)({serverHooks:g,staticGenerationAsyncStorage:c})}},72331:(e,a,t)=>{t.d(a,{prisma:()=>i});var r=t(53524);let i=globalThis.prisma??new r.PrismaClient({log:["error"]})}};var a=require("../../../../webpack-runtime.js");a.C(e);var t=e=>a(a.s=e),r=a.X(0,[9276,5972],()=>t(23738));module.exports=r})();