package main

import (
	"context"
	"flag"
	"net/http"
	"strings"

	"github.com/zeromicro/go-zero/rest/httpx"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"

	"github.com/golang-jwt/jwt/v4/request"
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/rest"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/config"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
)

var configFile = flag.String("f", "cmd/discuss/api/etc/core.yaml", "the config file")

func main() {
	// 配置加载
	flag.Parse()
	var c config.Config
	conf.MustLoad(*configFile, &c)

	// 系统设置
	systemSetup(c)

	// 服务初始化
	opts := make([]rest.RunOption, 0)
	opts = append(opts, rest.WithCustomCors(func(header http.Header) {
		header.Set("Access-Control-Allow-Headers", "*")
	}, func(writer http.ResponseWriter) {

	}))
	opts = append(opts, rest.WithUnauthorizedCallback(func(w http.ResponseWriter, r *http.Request, err error) {
		if err != nil {
			logx.Debugf("unauthorized: %s \n", err.Error())
		}
	}))

	server := rest.MustNewServer(c.RestConf, opts...)
	defer server.Stop()
	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)

	// 数据迁移
	if c.Migrate {
		db.Migrate(ctx.DB)
	}

	// 服务启动
	logx.Infof("Starting server at %s:%d...  \n", c.Host, c.Port)
	server.Start()
}

func systemSetup(c config.Config) {
	// 初始化Domain里面的配置
	domain.ProjectName = c.Name

	// 默认的token头 Authorization 修改为 x-mmm-accesstoken
	request.AuthorizationHeaderExtractor = &request.PostExtractionFilter{
		Extractor: request.HeaderExtractor{"x-mmm-accesstoken"},
		Filter: func(tok string) (string, error) {
			// Should be a bearer token
			if len(tok) > 6 && strings.ToUpper(tok[0:7]) == "BEARER " {
				return tok[7:], nil
			}
			return tok, nil
		},
	}

	// 系统错误应答包装
	httpx.SetErrorHandlerCtx(func(ctx context.Context, err error) (int, any) {
		return http.StatusOK, result.Error(xerr.ServerCommonError, err.Error())
	})

	// 系统成功应答包装
	httpx.SetOkHandler(func(ctx context.Context, a any) any {
		return result.Success(a)
	})
}