package reports

import (
	db "ttime/internal/database"

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

// GetWeeklyReportsUserHandler retrieves all weekly reports for a user in a specific project
func GetWeeklyReportsUserHandler(c *fiber.Ctx) error {
	// Extract the necessary parameters from the token
	user := c.Locals("user").(*jwt.Token)
	claims := user.Claims.(jwt.MapClaims)
	username := claims["name"].(string)

	// Extract necessary (path) parameters from the request
	projectName := c.Params("projectName")

	// TODO: Here we need to check whether the user is a member of the project
	// If not, we should return an error. On the other hand, if the user not a member,
	// the returned list of reports will (should) allways be empty.

	// Retrieve weekly reports for the user in the project from the database
	reports, err := db.GetDb(c).GetWeeklyReportsUser(username, projectName)
	if err != nil {
		log.Error("Error getting weekly reports for user:", username, "in project:", projectName, ":", err)
		return c.Status(500).SendString(err.Error())
	}

	log.Info("Returning weekly reports for user:", username, "in project:", projectName)

	// Return the list of reports as JSON
	return c.JSON(reports)
}