// docker-finance | modern accounting for the power-user
//
// Copyright (C) 2024-2025 Aaron Fiore (Founder, Evergreen Crypto LLC)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation  either version 3 of the License  or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not  see <https://www.gnu.org/licenses/>.

//! \file
//! \author Aaron Fiore (Founder, Evergreen Crypto LLC)
//! \note File intended to be loaded into ROOT.cern framework / Cling interpreter
//! \since docker-finance 1.1.0

// NOTE: `dfi` API headers are included by default

#include "./example.hh"

#include <limits>

namespace dfi::plugin::example
{
std::string Example::make_cmd(const std::string& base, const std::string& arg)
{
  return std::string{base + arg};
}

void Example::exec_cmd(const std::string& cmd)
{
  namespace common = ::dfi::common;

  const std::string base{
      common::get_env("global_basename") + " "
      + common::get_env("global_parent_profile")
      + common::get_env("global_arg_delim_1")
      + common::get_env("global_child_profile") + " "};

  common::throw_ex_if<common::type::RuntimeError>(
      common::exec("bash -i -c '" + Example::make_cmd(base, cmd) + "'"),
      "command failed");
}

void Example::print_env(const std::string& env)
{
  std::cout << env << "=" << ::dfi::common::get_env(env) << "\n";
};

void Example::example1()
{
  using g_Random = dfi::crypto::cryptopp::Random;
  using g_Hash = dfi::crypto::libsodium::Hash;
  g_Random r;
  g_Hash h;
  for (size_t i{}; i < 5; i++)
    {
      uint32_t num = r.generate();
      std::cout << h.encode<g_Hash::SHA2_256>(num) << " = " << num << "\n";
    }
}

void Example::example2()
{
  // Container environment
  std::cout
      << "\nShell environment (container)\n-----------------------------\n";
  Example::print_env("DOCKER_FINANCE_CLIENT_FLOW");
  Example::print_env("DOCKER_FINANCE_CONTAINER_CMD");
  Example::print_env("DOCKER_FINANCE_CONTAINER_CONF");
  Example::print_env("DOCKER_FINANCE_CONTAINER_EDITOR");
  Example::print_env("DOCKER_FINANCE_CONTAINER_FLOW");
  Example::print_env("DOCKER_FINANCE_CONTAINER_PLUGINS");
  Example::print_env("DOCKER_FINANCE_CONTAINER_REPO");
  Example::print_env("DOCKER_FINANCE_CONTAINER_SHARED");
  Example::print_env("DOCKER_FINANCE_DEBUG");
  Example::print_env("DOCKER_FINANCE_VERSION");

  std::cout << "\nSame as previous but executing commands in "
               "shell\n------------------------------------------------\n";
  ::dfi::common::exec("printenv | grep ^DOCKER_FINANCE | sort");

  // Caller environment
  std::cout << "\nShell environment (caller)\n--------------------------\n";
  Example::print_env("global_arg_delim_1");
  Example::print_env("global_arg_delim_2");
  Example::print_env("global_arg_delim_3");
  Example::print_env("global_arg_subcommand");
  Example::print_env("global_basename");
  Example::print_env("global_child_profile");
  Example::print_env("global_child_profile_flow");
  Example::print_env("global_child_profile_journal");
  Example::print_env("global_conf_fetch");
  Example::print_env("global_conf_hledger");
  Example::print_env("global_conf_meta");
  Example::print_env("global_conf_subscript");
  // TODO(unassigned): read array from shell? ROOT impl simply calls stdlib getenv()
  // Example::print_env("global_hledger_cmd");
  Example::print_env("global_parent_profile");
  Example::print_env("global_usage");
}

void Example::example3()
{
  std::cout << "\nImporting journals...\n";
  Example::exec_cmd("import 1>/dev/null");

  std::cout << "\nShowing BTC balance...\n";
  Example::exec_cmd("hledger bal assets liabilities cur:BTC");

  std::cout << "\nGenerating income tax snippet...\n";
  const std::string delim{::dfi::common::get_env("global_arg_delim_2")};
  Example::exec_cmd(
      "taxes all" + delim + "account tag" + delim + "income write" + delim
      + "off | tail -n3");
}
}  // namespace dfi::plugin::example

// # vim: sw=2 sts=2 si ai et
