//
// Copyright (C) 2025 veypi <i@veypi.com>
// 2025-03-04 16:08:06
// Distributed under terms of the MIT license.
//

package vbase

import (
	"embed"

	"github.com/veypi/vbase/api"
	"github.com/veypi/vbase/auth"
	"github.com/veypi/vbase/cfg"
	"github.com/veypi/vbase/models"
	"github.com/veypi/vhtml"
	vhtmlui "github.com/veypi/vhtml-ui"
	"github.com/veypi/vigo"
)

const Version = "v1.1.1"

var Router = vigo.NewRouter()
var (
	NewAuth = auth.Factory.New
	Config  = cfg.Global
)

func init() {
	Router.Extend("/api", api.Router)
	Router.Extend("v", vhtmlui.Router)
	Router.Extend("vhtml", vhtml.Router)
}

type Options = cfg.Options

func Init() error {
	// 初始化 VBaseProvider 并设置到全局 Auth
	cfg.Auth.SetProvider(NewAuth("vb"))
	// 初始化角色
	cfg.Auth.AddRole("admin", "管理员", "*:7")
	cfg.Auth.AddRole("user", "普通用户")
	return models.Migrate()
}

//go:embed ui
var uifs embed.FS

func init() {
	Router.Extend("/api", api.Router)
	Router.Extend("v", vhtmlui.Router)
	Router.Extend("vhtml", vhtml.Router)
	vhtml.WrapUI(Router, uifs)
}
