package handlers

import (
	"strconv"
	"ttime/internal/types"

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

func (gs *GState) SubmitWeeklyReport(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)

	report := new(types.NewWeeklyReport)
	if err := c.BodyParser(report); err != nil {
		log.Info("Error parsing weekly report")
		return c.Status(400).SendString(err.Error())
	}

	// Make sure all the fields of the report are valid
	if report.Week < 1 || report.Week > 52 {
		log.Info("Invalid week number")
		return c.Status(400).SendString("Invalid week number")
	}
	if report.DevelopmentTime < 0 || report.MeetingTime < 0 || report.AdminTime < 0 || report.OwnWorkTime < 0 || report.StudyTime < 0 || report.TestingTime < 0 {
		log.Info("Invalid time report")
		return c.Status(400).SendString("Invalid time report")
	}

	if err := gs.Db.AddWeeklyReport(report.ProjectName, username, report.Week, report.DevelopmentTime, report.MeetingTime, report.AdminTime, report.OwnWorkTime, report.StudyTime, report.TestingTime); err != nil {
		log.Info("Error adding weekly report to db:", err)
		return c.Status(500).SendString(err.Error())
	}

	log.Info("Weekly report added")
	return c.Status(200).SendString("Time report added")
}

// Handler for retrieving weekly report
func (gs *GState) GetWeeklyReport(c *fiber.Ctx) error {
	// Extract the necessary parameters from the request
	user := c.Locals("user").(*jwt.Token)
	claims := user.Claims.(jwt.MapClaims)
	username := claims["name"].(string)

	log.Info("Getting weekly report for: ", username)

	// Extract project name and week from query parameters
	projectName := c.Query("projectName")
	week := c.Query("week")

	if projectName == "" || week == "" {
		log.Info("Missing project name or week number")
		return c.Status(400).SendString("Missing project name or week number")
	}

	// Convert week to integer
	weekInt, err := strconv.Atoi(week)
	if err != nil {
		log.Info("Invalid week number")
		return c.Status(400).SendString("Invalid week number")
	}

	// Call the database function to get the weekly report
	report, err := gs.Db.GetWeeklyReport(username, projectName, weekInt)
	if err != nil {
		log.Info("Error getting weekly report from db:", err)
		return c.Status(500).SendString(err.Error())
	}

	log.Info("Returning weekly report")
	// Return the retrieved weekly report
	return c.JSON(report)
}

type ReportId struct {
	ReportId int
}

func (gs *GState) SignReport(c *fiber.Ctx) error {
	// Extract the necessary parameters from the token
	user := c.Locals("user").(*jwt.Token)
	claims := user.Claims.(jwt.MapClaims)
	projectManagerUsername := claims["name"].(string)

	log.Info("Signing report for: ", projectManagerUsername)

	// Extract report ID from the request query parameters
	// reportID := c.Query("reportId")
	rid := new(ReportId)
	if err := c.BodyParser(rid); err != nil {
		return err
	}
	log.Info("Signing report for: ", rid.ReportId)

	// Get the project manager's ID
	projectManagerID, err := gs.Db.GetUserId(projectManagerUsername)
	if err != nil {
		log.Info("Failed to get project manager ID")
		return c.Status(500).SendString("Failed to get project manager ID")
	}
	log.Info("Project manager ID: ", projectManagerID)

	// Call the database function to sign the weekly report
	err = gs.Db.SignWeeklyReport(rid.ReportId, projectManagerID)
	if err != nil {
		log.Info("Error signing weekly report:", err)
		return c.Status(500).SendString(err.Error())
	}

	return c.Status(200).SendString("Weekly report signed successfully")
}

// GetWeeklyReportsUserHandler retrieves all weekly reports for a user in a specific project
func (gs *GState) 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 := gs.Db.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)
}
