package middleware

import (
	"git.ma-al.com/goc_marek/ps_shop/internal/prestashop/cookie"

	"github.com/labstack/echo/v4"
)

const sessionContextKey = "prestashop_session"

func SetSession(c echo.Context, session *cookie.SessionContext) {
	if session == nil {
		session = defaultSession()
	}
	c.Set(sessionContextKey, session)
}

func GetSession(c echo.Context) *cookie.SessionContext {
	if value := c.Get(sessionContextKey); value != nil {
		if session, ok := value.(*cookie.SessionContext); ok {
			if session != nil {
				return session
			}
		}
	}
	return defaultSession()
}

func defaultSession() *cookie.SessionContext {
	return &cookie.SessionContext{
		Values:      map[string]string{},
		ParseStatus: cookie.ParseStatusAnonymous,
	}
}
