package projects

import (
	db "ttime/internal/database"

	"github.com/gofiber/fiber/v2"
	"github.com/golang-jwt/jwt/v5"
)

// GetUserProjects returns all projects that the user is a member of
func GetUserProjects(c *fiber.Ctx) error {
	// First we get the username from the token
	user := c.Locals("user").(*jwt.Token)
	claims := user.Claims.(jwt.MapClaims)
	username := claims["name"].(string)

	// Then dip into the database to get the projects
	projects, err := db.GetDb(c).GetProjectsForUser(username)
	if err != nil {
		return c.Status(500).SendString(err.Error())
	}

	// Return a json serialized list of projects
	return c.JSON(projects)
}