package handler

import (
	"net/http"
	"time"

	"git.ekzyis.com/ekzyis/delphi.market/db"
	"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
	"github.com/labstack/echo/v4"
)

func HandleLogout(sc context.ServerContext) echo.HandlerFunc {
	return func(c echo.Context) error {
		var (
			cookie    *http.Cookie
			sessionId string
			err       error
		)
		if cookie, err = c.Cookie("session"); err != nil {
			// cookie not found
			return c.JSON(http.StatusNotFound, map[string]string{"reason": "session not found"})
		}
		sessionId = cookie.Value
		if err = sc.Db.DeleteSession(&db.Session{SessionId: sessionId}); err != nil {
			return err
		}
		// tell browser that cookie is expired and thus can be deleted
		c.SetCookie(&http.Cookie{Name: "session", HttpOnly: true, Path: "/", Value: sessionId, Secure: true, Expires: time.Now()})
		return c.JSON(http.StatusSeeOther, map[string]string{"status": "OK"})
	}
}