package main

import (
	"main/api_discovery"
	"main/context"
	"main/grpc"
	"main/ipc/protos"
	"main/log"
	"main/utils"
)

func OnPreRequest() string {
	context.Clear()
	return ""
}

func OnRequestShutdownReporting(method, route, routeParsed string, statusCode int, user, ip, rateLimitGroup string, apiSpec *protos.APISpec, rateLimited bool) {
	if method == "" || route == "" || statusCode == 0 {
		return
	}

	log.Info("[RSHUTDOWN] Got request metadata: ", method, " ", route, " ", statusCode)

	if !rateLimited && !utils.ShouldDiscoverRoute(statusCode, route, method) {
		return
	}

	log.Info("[RSHUTDOWN] Got API spec: ", apiSpec)
	grpc.OnRequestShutdown(method, route, routeParsed, statusCode, user, ip, rateLimitGroup, apiSpec, rateLimited)
}

func OnPostRequest() string {
	go OnRequestShutdownReporting(context.GetMethod(), context.GetRoute(), context.GetParsedRoute(), context.GetStatusCode(), context.GetUserId(), context.GetIp(), context.GetRateLimitGroup(), api_discovery.GetApiInfo(), context.IsEndpointRateLimited())
	context.Clear()
	return ""
}
