package api

import (
	"encoding/json"
	"net/http"

	"dance-lessons-coach/pkg/user"

	"github.com/go-chi/chi/v5"
	"github.com/rs/zerolog/log"
)

// PasswordResetHandler handles password reset requests
type PasswordResetHandler struct {
	passwordResetService user.PasswordResetService
}

// NewPasswordResetHandler creates a new password reset handler
func NewPasswordResetHandler(passwordResetService user.PasswordResetService) *PasswordResetHandler {
	return &PasswordResetHandler{
		passwordResetService: passwordResetService,
	}
}

// RegisterRoutes registers password reset routes
func (h *PasswordResetHandler) RegisterRoutes(router chi.Router) {
	router.Post("/password-reset/request", h.handlePasswordResetRequest)
	router.Post("/password-reset/complete", h.handlePasswordResetComplete)
}

// PasswordResetRequest represents a password reset request

// handlePasswordResetRequest handles password reset requests
func (h *PasswordResetHandler) handlePasswordResetRequest(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()

	var req PasswordResetRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, `{"error":"invalid_request","message":"Invalid JSON request body"}`, http.StatusBadRequest)
		return
	}

	// Request password reset
	if err := h.passwordResetService.RequestPasswordReset(ctx, req.Username); err != nil {
		log.Error().Ctx(ctx).Err(err).Msg("Failed to request password reset")
		http.Error(w, `{"error":"server_error","message":"Failed to process password reset request"}`, http.StatusInternalServerError)
		return
	}

	// Return success
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(map[string]string{"message": "Password reset allowed, user can now reset password"})
}

// PasswordResetCompleteRequest represents a password reset completion request

// handlePasswordResetComplete handles password reset completion requests
func (h *PasswordResetHandler) handlePasswordResetComplete(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()

	var req PasswordResetCompleteRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, `{"error":"invalid_request","message":"Invalid JSON request body"}`, http.StatusBadRequest)
		return
	}

	// Complete password reset
	if err := h.passwordResetService.CompletePasswordReset(ctx, req.Username, req.NewPassword); err != nil {
		log.Error().Ctx(ctx).Err(err).Msg("Failed to complete password reset")
		http.Error(w, `{"error":"server_error","message":"Failed to complete password reset"}`, http.StatusInternalServerError)
		return
	}

	// Return success
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(map[string]string{"message": "Password reset completed successfully"})
}
