// Code scaffolded by goctl. Safe to edit.
package auth

import (
	"net/http"
	"strings"

	logicauth "gateway/internal/logic/auth"
	"gateway/internal/response"
	"gateway/internal/svc"
)

func LogoutHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		ctx := logicauth.WithBearerAccessToken(HandlerContext(r.Context(), r), bearerFromHeader(r.Header.Get("Authorization")))
		l := logicauth.NewLogoutLogic(ctx, svcCtx)
		data, err := l.Logout()
		response.Write(ctx, w, data, err)
	}
}

func bearerFromHeader(header string) string {
	const prefix = "Bearer "
	if !strings.HasPrefix(header, prefix) {
		return ""
	}
	return strings.TrimSpace(strings.TrimPrefix(header, prefix))
}
