package service

import (
	"fmt"

	"bsm/full/all/internal/config"
	"bsm/full/all/internal/server"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
)

type exposeFunc func(*server.Server) error

var services = []struct {
	name   string
	expose exposeFunc
}{
	{"ads", exposeAds},
	{"cloud", exposeCloud},
	{"cms", exposeCMS},
	{"feedback", exposeFeedback},
	{"fts", exposeFTS},
	{"initial", exposeInitial},
	{"logs", exposeLogs},
	{"mgt", exposeMgt},
	{"passport", exposePassport},
	{"sender", exposeSender},
	{"address", exposeAddress},
	{"mall", exposeMall},
	{"market", exposeMarket},
	{"order", exposeOrder},
	{"wallet", exposeWallet},
}

func Expose(srv *server.Server) error {
	srv.HTTP.Use(sessions.Sessions("mysession", cookie.NewStore([]byte(config.Spec.SecretKey+"-session"))))
	for _, item := range services {
		if !config.Enabled(item.name) {
			continue
		}
		if err := item.expose(srv); err != nil {
			return fmt.Errorf("expose %s: %w", item.name, err)
		}
	}
	return nil
}
