package users

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

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

func RegisterUserRoutes(app *fiber.App) {
	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=/users/:uuid; name=getUser; method=GET; response=users.User
	app.Get("/users/:uuid", tockenService.Middleware(), userController.GetUser)

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

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

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

	// Typescript: TSEndpoint= path=/auth/me; name=me; method=GET; response=users.User
	app.Get("/auth/me", tockenService.Middleware(), userController.Me)
	roles.RegisterEndpoint("GET/auth/me", int(roles.UserPermission))

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

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

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

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

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

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