package player

import (
	"database/sql"
	"log"
	"net/http"
	"volleyball/internal/common"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

func GetPlayers(c *gin.Context, db *sql.DB) {
	log.Println(c.GetString("userId"), c.GetString("email"), c.GetString("role"))
	// Simulate fetching players from a database

	players, err := GetAllPlayers(db)
	if err != nil {
		log.Printf("Error retrieving players: %v", err)
		common.RespondError(c, http.StatusInternalServerError, "Failed to retrieve players")
		return
	}

	if len(players) > 0 {
		log.Printf("User %s (%s) requested players", c.GetString("userId"), c.GetString("email"))
		c.JSON(http.StatusOK, players)
		return
	}
	log.Printf("User %s (%s) requested players, but none found", c.GetString("userId"), c.GetString("email"))

	common.RespondError(c, http.StatusNotFound, "No Players found")
}

func CreatePlayer(c *gin.Context, db *sql.DB) {
	var newPlayer Player
	var err error
	if err := c.ShouldBindJSON(&newPlayer); err != nil {
		log.Printf("Error binding player data: %v", err)
		common.RespondError(c, http.StatusBadRequest, "Invalid player data")
		return
	}
	newPlayer.Password, err = common.HashPassword(newPlayer.Password)
	if err != nil {
		log.Printf("Error hashing password: %v", err)
		common.RespondError(c, http.StatusInternalServerError, "Failed to hash password")
		return
	}

	newPlayer.ID = uuid.New().String()
	err = savePlayer(db, newPlayer)
	if err != nil {
		log.Printf("Error saving player: %v", err)
		common.RespondError(c, http.StatusInternalServerError, "Failed to create player")
		return
	}
	err = AddRoleToPlayer(db, newPlayer.ID, "player")
	if err != nil {
		log.Printf("Error adding role to player: %v", err)
		common.RespondError(c, http.StatusInternalServerError, "Failed to assign role to player")
		return
	}

	log.Printf("User %s (%s) created player: %s", c.GetString("userId"), c.GetString("email"), newPlayer.Name)

	common.RespondCreated(c, newPlayer)
}

func GetPlayer(c *gin.Context, db *sql.DB, id string) {
	log.Println("GetPlayer called with ID:", id)
	playerID := c.Param("id")
	if playerID == "" {
		common.RespondError(c, http.StatusBadRequest, "Player ID is required")
		return
	}

	player, err := GetPlayerByID(db, playerID)
	if err != nil {
		log.Printf("Error retrieving player with ID %s: %v", playerID, err)
		common.RespondError(c, http.StatusInternalServerError, "Failed to retrieve player")
		return
	}

	if player.ID == "" {
		log.Printf("Player with ID %s not found", playerID)
		common.RespondError(c, http.StatusNotFound, "Player not found")
		return
	}
	role, err := GetPlayerRole(db, player.ID)
	if err != nil {
		log.Printf("Error retrieving role for player ID %s: %v", player.ID, err)
		common.RespondError(c, http.StatusInternalServerError, "Failed to retrieve player role")
		return
	}
	player.Role = role
	log.Printf(player.ID, player.Name, player.Email, player.Role)
	c.JSON(http.StatusOK, player)

}

func UpdatePlayer(c *gin.Context, db *sql.DB) {
	playerID := c.Param("id")
	if playerID == "" {
		common.RespondError(c, http.StatusBadRequest, "Player ID is required")
		return
	}

	if playerID != c.GetString("userId") || c.GetString("role") != "admin" {
		common.RespondError(c, http.StatusForbidden, "You do not have permission to update this player")
		return
	}

	var updatedPlayer Player
	if err := c.ShouldBindJSON(&updatedPlayer); err != nil {
		log.Printf("Error binding player data: %v", err)
		common.RespondError(c, http.StatusBadRequest, "Invalid player data")
		return
	}

	updatedPlayer.ID = playerID
	err := updatePlayer(db, updatedPlayer)
	if err != nil {
		log.Printf("Error updating player with ID %s: %v", playerID, err)
		common.RespondError(c, http.StatusInternalServerError, "Failed to update player")
		return
	}

	log.Printf("User %s (%s) updated player: %s", c.GetString("userId"), c.GetString("email"), updatedPlayer.Name)

	c.JSON(http.StatusOK, updatedPlayer)
}
func DeletePlayer(c *gin.Context, db *sql.DB) {
	playerID := c.Param("id")
	if playerID == "" {
		common.RespondError(c, http.StatusBadRequest, "Player ID is required")
		return
	}
	if c.GetString("role") != "admin" {
		common.RespondError(c, http.StatusForbidden, "You do not have permission to delete this player")
		return
	}

	err := deletePlayer(db, playerID)
	if err != nil {
		log.Printf("Error deleting player with ID %s: %v", playerID, err)
		common.RespondError(c, http.StatusInternalServerError, "Failed to delete player")
		return
	}

	log.Printf("User %s (%s) deleted player with ID: %s", c.GetString("userId"), c.GetString("email"), playerID)

	common.RespondMessage(c, "Player deleted successfully")
}
