package types

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/session"
)

// LogtoSessionStorage
// @description: 实现Logto会话存储接口
type LogtoSessionStorage struct {
	Store *session.Store
	Ctx   *fiber.Ctx
}

// GetItem
// @description: 获取会话项
// @receiver storage
// @param key
// @return string
func (storage *LogtoSessionStorage) GetItem(key string) string {
	sess, err := storage.Store.Get(storage.Ctx)
	if err != nil {
		return ""
	}
	v := sess.Get(key)
	if v == nil {
		return ""
	}
	str, ok := v.(string)
	if !ok {
		return ""
	}
	return str
}

// SetItem
// @description: 设置会话项
// @receiver storage
// @param key
// @param value
func (storage *LogtoSessionStorage) SetItem(key, value string) {
	sess, err := storage.Store.Get(storage.Ctx)
	if err != nil {
		return
	}
	sess.Set(key, value)
	_ = sess.Save()
}
