package router

import (
	"hyapp-admin-server/internal/config"
	"hyapp-admin-server/internal/middleware"
	"hyapp-admin-server/internal/modules/adminuser"
	"hyapp-admin-server/internal/modules/appconfig"
	"hyapp-admin-server/internal/modules/appregistry"
	"hyapp-admin-server/internal/modules/appuser"
	"hyapp-admin-server/internal/modules/audit"
	authroutes "hyapp-admin-server/internal/modules/auth"
	"hyapp-admin-server/internal/modules/coinledger"
	"hyapp-admin-server/internal/modules/countryregion"
	"hyapp-admin-server/internal/modules/dailytask"
	"hyapp-admin-server/internal/modules/dashboard"
	gamemanagement "hyapp-admin-server/internal/modules/gamemanagement"
	"hyapp-admin-server/internal/modules/health"
	"hyapp-admin-server/internal/modules/hostorg"
	"hyapp-admin-server/internal/modules/job"
	"hyapp-admin-server/internal/modules/menu"
	"hyapp-admin-server/internal/modules/notification"
	"hyapp-admin-server/internal/modules/payment"
	"hyapp-admin-server/internal/modules/rbac"
	"hyapp-admin-server/internal/modules/registrationreward"
	resourcemodule "hyapp-admin-server/internal/modules/resource"
	"hyapp-admin-server/internal/modules/roomadmin"
	"hyapp-admin-server/internal/modules/search"
	"hyapp-admin-server/internal/modules/upload"
	"hyapp-admin-server/internal/platform/logging"
	"hyapp-admin-server/internal/service"

	"github.com/gin-gonic/gin"
)

type Handlers struct {
	AdminUser          *adminuser.Handler
	AppConfig          *appconfig.Handler
	AppRegistry        *appregistry.Handler
	AppUser            *appuser.Handler
	Audit              *audit.Handler
	Auth               *authroutes.Handler
	CoinLedger         *coinledger.Handler
	CountryRegion      *countryregion.Handler
	DailyTask          *dailytask.Handler
	Dashboard          *dashboard.Handler
	Game               *gamemanagement.Handler
	Health             *health.Handler
	HostOrg            *hostorg.Handler
	Job                *job.Handler
	Menu               *menu.Handler
	Notification       *notification.Handler
	Payment            *payment.Handler
	RBAC               *rbac.Handler
	RegistrationReward *registrationreward.Handler
	Resource           *resourcemodule.Handler
	RoomAdmin          *roomadmin.Handler
	Search             *search.Handler
	Upload             *upload.Handler
}

func New(cfg config.Config, auth *service.AuthService, h Handlers) *gin.Engine {
	engine := gin.New()
	engine.Use(middleware.RequestID(), middleware.AppCode(), logging.GinAccessLogger(), gin.Recovery(), middleware.CORS(cfg))

	engine.GET("/healthz", h.Health.Health)
	engine.GET("/readyz", h.Health.Ready)

	api := engine.Group("/api/v1")
	protected := api.Group("")
	protected.Use(middleware.AuthRequired(auth), middleware.Audit(h.Audit))

	authroutes.RegisterRoutes(api, protected, h.Auth)
	coinledger.RegisterRoutes(protected, h.CoinLedger)
	menu.RegisterRoutes(protected, h.Menu)
	rbac.RegisterRoutes(protected, h.RBAC)
	resourcemodule.RegisterRoutes(protected, h.Resource)
	adminuser.RegisterRoutes(protected, h.AdminUser)
	appuser.RegisterRoutes(protected, h.AppUser)
	appregistry.RegisterRoutes(protected, h.AppRegistry)
	appconfig.RegisterRoutes(protected, h.AppConfig)
	countryregion.RegisterRoutes(protected, h.CountryRegion)
	dailytask.RegisterRoutes(protected, h.DailyTask)
	registrationreward.RegisterRoutes(protected, h.RegistrationReward)
	gamemanagement.RegisterRoutes(protected, h.Game)
	roomadmin.RegisterRoutes(protected, h.RoomAdmin)
	dashboard.RegisterRoutes(protected, h.Dashboard)
	hostorg.RegisterRoutes(protected, h.HostOrg)
	audit.RegisterRoutes(protected, h.Audit)
	notification.RegisterRoutes(protected, h.Notification)
	payment.RegisterRoutes(protected, h.Payment)
	job.RegisterRoutes(protected, h.Job)
	upload.RegisterRoutes(protected, h.Upload)
	search.RegisterRoutes(protected, h.Search)

	return engine
}
