import { postgresConnection } from "db";
import type { NextApiRequest, NextApiResponse } from "next";

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  try {
    if (req.method === "GET") {
      const db = postgresConnection;

      const result = await db.query("SELECT wallet FROM users WHERE id = $1", [
        req.query.userId,
      ]);

      if (result.rowCount > 0) {
        return res.status(200).json({ wallet: result.rows[0].wallet });
      } else {
        return res.status(404).json({ message: "User not found" });
      }
    }
  } catch (error) {
    res.status(500).json(error);
  }
}
