package fiber

import (
	cache "git.dmitriygnatenko.ru/dima/go-common/cache/ttl_memory_cache"
	"git.dmitriygnatenko.ru/dima/go-common/db"
	"github.com/gofiber/fiber/v2"

	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/repository"
	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/service/auth"
	"git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/service/config"
)

const (
	appName    = "dmitriygnatenko"
	metricsURI = "/metrics"
)

type ServiceProvider interface {
	ConfigService() *config.Service
	AuthService() *auth.Service
	CacheService() *cache.Cache
	TransactionManager() *db.TxManager
	ArticleRepository() *repository.ArticleRepository
	TagRepository() *repository.TagRepository
	ArticleTagRepository() *repository.ArticleTagRepository
	UserRepository() *repository.UserRepository
	LanguageRepository() *repository.LanguageRepository
}

func Init(sp ServiceProvider) (*fiber.App, error) {
	fiberApp := fiber.New(getConfig(sp))

	initStatic(fiberApp, sp)

	initMetrics(fiberApp, sp)

	initMiddleware(fiberApp, sp)

	initPublicHandlers(fiberApp, sp)

	initAdminHandlers(fiberApp, sp)

	return fiberApp, nil
}

func getConfig(sp ServiceProvider) fiber.Config {
	return fiber.Config{
		AppName:               appName,
		DisableStartupMessage: true,
		Views:                 getViewsEngine(sp),
		ErrorHandler:          getErrorHandler(),
	}
}
