package admin

import (
	"time"

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

type logoutPage struct {
	configService ConfigService
}

func NewLogoutPageHandler(
	configService ConfigService,
) fiber.Handler {
	h := logoutPage{
		configService: configService,
	}

	return h.handler()
}

func (h logoutPage) handler() fiber.Handler {
	return func(fctx *fiber.Ctx) error {
		cookie := new(fiber.Cookie)
		cookie.Name = h.configService.JWTCookie()
		cookie.Expires = time.Now().Add(-1 * time.Second)
		fctx.Cookie(cookie)

		return fctx.Redirect("/admin/login")
	}
}
