package routers

import (
	"github.com/go-chi/chi/v5"
	"github.com/go-chi/jwtauth/v5"
	"github.com/tech/sendico/pkg/auth"
	"github.com/tech/sendico/pkg/db/account"
	"github.com/tech/sendico/pkg/db/refreshtokens"
	"github.com/tech/sendico/pkg/mlogger"
	"github.com/tech/sendico/pkg/mservice"
	"github.com/tech/sendico/server/interface/middleware"
	re "github.com/tech/sendico/server/internal/api/routers/endpoint"
)

type AuthorizedRouter struct {
	logger  mlogger.Logger
	db      account.DB
	rtdb    refreshtokens.DB
	imp     *re.HttpEndpointRouter
	service mservice.Type
}

func NewRouter(logger mlogger.Logger, apiEndpoint string, router chi.Router, db account.DB, rtdb refreshtokens.DB, enforcer auth.Enforcer, config *middleware.TokenConfig, signature *middleware.Signature) *AuthorizedRouter {
	ja := jwtauth.New(signature.Algorithm, signature.PrivateKey, signature.PublicKey)
	router.Use(jwtauth.Verifier(ja))
	router.Use(jwtauth.Authenticator(ja))
	l := logger.Named("authorized")
	ar := AuthorizedRouter{
		logger:  l,
		db:      db,
		rtdb:    rtdb,
		imp:     re.NewHttpEndpointRouter(l, apiEndpoint, router, config, signature),
		service: mservice.Accounts,
	}

	return &ar
}
