import express from "express";
import dotenv from "dotenv";
const router = express.Router();
dotenv.config();

import { getUser, newEntry, confirmUser } from "./frontend.data.js";

router.post("/new-entry", async (req, res) => {
  const username = req.query.username;
  const result = await newEntry(req.body, username);
  if (!result.success) {
    return res.status(500).json({ message: "Form Data Invalid" });
  }
  res.sendStatus(204);
});

router.get("/users", async (req, res) => {
  const users = await getUser();
  res.json(users);
});

router.get("/confirm-user", async (req, res) => {
  const username = req.query.username;
  if (!username) {
    return res.status(400).json({ message: "Username is required" });
  }
  const user = await confirmUser(username);
  if (!user) {
    return res.status(404).json({ message: "User not found" });
  }
  res.json(user);
});

export default router;
