 defmodule Farmbot.BotState do
-  @moduledoc "JSON Serializable state tree that gets pushed over variour transports."
+  @moduledoc """
+  Central bot state that gets pushed out over transports.
+    * JSON Serializable
+    * Global
+    * Scary
+  """
 
   use GenStage
   @version Farmbot.Project.version()

    new_user_env = Map.merge(state.user_env, %{to_string(key) => val})
    case Poison.encode(new_user_env) do
      {:ok, encoded} ->
        Farmbot.System.ConfigStorage.update_config_value(:string, "settings", "user_env", encoded)
        {:reply, :ok, [], state}
      _ -> {:reply, {:error, "user_env must be json encodeable!"}, [], state}
    end

    do_handle(rest, new_state)
  end

  defp do_handle([{:config, "settings", "auto_sync", true} | rest], state) do
    spawn Farmbot.Repo, :flip, []
    new_config = Map.put(state.configuration, "auto_sync", true)
    new_state = %{state | configuration: new_config}
    do_handle(rest, new_state)
  end
