package session

import (
	"errors"
	"github.com/gorilla/sessions"
	"net/http"
	"status/storage"
)

const cookieName = "status"

type Session struct {
	Store   *sessions.CookieStore
	Storage *storage.Storage
}

func New(key string, storage *storage.Storage) *Session {
	store := sessions.NewCookieStore([]byte(key))
	store.Options = &sessions.Options{
		HttpOnly: true,
		Secure:   true,
		MaxAge:   86400 * 30,
	}
	return &Session{
		Store:   store,
		Storage: storage,
	}
}

func (s *Session) Delete(w http.ResponseWriter, r *http.Request) error {
	session, err := s.Store.Get(r, cookieName)
	if err != nil {
		return err
	}
	session.Options.MaxAge = -1
	err = session.Save(r, w)
	return err
}

func (s *Session) Save(r *http.Request, w http.ResponseWriter, name string) error {
	session, _ := s.Store.Get(r, cookieName)
	session.Values["name"] = name
	return session.Save(r, w)
}

func (s *Session) Get(r *http.Request) (string, error) {
	session, err := s.Store.Get(r, cookieName)
	if err != nil {
		return "", err
	}
	name, ok := session.Values["name"].(string)
	if name == "" || !ok {
		return "", errors.New("error extracting session")
	}
	u, err := s.Storage.UserByName(name)
	if err != nil {
		return "", errors.New("error getting user")
	}
	if !u.Active {
		return "", errors.New("user not active")
	}
	return name, nil
}
