package config

import (
	"SafelineAPI/internal/app/logger"
	"SafelineAPI/pkg/utils"
)

type SafeLine struct {
	Host     `json:"Host"`
	ApiToken `json:"ApiToken"`
}
type ApiToken string

func (apiToken ApiToken) GetApiToken() (string, string) {
	return "X-SLCE-API-TOKEN", apiToken.String()
}

func (apiToken ApiToken) String() string {
	return string(apiToken)
}

func (apiToken ApiToken) Verify() bool {
	if apiToken.String() == "" {
		logger.Warning.Printf("No %sSafeLine API token%s set: please check the %sSafeLine.ApiToken%s parameter", logger.Cyan, logger.Reset, logger.Yellow, logger.Reset)
		return true
	}
	return false
}

func (safeLine SafeLine) Verify() bool {
	a := safeLine.ApiToken.Verify()
	b := safeLine.Host.Verify()
	if a || b {
		return true
	}

	NoLoginAuthTokenResp, NoLoginStatusCode, NoLoginErr := utils.AuthSafeLine(*safeLine.Host.Url())
	if NoLoginErr != nil {
		logger.Error.Printf("Error requesting server: %s%s%s", logger.Red, NoLoginErr.Error(), logger.Reset)
		return true
	}
	LoginAuthTokenResp, LoginStatusCode, LoginErr := utils.VerifyAuthToken(*safeLine.Host.Url(), safeLine.ApiToken.String())
	if LoginErr != nil {
		logger.Error.Printf("Error validating %sSafeLine API token%s: %s%s%s", logger.Cyan, logger.Reset, logger.Red, LoginErr.Error(), logger.Reset)
		return true
	}

	if !(NoLoginAuthTokenResp.Err == "login-required" && NoLoginStatusCode == 401) {
		logger.Warning.Printf("Server endpoint %s/open/auth/token%s returned error: please check the %sSafeLine.Host%s configuration", logger.Cyan, logger.Reset, logger.Yellow, logger.Reset)
		return true
	}

	if LoginAuthTokenResp.Err == "login-required" && LoginStatusCode == 401 {
		logger.Warning.Printf("%sSafeLine API token%s is invalid: please check and retry", logger.Cyan, logger.Reset)
		return true
	}

	logger.Success.Printf("%sSafeLine%s configuration check completed!", logger.Cyan, logger.Reset)
	return false
}

func (apiToken ApiToken) VerifyCommand() bool {
	if apiToken.String() == "" {
		logger.Warning.Printf("No %sSafeLine API token%s set: please check the %s-t%s command-line argument", logger.Cyan, logger.Reset, logger.Yellow, logger.Reset)
		return true
	}
	return false
}

func (safeLine SafeLine) VerifyCommand() bool {
	a := safeLine.ApiToken.VerifyCommand()
	b := safeLine.Host.VerifyCommand()
	if a || b {
		return true
	}

	NoLoginAuthTokenResp, NoLoginStatusCode, NoLoginErr := utils.AuthSafeLine(*safeLine.Host.Url())
	if NoLoginErr != nil {
		logger.Error.Printf("Error requesting server: %s%s%s", logger.Red, NoLoginErr.Error(), logger.Reset)
		return true
	}
	LoginAuthTokenResp, LoginStatusCode, LoginErr := utils.VerifyAuthToken(*safeLine.Host.Url(), safeLine.ApiToken.String())
	if LoginErr != nil {
		logger.Error.Printf("Error validating %sSafeLine API token%s: %s%s%s", logger.Cyan, logger.Reset, logger.Red, LoginErr.Error(), logger.Reset)
		return true
	}

	if !(NoLoginAuthTokenResp.Err == "login-required" && NoLoginStatusCode == 401) {
		logger.Warning.Printf("Server endpoint %s/open/auth/token%s returned error: please check the %s-h%s command-line argument", logger.Cyan, logger.Reset, logger.Yellow, logger.Reset)
		return true
	}

	if LoginAuthTokenResp.Err == "login-required" && LoginStatusCode == 401 {
		logger.Warning.Printf("%sSafeLine API token%s is invalid: please check and retry", logger.Cyan, logger.Reset)
		return true
	}

	logger.Success.Printf("%sSafeLine%s configuration check completed!", logger.Cyan, logger.Reset)
	return false
}
