/*
 * Copyright © 2021 peizhaoyou <peizhaoyou@4paradigm.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main

import (
	"github.com/labstack/echo-contrib/prometheus"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"github.com/labstack/gommon/log"
	"github.com/4paradigm/openaios-platform/src/internal/auth"
	"github.com/4paradigm/openaios-platform/src/webterminal/server/apigen"
	"github.com/4paradigm/openaios-platform/src/webterminal/server/handler"
)

func main() {
	e := echo.New()
	e.Logger.SetHeader(`${time_rfc3339} ${level} ${short_file}:${line} `)
	e.Logger.SetLevel(log.INFO)

	e.Use(middleware.Logger())

	p := prometheus.NewPrometheus("echo", nil)
	p.Use(e)

	//authVerifier, err := auth.NewAuthVerifier()
	//if err != nil {
	//	e.Logger.Fatalf("auth handler initialize failed, err = " + err.Error())
	//	return
	//}

	h := handler.NewHandler()

	apiGroup := e.Group("/web-terminal")

	if err := auth.InitAuth(); err != nil {
		panic(err.Error())
	}
	apiGroup.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
		c.Set("bearerToken", key)
		idTokenClaim, err := auth.Verify(key)
		if err != nil {
			c.Logger().Warn("auth verify failed, err = " + err.Error())
			return false, err
		}
		c.Set("userName", idTokenClaim.PreferredUserName)
		c.Set("userID", idTokenClaim.Sub)
		return true, nil
	}))

	apigen.RegisterHandlers(apiGroup, &h)

	e.Logger.Info(e.Start("0.0.0.0:80"))

}
