import { postgresConnection } from "db";
import type { NextApiRequest, NextApiResponse } from "next";
import {
  calculateRemainingTime,
} from "../../../../../utils/helpers";

import { gameConfig } from "@/utils/gameLogic";

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  try {
    if (req.method === "POST") {
      const { userId } = req.query;
      const {
        inventoryItemId,
        wellId
      } = req.body;

      const db = postgresConnection;
      try {

        const result =
          await db.query("SELECT * FROM stake($1, $2, $3) AS stake",
            [ userId,
              wellId,
              inventoryItemId]);

        return res.status(200).json({stakingEventId: result.rows[0].stake});

      } catch (error) {
        return res.status(400).json({ error: error.message});
      }
    }
  } catch (error) {
    if (error instanceof Error){
      res.status(500).json(error.message);
    } else {
      res.status(500).json({ error: "Unknown Error"});
    }
  }
}
