package web

import (
	"context"
	"net/http"

	"party.at/party/cmd/party/common"
	"party.at/party/internal/data"
)

type Web struct {
	App *common.Application
}

type contextKey string

const userKey contextKey = "web_user"
const permissionsKey contextKey = "web_permissions"

func setUser(r *http.Request, user *data.User) *http.Request {
	return r.WithContext(context.WithValue(r.Context(), userKey, user))
}

func getUser(r *http.Request) *data.User {
	user, ok := r.Context().Value(userKey).(*data.User)
	if !ok {
		return data.AnonymousUser
	}
	return user
}

func setPermissions(r *http.Request, permissions data.Permissions) *http.Request {
	return r.WithContext(context.WithValue(r.Context(), permissionsKey, permissions))
}

func getPermissions(r *http.Request) data.Permissions {
	permissions, ok := r.Context().Value(permissionsKey).(data.Permissions)
	if !ok {
		return data.Permissions{}
	}
	return permissions
}
