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