package admin

import (
	"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 AdminHandler struct {
	repo *repository.Queries
	cfg  *config.AppConfig
}

func New(repo *repository.Queries, cfg *config.AppConfig) *AdminHandler {
	return &AdminHandler{
		repo,
		cfg,
	}
}

func (h *AdminHandler) RegisterRoutes(router chi.Router) {
	router.Route("/admin", func(r chi.Router) {
		authMiddleware := imiddleware.NewAuthMiddleware(h.cfg)
		adminMiddleware := imiddleware.NewAdminMiddleware(h.repo)
		r.Use(authMiddleware.Runner, adminMiddleware.Runner)

		r.Get("/api-services", h.GetApiServices)
		r.Get("/api-services/{id}", h.GetApiService)
		r.Post("/api-services", h.AddApiService)
		r.Patch("/api-services/{id}", h.RegenerateApiServiceSecret)
		r.Put("/api-services/{id}", h.UpdateApiService)
		r.Patch("/api-services/toggle/{id}", h.ToggleApiService)

		r.Get("/users", h.GetUsers)
		r.Post("/users", h.CreateUser)
		r.Get("/users/{id}", h.GetUser)
	})
}
