package handlers

import (
	"log"
	"sno/internal/middleware"
	"sno/internal/service"

	"github.com/gofiber/fiber/v2"
)

// UserHandler handles HTTP requests for users.
type UserHandler struct {
	userService service.UserService
}

// NewUserHandler creates a new instance of a user handler.
func NewUserHandler(s service.UserService) *UserHandler {
	return &UserHandler{userService: s}
}

// GetMe handles the request to get the current user's profile.
// @Summary Get current user profile
// @Description Returns the current user's profile information including balance
// @Tags user
// @Accept json
// @Produce json
// @Success 200 {object} object{success=bool,message=string,data=models.User}
// @Failure 404 {object} object{success=bool,message=string}
// @Router /api/me [get]
// @Security ApiKeyAuth
func (h *UserHandler) GetMe(c *fiber.Ctx) error {
	// Get user data from auth middleware
	userData := middleware.GetTelegramUser(c)
	if userData == nil {
		return c.Status(fiber.StatusUnauthorized).JSON(Response{
			Success: false,
			Message: "User not authenticated",
		})
	}

	user, err := h.userService.GetUserProfile(c.Context(), userData.ID)
	if err != nil {
		log.Printf("ERROR: Failed to get user profile: %v", err)
		return c.Status(fiber.StatusNotFound).JSON(Response{
			Success: false,
			Message: "User not found",
		})
	}

	return c.Status(fiber.StatusOK).JSON(Response{
		Success: true,
		Message: "User profile retrieved successfully",
		Data:    user,
	})
}

// GetUserPurchases handles the request to get the user's purchase history.
// @Summary Get user purchase history
// @Description Returns the current user's purchase history
// @Tags user
// @Accept json
// @Produce json
// @Success 200 {object} object{success=bool,message=string,data=[]models.Purchase}
// @Failure 500 {object} object{success=bool,message=string}
// @Router /api/user/purchases [get]
// @Security ApiKeyAuth
func (h *UserHandler) GetUserPurchases(c *fiber.Ctx) error {
	// Get user data from auth middleware
	userData := middleware.GetTelegramUser(c)
	if userData == nil {
		return c.Status(fiber.StatusUnauthorized).JSON(Response{
			Success: false,
			Message: "User not authenticated",
		})
	}

	purchases, err := h.userService.GetUserPurchases(c.Context(), userData.ID)
	if err != nil {
		log.Printf("ERROR: Failed to get user purchases: %v", err)
		return c.Status(fiber.StatusInternalServerError).JSON(Response{
			Success: false,
			Message: "Failed to retrieve user purchases",
		})
	}

	return c.Status(fiber.StatusOK).JSON(Response{
		Success: true,
		Message: "User purchases retrieved successfully",
		Data:    purchases,
	})
}

// GetUserTransactions handles the request to get the user's transaction history.
// @Summary Get user transaction history
// @Description Returns the current user's transaction history (earned/spent stars)
// @Tags user
// @Accept json
// @Produce json
// @Success 200 {object} object{success=bool,message=string,data=[]models.Transaction}
// @Failure 500 {object} object{success=bool,message=string}
// @Router /api/user/transactions [get]
// @Security ApiKeyAuth
func (h *UserHandler) GetUserTransactions(c *fiber.Ctx) error {
	// Get user data from auth middleware
	userData := middleware.GetTelegramUser(c)
	if userData == nil {
		return c.Status(fiber.StatusUnauthorized).JSON(Response{
			Success: false,
			Message: "User not authenticated",
		})
	}

	transactions, err := h.userService.GetUserTransactions(c.Context(), userData.ID)
	if err != nil {
		log.Printf("ERROR: Failed to get user transactions: %v", err)
		return c.Status(fiber.StatusInternalServerError).JSON(Response{
			Success: false,
			Message: "Failed to retrieve user transactions",
		})
	}

	return c.Status(fiber.StatusOK).JSON(Response{
		Success: true,
		Message: "User transactions retrieved successfully",
		Data:    transactions,
	})
}
