"use strict";(()=>{var e={};e.id=3671,e.ids=[3671],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")},44807:(e,t,r)=>{r.r(t),r.d(t,{originalPathname:()=>x,patchFetch:()=>E,requestAsyncStorage:()=>I,routeModule:()=>m,serverHooks:()=>T,staticGenerationAsyncStorage:()=>_});var a={};r.r(a),r.d(a,{POST:()=>w});var n=r(73278),c=r(45002),s=r(54877),o=r(71309),i=r(18445),u=r(73275),p=r(82208),d=r(37642),l=r(68172);let y=d.Ry({amount:d.Rx().positive("Amount must be positive"),paymentMethod:d.Km(["stripe","mobile_money","bank_transfer","cash"]),currency:d.Z_().default("UGX"),description:d.Z_().optional(),mobileMoneyProvider:d.Z_().optional(),mobileMoneyPhone:d.Z_().optional(),mobileMoneyReference:d.Z_().optional(),bankTransferReference:d.Z_().optional(),depositReceiptUrl:d.Z_().optional()});async function w(e){try{let t,r;let a=await (0,i.getServerSession)(u.Lz);if(!a?.user?.id)return o.NextResponse.json({error:"Unauthorized"},{status:401});let n=a.user.id,c=a.user.isTenant?"tenant":a.user.isTechnician?"technician":"user",s=await e.json(),d=y.safeParse(s);if(!d.success)return o.NextResponse.json({error:"Validation failed",details:d.error.issues},{status:400});let{amount:w,paymentMethod:m,currency:I,description:_,...T}=d.data;if("tenant"===c){let e=await p._.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 p._.$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
      `,0===t.length&&(await p._.$executeRaw`
          INSERT INTO wallets (id, userType, tenantId, companyId, currencyId, balance, status, createdAt, updatedAt)
          VALUES (${crypto.randomUUID()}, 'tenant', ${n}, ${r}, 'UGX', 0, 'active', NOW(), NOW())
        `,t=await p._.$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"===c){let e=await p._.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 p._.$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
      `,0===t.length&&(await p._.$executeRaw`
          INSERT INTO wallets (id, userType, technicianId, companyId, currencyId, balance, status, createdAt, updatedAt)
          VALUES (${crypto.randomUUID()}, 'technician', ${n}, ${r}, 'UGX', 0, 'active', NOW(), NOW())
        `,t=await p._.$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 p._.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 p._.$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
      `,0===t.length&&(await p._.$executeRaw`
          INSERT INTO wallets (id, userType, userId, companyId, currencyId, balance, status, createdAt, updatedAt)
          VALUES (${crypto.randomUUID()}, 'user', ${n}, ${r}, 'UGX', 0, 'active', NOW(), NOW())
        `,t=await p._.$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:"Failed to create or find wallet"},{status:500});let x={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},E="pending",N=!1;"user"===c&&"company_admin"===a.user.role&&("cash"===m||"bank_transfer"===m)?(E="completed",N=!0):"technician"===c&&"cash"===m?(E="pending_approval",N=!1):"tenant"===c&&"cash"===m?(E="pending_approval",N=!1):"bank_transfer"===m?(E="pending_approval",N=!1):"cash"!==m?(E="pending",N=!1):(E="completed",N=!0);let R=crypto.randomUUID();await p._.$executeRaw`
       INSERT INTO wallet_transactions (id, walletId, companyId, transactionType, amount, currency, description, paymentMethod, status, metadata, createdAt, updatedAt)
       VALUES (${R}, ${x.id}, ${r}, 'top_up', ${w}, ${I}, ${_||`Wallet top-up via ${m}`}, ${m}, ${E}, ${JSON.stringify(T)}, NOW(), NOW())
     `,N?(await p._.$executeRaw`
         UPDATE wallets SET balance = balance + ${w}, updatedAt = NOW() WHERE id = ${x.id}
       `,await (0,l.yx)(R),await (0,l.XL)(x.id)):await (0,l._V)(R);let h={id:R,walletId:x.id,companyId:r,transactionType:"top_up",amount:w,currency:I,description:_||`Wallet top-up via ${m}`,paymentMethod:m,status:E,metadata:T,createdAt:new Date().toISOString(),updatedAt:new Date().toISOString()};return o.NextResponse.json({transaction:h,message:"pending_approval"===E?"Top-up request submitted for approval":"Wallet topped up successfully"})}catch(e){return console.error("Error topping up wallet:",e),o.NextResponse.json({error:"Internal server error"},{status:500})}}let m=new n.AppRouteRouteModule({definition:{kind:c.x.APP_ROUTE,page:"/api/wallet/top-up/route",pathname:"/api/wallet/top-up",filename:"route",bundlePath:"app/api/wallet/top-up/route"},resolvedPagePath:"c:\\xampp\\htdocs\\propmaster.io\\src\\app\\api\\wallet\\top-up\\route.ts",nextConfigOutput:"standalone",userland:a}),{requestAsyncStorage:I,staticGenerationAsyncStorage:_,serverHooks:T}=m,x="/api/wallet/top-up/route";function E(){return(0,s.patchFetch)({serverHooks:T,staticGenerationAsyncStorage:_})}}};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(44807));module.exports=a})();