"use strict";(()=>{var e={};e.id=414,e.ids=[414],e.modules={53524:e=>{e.exports=require("@prisma/client")},98432:e=>{e.exports=require("bcryptjs")},72934:e=>{e.exports=require("next/dist/client/components/action-async-storage.external.js")},54580:e=>{e.exports=require("next/dist/client/components/request-async-storage.external.js")},45869:e=>{e.exports=require("next/dist/client/components/static-generation-async-storage.external.js")},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")},27790:e=>{e.exports=require("assert")},78893:e=>{e.exports=require("buffer")},84770:e=>{e.exports=require("crypto")},17702:e=>{e.exports=require("events")},32615:e=>{e.exports=require("http")},35240:e=>{e.exports=require("https")},86624:e=>{e.exports=require("querystring")},17360:e=>{e.exports=require("url")},21764:e=>{e.exports=require("util")},71568:e=>{e.exports=require("zlib")},8377:(e,t,r)=>{r.r(t),r.d(t,{originalPathname:()=>f,patchFetch:()=>_,requestAsyncStorage:()=>x,routeModule:()=>y,serverHooks:()=>I,staticGenerationAsyncStorage:()=>h});var a={};r.r(a),r.d(a,{POST:()=>m});var n=r(73278),s=r(45002),i=r(54877),o=r(71309),c=r(18445),u=r(73275),d=r(82208),p=r(37642),l=r(68172);let w=p.Ry({amount:p.Rx().positive("Amount must be positive"),paymentMethod:p.Km(["mobile_money","bank_transfer"]),currency:p.Z_().default("UGX"),description:p.Z_().optional(),phoneNumber:p.Z_().optional(),bankAccountName:p.Z_().optional(),bankAccountNumber:p.Z_().optional(),bankName:p.Z_().optional()});async function m(e){try{let t,r;let a=await (0,c.getServerSession)(u.Lz);if(!a?.user?.id)return o.NextResponse.json({error:"Unauthorized"},{status:401});let n=a.user.id,s=a.user.isTenant?"tenant":a.user.isTechnician?"technician":"user",i=await e.json(),p=w.safeParse(i);if(!p.success)return o.NextResponse.json({error:"Validation failed",details:p.error.issues},{status:400});let{amount:m,paymentMethod:y,currency:x,description:h,...I}=p.data;if("tenant"===s){let e=await d._.tenant.findUnique({where:{id:n},select:{companyId:!0}});if(!e?.companyId)return o.NextResponse.json({error:"Tenant not associated with a company"},{status:400});r=e.companyId,t=await d._.$queryRaw`
        SELECT w.*, c.code as currency_code, c.symbol as currency_symbol, c.name as currency_name
        FROM wallets w
        LEFT JOIN currencies c ON w.currencyId = c.id
        WHERE w.userType = 'tenant' AND w.tenantId = ${n}
        LIMIT 1
      `}else if("technician"===s){let e=await d._.technician.findUnique({where:{id:n},select:{companyId:!0}});if(!e?.companyId)return o.NextResponse.json({error:"Technician not associated with a company"},{status:400});r=e.companyId,t=await d._.$queryRaw`
        SELECT w.*, c.code as currency_code, c.symbol as currency_symbol, c.name as currency_name
        FROM wallets w
        LEFT JOIN currencies c ON w.currencyId = c.id
        WHERE w.userType = 'technician' AND w.technicianId = ${n}
        LIMIT 1
      `}else{let e=await d._.user.findUnique({where:{id:n},select:{companyId:!0,role:!0}});if(!e?.companyId)return o.NextResponse.json({error:"User not associated with a company"},{status:400});r=e.companyId,t=await d._.$queryRaw`
        SELECT w.*, c.code as currency_code, c.symbol as currency_symbol, c.name as currency_name
        FROM wallets w
        LEFT JOIN currencies c ON w.currencyId = c.id
        WHERE w.userType = 'user' AND w.userId = ${n}
        LIMIT 1
      `}if(0===t.length)return o.NextResponse.json({error:"Wallet not found"},{status:404});let f={id:t[0].id,balance:t[0].balance,companyId:t[0].companyId,currency:{code:t[0].currency_code,symbol:t[0].currency_symbol,name:t[0].currency_name},status:t[0].status};if(f.balance<m)return o.NextResponse.json({error:"Insufficient wallet balance"},{status:400});let _=crypto.randomUUID();await d._.$executeRaw`
      INSERT INTO wallet_transactions (id, walletId, companyId, transactionType, amount, currency, description, paymentMethod, reference, externalReference, status, metadata, createdAt, updatedAt)
      VALUES (?, ?, ?, 'withdrawal', ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
    `,f.id,JSON.stringify(I),"technician"===s&&await d._.$executeRaw`
        UPDATE wallets SET balance = balance - ${m}, updatedAt = NOW() WHERE id = ${f.id}
      `,await (0,l.JT)(_);let b={id:_,walletId:f.id,companyId:r,transactionType:"withdrawal",amount:-m,currency:x,description:h||`Wallet withdrawal via ${y}`,paymentMethod:y,status:"pending_approval",metadata:I,createdAt:new Date().toISOString(),updatedAt:new Date().toISOString()};return o.NextResponse.json({transaction:b,message:"Withdrawal request submitted successfully. Please wait for admin approval."})}catch(e){return console.error("Error processing withdrawal:",e),o.NextResponse.json({error:"Internal server error"},{status:500})}}let y=new n.AppRouteRouteModule({definition:{kind:s.x.APP_ROUTE,page:"/api/wallet/withdraw/route",pathname:"/api/wallet/withdraw",filename:"route",bundlePath:"app/api/wallet/withdraw/route"},resolvedPagePath:"c:\\xampp\\htdocs\\propmaster.io\\src\\app\\api\\wallet\\withdraw\\route.ts",nextConfigOutput:"standalone",userland:a}),{requestAsyncStorage:x,staticGenerationAsyncStorage:h,serverHooks:I}=y,f="/api/wallet/withdraw/route";function _(){return(0,i.patchFetch)({serverHooks:I,staticGenerationAsyncStorage:h})}}};var t=require("../../../../webpack-runtime.js");t.C(e);var r=e=>t(t.s=e),a=t.X(0,[7787,9121,4833,7642,3976],()=>r(8377));module.exports=a})();