package requestclient

import (
	"encoding/json"
	"jannex/admin-dashboard-backend/modules/config"
	"jannex/admin-dashboard-backend/modules/logger"
	"jannex/admin-dashboard-backend/modules/structs"
	"strconv"

	"git.ex.umbach.dev/Alex/roese-utils/rslogger"
	"github.com/gofiber/fiber/v2"
	"github.com/rs/zerolog/log"
)

func InvexApiRequestClient(requestMethod string, url string) (statusCode int, body []byte, err error) {
	a := fiber.AcquireAgent()

	a.Add("Authorization", "Token "+config.Cfg.InvexAPI.Token)

	req := a.Request()
	req.Header.SetMethod(requestMethod)
	req.SetRequestURI(url)

	if err := a.Parse(); err != nil {
		log.Error().Msgf("Failed to parse request, err: %s", err)
		return 0, nil, err
	}

	code, body, _ := a.Bytes()

	if code == 401 {
		log.Error().Msgf("invex not authorized, code: %d", code)

		logger.AddSystemLog(rslogger.LogTypeError, "InvexApiRequestClient err: %s statusCode: %s",
			"invex not authorized", strconv.Itoa(code))

		return code, nil, err
	} else if code == 404 {
		log.Error().Msgf("Invex stock item not found, code: %d", code)

		logger.AddSystemLog(rslogger.LogTypeWarning, "InvexApiRequestClient err: %s statusCode: %s",
			"invex stock item not found", strconv.Itoa(code))

		return code, nil, err
	} else if code != 200 {
		log.Error().Msgf("InvexApiRequestClient request failed, statusCode: %s", strconv.Itoa(code))

		logger.AddSystemLog(rslogger.LogTypeError, "InvexApiRequestClient request failed, statusCode: %s", strconv.Itoa(code))

		return code, nil, err
	}

	return code, body, nil
}

func TelegramBotManagerRequestClient(telegramBotManagerRequestBody structs.TelegramBotManagerRequestBody) {
	if !config.Cfg.TelegramBotManagerEnabled {
		return
	}

	a := fiber.AcquireAgent()

	req := a.Request()
	req.Header.SetMethod(fiber.MethodPost)
	req.SetRequestURI(config.Cfg.TelegramBotManagerServerUrl + "/v1/notification")
	req.Header.SetContentType("application/json")

	reqestBodyBytes, err := json.Marshal(telegramBotManagerRequestBody)

	if err != nil {
		log.Error().Msgf("Failed to marshal request body, err: %s", err)
		logger.AddSystemLog(rslogger.LogTypeError, "TelegramBotManagerRequestClient failed to marshal request body, err: %s", err.Error())
		return
	}

	req.SetBody(reqestBodyBytes)

	if err = a.Parse(); err != nil {
		log.Error().Msgf("Failed to parse request, err: %s", err)
		logger.AddSystemLog(rslogger.LogTypeError, "TelegramBotManagerRequestClient failed to parse request, err: %s", err.Error())
		return
	}

	code, _, _ := a.Bytes()

	if code != 200 {
		logger.AddSystemLog(rslogger.LogTypeError, "TelegramBotManagerRequestClient err: %s statusCode: %s", "failed to send notification", strconv.Itoa(code))
	}
}

func TelegramBotManagerDeleteUserRequestClient(userId string) {
	if !config.Cfg.TelegramBotManagerEnabled {
		return
	}

	a := fiber.AcquireAgent()

	req := a.Request()
	req.Header.SetMethod(fiber.MethodDelete)
	req.SetRequestURI(config.Cfg.TelegramBotManagerServerUrl + "/v1/verifycode/" + userId)

	if err := a.Parse(); err != nil {
		log.Error().Msgf("Failed to parse request, err: %s", err)
		logger.AddSystemLog(rslogger.LogTypeError, "TelegramBotManagerRequestClient failed to parse request, err: %s", err.Error())
		return
	}

	code, _, _ := a.Bytes()

	if code != 200 {
		logger.AddSystemLog(rslogger.LogTypeError, "TelegramBotManagerRequestClient err: %s statusCode: %s", "failed to send notification", strconv.Itoa(code))
	}
}
