package oauth

import (
	"gitea.local/admin/hspguard/internal/cache"
	"gitea.local/admin/hspguard/internal/config"
	imiddleware "gitea.local/admin/hspguard/internal/middleware"
	"gitea.local/admin/hspguard/internal/repository"
	"github.com/go-chi/chi/v5"
)

type OAuthHandler struct {
	repo  *repository.Queries
	cache *cache.Client
	cfg   *config.AppConfig
}

func NewOAuthHandler(repo *repository.Queries, cache *cache.Client, cfg *config.AppConfig) *OAuthHandler {
	return &OAuthHandler{
		repo,
		cache,
		cfg,
	}
}

func (h *OAuthHandler) RegisterRoutes(router chi.Router) {
	router.Route("/oauth", func(r chi.Router) {
		r.Group(func(protected chi.Router) {
			authMiddleware := imiddleware.NewAuthMiddleware(h.cfg)
			protected.Use(authMiddleware.Runner)

			protected.Post("/code", h.getAuthCode)
		})
		r.Get("/authorize", h.AuthorizeClient)
		r.Post("/token", h.tokenEndpoint)
	})
}
