package sandbox

import (
	
	
	
	

	
	
	
	
	
	
	
	
	
)

// GenerateCommand creates a command for running Sandbox PHP instance with required parameters
func ( *models.Extension,  *models.Credentials,  *models.CommandParams) (string, error) {
	// Get needed datas
	.Name = shellescape.StripUnsafe(.Name)

	if !helpers.IsLetter(.Name) {
		return "", logger.FiberError(fiber.StatusBadRequest, "extension names can only contains letters")
	}

	, , ,  := GetParams(, , )
	if  != nil {
		return "", 
	}

	, ,  := liman.GetPermissions(, .Name)
	if  != nil {
		return "", 
	}

	,  := liman.GetExtensionJSON()
	if  != nil {
		return "", 
	}

	 := []string{}
	if ["functions"] != nil {
		for ,  := range ["functions"].([]interface{}) {
			 := .(map[string]any)
			 = append(, ["name"].(string))
		}
	}

	if .Status != 1 && !helpers.Contains(, .TargetFunction) && helpers.Contains(, .TargetFunction) {
		return "", logger.FiberError(fiber.StatusForbidden, "you have no permission to do this")
	}

	if .Username != "" && .Key != "" {
		["clientUsername"] = .Username
		["clientPassword"] = .Key
	}

	,  := liman.GetLicence()
	 := ""
	if  == nil {
		 = .Data
	}

	// Convert required datas to JSON
	,  := json.Marshal()
	,  := json.Marshal()
	,  := json.Marshal()
	,  := json.Marshal()
	,  := json.Marshal(.RequestData)
	,  := json.Marshal()
	,  := json.Marshal()

	// Construct data that needed to be used on PHP Sandbox
	 := map[string]string{
		"server":          string(),
		"extension":       string(),
		"settings":        string(),
		"user":            string(),
		"permissions":     string(),
		"variables":       string(),
		"requestData":     string(),
		"publicPath":      fmt.Sprintf(constants.EXTENSION_PUBLIC_PATH, .BaseURL, .ID),
		"functionsPath":   fmt.Sprintf("%s/%s%s", constants.EXTENSIONS_PATH, strings.ToLower(.Name), constants.FUNCTIONS_FILE_PATH),
		"navigationRoute": fmt.Sprintf(constants.NAVIGATION_ROUTE, .ID, .ID),
		"key_type":        .Type,
		"function":        .TargetFunction,
		"license":         ,
		"token":           .Token,
		"locale":          .Locale,
		"log_id":          .LogID,
		"ajax":            "true",
		"apiRoute":        "/extensionRun",
	}

	if .AuthType == "keycloak" {
		 = auth.RefreshTokenIfNecessary(.ID)
		if  != nil {
			return "", 
		}

		,  := auth.GetOauth2Token(.ID)
		if  != nil {
			return "", 
		}
		["keycloak_auth"] = .AccessToken
	}

	,  := os.ReadFile(constants.KEYS_PATH + "/" + .ID)
	if  != nil {
		return "", logger.FiberError(fiber.StatusNotFound, "cannot found extension key file")
	}

	,  := json.Marshal()
	 := aes256.Encrypt(string(), string())

	// If exist, use Liman licence system on extension runner
	 := "/liman/extensions/" + strings.ToLower(.Name) + "/liman.so"
	 := ""
	if ,  := os.Stat();  == nil {
		 = "-dextension=" + shellescape.Quote() + " "
	}

	// Create command
	 := ""
	if helpers.Env("CONTAINER_MODE", "false") != "true" {
		 = fmt.Sprintf(
			"runuser %s -c 'timeout %s /usr/bin/php %s -d display_errors=on %s %s %s'",
			strings.Replace(.ID, "-", "", -1),
			helpers.Env("EXTENSION_TIMEOUT", "30"),
			,
			constants.SANDBOX_PATH,
			constants.KEYS_PATH+"/"+.ID,
			,
		)
	} else {
		 = fmt.Sprintf(
			"runuser extuser -c 'timeout %s /usr/bin/php %s -d display_errors=on %s %s %s'",
			helpers.Env("EXTENSION_TIMEOUT", "30"),
			,
			constants.SANDBOX_PATH,
			constants.KEYS_PATH+"/"+.ID,
			,
		)
	}

	return , nil
}

// Get needed parameters and return them
func (
	 *models.Extension,
	 *models.Credentials,
	 *models.CommandParams,
) (*models.Server, *models.User, map[string]string, error) {
	,  := liman.GetServer(&models.Server{ID: .Server})
	if  != nil {
		return nil, nil, nil, 
	}

	,  := liman.GetUser(&models.User{ID: .User})
	if  != nil {
		return nil, nil, nil, 
	}

	,  := liman.GetSettings(, , )
	if  != nil {
		return nil, nil, nil, 
	}

	return , , , nil
}