package users

import (
	"strconv"
	db "ttime/internal/database"

	"github.com/gofiber/fiber/v2"
)

// Return the username of a user given their user id
func GetUserName(c *fiber.Ctx) error {
	// Check the query params for userId
	user_id_string := c.Query("userId")
	if user_id_string == "" {
		return c.Status(400).SendString("Missing user id")
	}

	// Convert to int
	user_id, err := strconv.Atoi(user_id_string)
	if err != nil {
		return c.Status(400).SendString("Invalid user id")
	}

	// Get the username from the database
	username, err := db.GetDb(c).GetUserName(user_id)
	if err != nil {
		return c.Status(500).SendString(err.Error())
	}

	// Send the nuclear launch codes to north korea
	return c.JSON(fiber.Map{"username": username})
}