package api

import (
	"fmt"
	"net/http"
	"os"

	"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
	"github.com/gin-gonic/gin"
)

// https://auth.3dqueen.cloud/login/oauth/authorize?client_id=9f22d6616ae14fe59dc4&redirect_uri=https://www.3dqueen.cloud/box/v1/boxcost/callback&response_type=code&scope=openid&state=STATE
// 需要在第三方提供商配置回调 https://auth.3dqueen.cloud/callback
func callback(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	authConf := apictx.Svc.Conf.Auth
	pemByte, err := os.ReadFile(authConf.Certificate)
	if err != nil {
		return nil, err
	}
	casdoorsdk.InitConfig(authConf.Endpoint, authConf.ClientId, authConf.ClientSecret, string(pemByte), authConf.OrganizationName, authConf.ApplicationName)
	token, err := casdoorsdk.GetOAuthToken(c.Query("code"), c.Query("state"))
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(token.AccessToken)
	claims, err := casdoorsdk.ParseJwtToken(token.AccessToken)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(claims)
	c.Redirect(http.StatusFound, "https://auth.3dqueen.cloud")

	// claims.AccessToken = token.AccessToken
	return nil, nil
}
