package handlers

import (
	"Yimaru-Backend/internal/domain"
	"Yimaru-Backend/internal/web_server/response"
	"fmt"
	"time"

	"github.com/gofiber/fiber/v2"
	"go.uber.org/zap"
)

func (h *Handler) CreateReferralCode(c *fiber.Ctx) error {

	companyID := c.Locals("company_id").(domain.ValidInt64)
	if !companyID.Valid {
		h.BadRequestLogger().Error("invalid company id")
		return fiber.NewError(fiber.StatusBadRequest, "invalid company id")
	}
	userID, ok := c.Locals("user_id").(int64)
	if !ok || userID == 0 {
		h.mongoLoggerSvc.Info("Invalid user ID in context",
			zap.Int("status_code", fiber.StatusInternalServerError),
			zap.Int64("userID", userID),
			zap.Time("timestamp", time.Now()),
		)
		return fiber.NewError(fiber.StatusInternalServerError, "Invalid user identification")
	}
	// referralCode, err := h.referralSvc.CreateReferralCode(c.Context(), userID, companyID.Value)

	// if err != nil {
	// 	h.mongoLoggerSvc.Error("Failed to create referral",
	// 		zap.Int64("userID", userID),
	// 		zap.Int("status_code", fiber.StatusInternalServerError),
	// 		zap.Error(err),
	// 		zap.Time("timestamp", time.Now()),
	// 	)
	// 	return fiber.NewError(fiber.StatusInternalServerError, "Failed to create referral")
	// }

	fmt.Printf("Successfully created referral!")

	// res := domain.ConvertReferralCodeRes(referralCode)

	return response.WriteJSON(c, fiber.StatusOK, "Referral created successfully", nil, nil)
}

func (h *Handler) GetReferralCode(c *fiber.Ctx) error {
	companyID := c.Locals("company_id").(domain.ValidInt64)
	if !companyID.Valid {
		h.BadRequestLogger().Error("invalid company id")
		return fiber.NewError(fiber.StatusBadRequest, "invalid company id")
	}
	userID, ok := c.Locals("user_id").(int64)
	if !ok || userID == 0 {
		h.mongoLoggerSvc.Error("Invalid user ID in context",
			zap.Int64("userID", userID),
			zap.Int("status_code", fiber.StatusInternalServerError),
			zap.Time("timestamp", time.Now()),
		)
		return fiber.NewError(fiber.StatusInternalServerError, "Invalid user id")
	}

	_, err := h.userSvc.GetUserByID(c.Context(), userID)
	if err != nil {
		h.mongoLoggerSvc.Error("Failed to get user",
			zap.Int64("userID", userID),
			zap.Int("status_code", fiber.StatusInternalServerError),
			zap.Error(err),
			zap.Time("timestamp", time.Now()),
		)
		return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve user")
	}

	// if !user.OrganizationID.Valid || user.OrganizationID.Value != companyID.Value {
	// 	h.mongoLoggerSvc.Warn("User attempt to login to different company",
	// 		zap.Int64("userID", userID),
	// 		zap.Int("status_code", fiber.StatusInternalServerError),
	// 		zap.Error(err),
	// 		zap.Time("timestamp", time.Now()),
	// 	)
	// 	return fiber.NewError(fiber.StatusBadRequest, "Failed to retrieve user")
	// }

	// referrals, err := h.referralSvc.GetReferralCodesByUser(c.Context(), user.ID)

	// if err != nil {
	// 	h.mongoLoggerSvc.Error("Failed to get user referrals",
	// 		zap.Int64("userID", userID),
	// 		zap.Int("status_code", fiber.StatusInternalServerError),
	// 		zap.Error(err),
	// 		zap.Time("timestamp", time.Now()),
	// 	)
	// 	return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve user referral codes")
	// }

	// result := domain.ConvertReferralCodeResList(referrals)

	return response.WriteJSON(c, fiber.StatusOK, "Referral Code Fetched Successfully", nil, nil)

}

