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

package oauth

import (
	"github.com/veypi/vbase/api/oauth/providers"
	"github.com/veypi/vbase/cfg"
	"github.com/veypi/vigo"
)

var Router = vigo.NewRouter()

func init() {
	// === OAuth 公开端点（跳过认证）===
	Router.Get("/authorize", "授权端点", vigo.SkipBefore, authorize)
	Router.Post("/token", "令牌端点", vigo.SkipBefore, token)
	Router.Post("/revoke", "撤销令牌", vigo.SkipBefore, revoke)
	Router.Get("/userinfo", "用户信息(OIDC)", userInfo)
	Router.Get("/.well-known/openid-configuration", "OIDC发现文档", vigo.SkipBefore, discovery)

	// === OAuth 客户端管理（需要认证）===
	clientRouter := Router.SubRouter("/clients")
	clientRouter.Get("/", "OAuth客户端列表", cfg.Auth.RequireRead("oauth-client:*"), listClients)
	clientRouter.Post("/", "创建OAuth客户端", cfg.Auth.RequireCreate("oauth-client"), createClient)
	clientRouter.Get("/{client_id}", "获取客户端详情", cfg.Auth.RequireRead("oauth-client:*"), getClient)
	clientRouter.Patch("/{client_id}", "更新OAuth客户端", cfg.Auth.RequireWrite("oauth-client:*"), updateClient)
	clientRouter.Delete("/{client_id}", "删除OAuth客户端", cfg.Auth.RequireWrite("oauth-client:*"), deleteClient)

	// === OAuth 提供商管理 ===
	Router.Extend("/providers", providers.Router)
}
