package users

import (
	"fmt"
	"server/internal/middleware"
	"server/internal/tokens"
	"time"

	"github.com/gofiber/fiber/v3"
	"github.com/gofiber/fiber/v3/middleware/limiter"
)

func RegisterUserRoutes(app fiber.Router) {
	tockenService, err := tokens.GetTockenService()
	if err != nil {
		panic(fmt.Sprintf("token service: %v", err))
	}

	authRateLimiter := limiter.New(limiter.Config{
		Max:               10,
		Expiration:        time.Minute,
		LimiterMiddleware: limiter.SlidingWindow{},
	})

	userController := NewUserController(tockenService)

	// Typescript: TSEndpoint= path=/api/users/:uuid; name=getUser; method=GET; response=users.User
	app.Get("/users/:uuid", userController.GetUser)

	// Typescript: TSEndpoint= path=/api/users; name=createUser; method=POST; request=users.UserCreateRequest; response=users.User
	app.Post("/users", userController.CreateUser)

	// Typescript: TSEndpoint= path=/api/users/update; name=updateUser; method=PUT; request=users.UpdateUserRequest; response=users.User
	app.Put("/users/update", userController.UpdateUser)

	// Typescript: TSEndpoint= path=/api/users/:uuid; name=deleteUser; method=DELETE; response=responses.SimpleResponse
	app.Delete("/users/:uuid", userController.DeleteUser)

	// Typescript: TSEndpoint= path=/api/auth/me; name=me; method=GET; response=users.User
	app.Get("/auth/me", middleware.AuthMe, userController.Me)

	// Typescript: TSEndpoint= path=/api/auth/login; name=login; method=POST; request=users.LoginRequest; response=tokens.TokenPair
	app.Post("/auth/login", authRateLimiter, userController.Login)

	// Typescript: TSEndpoint= path=/api/auth/refresh; name=refresh; method=POST; request=users.RefreshRequest; response=tokens.TokenPair
	app.Post("/auth/refresh", authRateLimiter, userController.Refresh)

	// Typescript: TSEndpoint= path=/api/auth/register; name=register; method=POST; request=users.UserCreateRequest; response=users.User
	app.Post("/auth/register", authRateLimiter, userController.Register)

	// Typescript: TSEndpoint= path=/api/auth/password/forgot; name=forgotPassword; method=POST; request=users.ForgotPasswordRequest; response=responses.SimpleResponse
	app.Post("/auth/password/forgot", authRateLimiter, userController.ForgotPassword)

	// Typescript: TSEndpoint= path=/api/auth/password/reset; name=resetPassword; method=POST; request=users.ResetPasswordRequest; response=responses.SimpleResponse
	app.Post("/auth/password/reset", authRateLimiter, userController.ResetPassword)

	// Typescript: TSEndpoint= path=/api/auth/password/valid; name=validToken; method=POST; request=string; response=responses.SimpleResponse
	app.Post("/auth/password/valid", authRateLimiter, userController.ValidToken)

	// Typescript: TSEndpoint= path=/api/auth/password/update; name=updatePassword; method=PUT; request=users.UpdatePasswordRequest; response=responses.SimpleResponse
	app.Put("/auth/password/update", authRateLimiter, userController.UpdatePassword)
}