// // UpdateReferralSettings godoc
// // @Summary Update referral settings
// // @Description Updates referral settings (admin only)
// // @Tags referral
// // @Accept json
// // @Produce json
// // @Param settings body domain.ReferralSettings true "Referral settings"
// // @Success 200 {object} response.APIResponse
// // @Failure 401 {object} response.APIResponse
// // @Failure 403 {object} response.APIResponse
// // @Failure 500 {object} response.APIResponse
// // @Security Bearer
// // @Router /api/v1/referral/settings [put]
// func (h *Handler) UpdateReferralSettings(c *fiber.Ctx) error {
// 	userID, ok := c.Locals("user_id").(int64)
// 	if !ok || userID == 0 {
// 		h.logger.Error("Invalid user ID in context")
// 		h.mongoLoggerSvc.Error("Failed to delete user",
// 			zap.Int64("userID", userID),
// 			zap.Int("status_code", fiber.StatusInternalServerError),
// 			zap.Time("timestamp", time.Now()),
// 		)
// 		return fiber.NewError(fiber.StatusInternalServerError, "Invalid user id")
// 	}

// 	user, err := h.userSvc.GetUserByID(c.Context(), userID)
// 	if err != nil {
// 		h.mongoLoggerSvc.Error("Failed to get user",
// 			zap.Int64("userID", userID),
// 			zap.Int("status_code", fiber.StatusInternalServerError),
// 			zap.Error(err),
// 			zap.Time("timestamp", time.Now()),
// 		)
// 		return fiber.NewError(fiber.StatusInternalServerError, err.Error())
// 	}

// 	if user.Role != domain.RoleAdmin {
// 		h.mongoLoggerSvc.Error("Access Forbidden",
// 			zap.Int64("userID", userID),
// 			zap.Int("status_code", fiber.StatusForbidden),
// 			zap.Error(err),
// 			zap.Time("timestamp", time.Now()),
// 		)
// 		return fiber.NewError(fiber.StatusForbidden, "Admin access required")
// 	}

// 	var settings domain.ReferralSettings
// 	if err := c.BodyParser(&settings); err != nil {
// 		h.mongoLoggerSvc.Info("Failed to parse settings",
// 			zap.Int64("userID", userID),
// 			zap.Int("status_code", fiber.StatusBadRequest),
// 			zap.Error(err),
// 			zap.Time("timestamp", time.Now()),
// 		)
// 		return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
// 	}

// 	settings.UpdatedBy = user.PhoneNumber
// 	if err := h.referralSvc.UpdateReferralSettings(c.Context(), &settings); err != nil {
// 		h.mongoLoggerSvc.Error("Failed to update referral settings",
// 			zap.Int64("userID", userID),
// 			zap.Int("status_code", fiber.StatusInternalServerError),
// 			zap.Error(err),
// 			zap.Time("timestamp", time.Now()),
// 		)
// 		return fiber.NewError(fiber.StatusInternalServerError, err.Error())
// 	}

// 	return response.WriteJSON(c, fiber.StatusOK, "Referral settings updated successfully", nil, nil)
// }

// // GetReferralSettings godoc
// // @Summary Get referral settings
// // @Description Retrieves current referral settings (admin only)
// // @Tags referral
// // @Accept json
// // @Produce json
// // @Success 200 {object} domain.ReferralSettings
// // @Failure 401 {object} response.APIResponse
// // @Failure 403 {object} response.APIResponse
// // @Failure 500 {object} response.APIResponse
// // @Security Bearer
// // @Router /api/v1/referral/settings [get]
// func (h *Handler) GetReferralSettings(c *fiber.Ctx) error {
// 	// userID, ok := c.Locals("user_id").(int64)
// 	// if !ok || userID == 0 {
// 	// 	h.logger.Error("Invalid user ID in context")
// 	// 	return fiber.NewError(fiber.StatusUnauthorized, "Invalid user identification")
// 	// }
// 	settings, err := h.referralSvc.GetReferralSettings(c.Context())
// 	if err != nil {
// 		h.mongoLoggerSvc.Error("Failed to get referral settings",
// 			zap.Int("status_code", fiber.StatusInternalServerError),
// 			zap.Error(err),
// 			zap.Time("timestamp", time.Now()),
// 		)
// 		return fiber.NewError(fiber.StatusInternalServerError, err.Error())
// 	}

// 	return response.WriteJSON(c, fiber.StatusOK, "Referral settings retrieved successfully", settings, nil)
// }
