package handlers

import (
	"net/http"
	"strconv"

	"veza-backend-api/internal/services"

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

// GetPlaylistVersions récupère l'historique des versions d'une playlist
// T0509: Create Playlist Version History
func GetPlaylistVersions(versionService *services.PlaylistVersionService) gin.HandlerFunc {
	return func(c *gin.Context) {
		playlistID, err := strconv.ParseInt(c.Param("id"), 10, 64)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid playlist ID"})
			return
		}

		// Pagination
		limit := 20
		offset := 0
		if limitStr := c.Query("limit"); limitStr != "" {
			if parsedLimit, err := strconv.Atoi(limitStr); err == nil && parsedLimit > 0 && parsedLimit <= 100 {
				limit = parsedLimit
			}
		}
		if offsetStr := c.Query("offset"); offsetStr != "" {
			if parsedOffset, err := strconv.Atoi(offsetStr); err == nil && parsedOffset >= 0 {
				offset = parsedOffset
			}
		}

		versions, total, err := versionService.GetVersions(c.Request.Context(), playlistID, limit, offset)
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"versions": versions,
			"total":    total,
			"limit":    limit,
			"offset":   offset,
		})
	}
}

// GetPlaylistVersion récupère une version spécifique
// T0509: Create Playlist Version History
func GetPlaylistVersion(versionService *services.PlaylistVersionService) gin.HandlerFunc {
	return func(c *gin.Context) {
		playlistID, err := strconv.ParseInt(c.Param("id"), 10, 64)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid playlist ID"})
			return
		}

		versionStr := c.Param("version")
		version, err := strconv.Atoi(versionStr)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid version number"})
			return
		}

		playlistVersion, err := versionService.GetVersion(c.Request.Context(), playlistID, version)
		if err != nil {
			if err.Error() == "version not found" {
				c.JSON(http.StatusNotFound, gin.H{"error": "Version not found"})
				return
			}
			c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
			return
		}

		c.JSON(http.StatusOK, playlistVersion)
	}
}

// RestorePlaylistVersion restaure une playlist à une version spécifique
// T0509: Create Playlist Version History
func RestorePlaylistVersion(versionService *services.PlaylistVersionService) gin.HandlerFunc {
	return func(c *gin.Context) {
		userID, exists := c.Get("user_id")
		if !exists {
			c.JSON(http.StatusUnauthorized, gin.H{"error": "User not authenticated"})
			return
		}

		playlistID, err := strconv.ParseInt(c.Param("id"), 10, 64)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid playlist ID"})
			return
		}

		versionStr := c.Param("version")
		version, err := strconv.Atoi(versionStr)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid version number"})
			return
		}

		userIDInt64 := int64(userID.(int))
		restoredVersion, err := versionService.RestoreVersion(c.Request.Context(), playlistID, userIDInt64, version)
		if err != nil {
			if err.Error() == "version not found" {
				c.JSON(http.StatusNotFound, gin.H{"error": "Version not found"})
				return
			}
			if err.Error() == "playlist not found" {
				c.JSON(http.StatusNotFound, gin.H{"error": "Playlist not found"})
				return
			}
			c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"message": "Playlist restored successfully",
			"version": restoredVersion,
		})
	}
}

