get_random_string(){
    local random_password=$(tr -dc 'A-Za-z0-9' </dev/urandom | head -c 30)
    echo "$random_password"
}


update_verification_keys() {
    local db_config="/opt/cyops/configs/database/db_config.yml"
    local utils_py="/opt/cyops/scripts/confUtil.py"

    # Fixed keys to be present in the conf file
    local keys=("router" "das" "integrations")

    # Start new dict
    local updated_dict="{"

    for key_name in "${keys[@]}"; do
        val=$(python3 "$utils_py" -f "$db_config" -g "$key_name" 2>/dev/null || echo 0)
        # if already present use the same
        if [ "$val" != "0" ] && [ -n "$val" ]; then
          updated_dict+="\"$key_name\":\"$val\","
        # otherwise generate random value
        else
          local rand_val
          rand_val=$(get_random_string)
          updated_dict+="\"$key_name\":\"$rand_val\","
        fi
    done

    # Remove trailing comma and close JSON
    updated_dict="${updated_dict%,}}"

    # Write back using confUtil.py
    python3 "$utils_py" -f "$db_config" -k verification_key -v "$updated_dict" -a APPEND

    echo "Updated keys: ${keys[*]}"
}

update_verification_keys
