/*
 * Author:          Patrick-Christopher Mattulat
 * Co-Author:       Claude Sonnet 4.6 (LLM)
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2020-08-20
 * Changed:         2026-06-23
 *
 * */

#include <classes/TestHelper.hpp>
#include <gtest/gtest.h>
#include <ls-std/ls-std-core.hpp>
#include <ls-std/ls-std-io.hpp>

using ls::standard::core::IllegalArgumentException;
using ls::standard::core::interface_type::IWriter;
using ls::standard::io::File;
using ls::standard::io::FileOutputStream;
using ls::standard::io::FileReader;
using ls::standard::io::Logger;
using ls::standard::io::LogLevelValue;
using ls::standard::io::NewLine;
using ls::standard::test::TestHelper;
using std::dynamic_pointer_cast;
using std::make_shared;
using std::shared_ptr;
using std::string;
using testing::Test;

namespace
{
  class LoggerTest : public Test
  {
    public:

      LoggerTest() = default;
      ~LoggerTest() override = default;

      static shared_ptr<IWriter> createFileLogger(const string &_logName)
      {
        const string path = TestHelper::getResourcesFolderLocation() + _logName;
        File file{path};

        if (!file.exists())
        {
          file.createNewFile();
        }

        shared_ptr<IWriter> writer = make_shared<FileOutputStream>(file);

        return writer;
      }

      static string getContentFromLogFile(const string &_logName)
      {
        const File file{TestHelper::getResourcesFolderLocation() + _logName};
        FileReader reader{file};
        string content{reader.read()};
        file.remove();

        return content;
      }
  };

  TEST_F(LoggerTest, constructor_no_writer_reference)
  {
    EXPECT_THROW(
        {
          try
          {
            Logger logger{nullptr};
          }
          catch (const IllegalArgumentException &_exception)
          {
            throw;
          }
        },
        IllegalArgumentException);
  }

  TEST_F(LoggerTest, debug)
  {
    // write to log file

    const string logName = "output-debug.log";
    const shared_ptr<IWriter> writer = createFileLogger(logName);

    Logger logger{writer};
    logger.setLogLevel(LogLevelValue::DEBUG);
    logger.showLogLevel();
    logger.showTimestamp();
    logger.debug("1. line!");
    logger.info("2. line!");
    logger.error("3. line!");
    logger.fatal("4. line!");
    logger.warn("5. line!");
    logger.trace("6. line!");

    // get content and check

    dynamic_pointer_cast<FileOutputStream>(writer)->close();
    const string content = getContentFromLogFile(logName);

    ASSERT_TRUE(content.find("1. line!") != string::npos);
    ASSERT_TRUE(content.find("2. line!") != string::npos);
    ASSERT_TRUE(content.find("3. line!") != string::npos);
    ASSERT_TRUE(content.find("4. line!") != string::npos);
    ASSERT_TRUE(content.find("5. line!") != string::npos);
    ASSERT_FALSE(content.find("6. line!") != string::npos);
  }

  TEST_F(LoggerTest, error)
  {
    // write to log file

    const string logName = "output-error.log";
    const shared_ptr<IWriter> writer = createFileLogger(logName);

    Logger logger{writer};
    logger.setLogLevel(LogLevelValue::ERR);
    logger.showLogLevel();
    logger.showTimestamp();
    logger.debug("1. line!");
    logger.info("2. line!");
    logger.error("3. line!");
    logger.fatal("4. line!");
    logger.warn("5. line!");
    logger.trace("6. line!");

    // get content and check

    dynamic_pointer_cast<FileOutputStream>(writer)->close();
    const string content = getContentFromLogFile(logName);

    ASSERT_FALSE(content.find("1. line!") != string::npos);
    ASSERT_FALSE(content.find("2. line!") != string::npos);
    ASSERT_TRUE(content.find("3. line!") != string::npos);
    ASSERT_TRUE(content.find("4. line!") != string::npos);
    ASSERT_FALSE(content.find("5. line!") != string::npos);
    ASSERT_FALSE(content.find("6. line!") != string::npos);
  }

  TEST_F(LoggerTest, fatal)
  {
    // write to log file

    const string logName = "output-fatal.log";
    const shared_ptr<IWriter> writer = createFileLogger(logName);

    Logger logger{writer};
    logger.setLogLevel(LogLevelValue::FATAL);
    logger.showLogLevel();
    logger.showTimestamp();
    logger.debug("1. line!");
    logger.info("2. line!");
    logger.error("3. line!");
    logger.fatal("4. line!");
    logger.warn("5. line!");
    logger.trace("6. line!");

    // get content and check

    dynamic_pointer_cast<FileOutputStream>(writer)->close();
    const string content = getContentFromLogFile(logName);

    ASSERT_FALSE(content.find("1. line!") != string::npos);
    ASSERT_FALSE(content.find("2. line!") != string::npos);
    ASSERT_FALSE(content.find("3. line!") != string::npos);
    ASSERT_TRUE(content.find("4. line!") != string::npos);
    ASSERT_FALSE(content.find("5. line!") != string::npos);
    ASSERT_FALSE(content.find("6. line!") != string::npos);
  }

  TEST_F(LoggerTest, getLogLevel)
  {
    const Logger logger{createFileLogger("output.log")};
    ASSERT_EQ(LogLevelValue::INFO, logger.getLogLevel().getValue());
  }

  TEST_F(LoggerTest, hideLogLevel)
  {
    const string logName = "hide-log-level-output-fatal.log";
    const shared_ptr<IWriter> writer = createFileLogger(logName);

    Logger logger{writer};
    logger.setLogLevel(LogLevelValue::DEBUG);
    logger.hideLogLevel();
    logger.fatal("test message");

    // get content and check

    dynamic_pointer_cast<FileOutputStream>(writer)->close();
    const string content = getContentFromLogFile(logName);

    ASSERT_TRUE(content.find("FATAL") == string::npos);
  }

  TEST_F(LoggerTest, hideTimestamp)
  {
    const string logName = "hide-time-stamp-output-fatal.log";
    const shared_ptr<IWriter> writer = createFileLogger(logName);

    Logger logger{writer};
    logger.setLogLevel(LogLevelValue::DEBUG);
    logger.hideTimestamp();
    logger.fatal("test message");

    // get content and check

    dynamic_pointer_cast<FileOutputStream>(writer)->close();
    const string content = getContentFromLogFile(logName);

    ASSERT_TRUE(content.find('[') == string::npos);
  }

  TEST_F(LoggerTest, hideInformation)
  {
    const string logName = "hide-info-output-fatal.log";
    const shared_ptr<IWriter> writer = createFileLogger(logName);
    const string message = "test message";

    Logger logger{writer};
    logger.setLogLevel(LogLevelValue::DEBUG);
    logger.hideLogLevel();
    logger.hideTimestamp();
    logger.fatal(message.c_str());

    // get content and check

    dynamic_pointer_cast<FileOutputStream>(writer)->close();
    const string content = getContentFromLogFile(logName);

    ASSERT_STREQ(string(message + NewLine::get()).c_str(), content.c_str());
  }

  TEST_F(LoggerTest, info)
  {
    // write to log file

    const string logName = "output-info.log";
    const shared_ptr<IWriter> writer = createFileLogger(logName);

    Logger logger{writer};
    logger.setLogLevel(LogLevelValue::INFO);
    logger.showLogLevel();
    logger.showTimestamp();
    logger.fatal("1. line!");
    logger.error("2. line!");
    logger.warn("3. line!");
    logger.info("4. line!");
    logger.debug("5. line!");
    logger.trace("6. line!");

    // get content and check

    dynamic_pointer_cast<FileOutputStream>(writer)->close();
    const string content = getContentFromLogFile(logName);

    ASSERT_TRUE(content.find("1. line!") != string::npos);
    ASSERT_TRUE(content.find("2. line!") != string::npos);
    ASSERT_TRUE(content.find("3. line!") != string::npos);
    ASSERT_TRUE(content.find("4. line!") != string::npos);
    ASSERT_FALSE(content.find("5. line!") != string::npos);
    ASSERT_FALSE(content.find("6. line!") != string::npos);
  }

  TEST_F(LoggerTest, setLogLevel)
  {
    Logger logger{createFileLogger("output.log")};
    logger.setLogLevel(LogLevelValue::ERR);

    ASSERT_EQ(LogLevelValue::ERR, logger.getLogLevel().getValue());
  }

  TEST_F(LoggerTest, showLogLevel)
  {
    const string logName = "show-log-level-output-fatal.log";
    const shared_ptr<IWriter> writer = createFileLogger(logName);

    Logger logger{writer};
    logger.setLogLevel(LogLevelValue::DEBUG);
    logger.showLogLevel();
    logger.fatal("test message");

    // get content and check

    dynamic_pointer_cast<FileOutputStream>(writer)->close();
    const string content = getContentFromLogFile(logName);

    ASSERT_TRUE(content.find("FATAL") != string::npos);
  }

  TEST_F(LoggerTest, showTimestamp)
  {
    const string logName = "hide-time-stamp-output-fatal.log";
    const shared_ptr<IWriter> writer = createFileLogger(logName);

    Logger logger{writer};
    logger.setLogLevel(LogLevelValue::DEBUG);
    logger.showTimestamp();
    logger.fatal("test message");

    // get content and check

    dynamic_pointer_cast<FileOutputStream>(writer)->close();
    const string content = getContentFromLogFile(logName);

    ASSERT_TRUE(content.find('[') != string::npos);
  }

  TEST_F(LoggerTest, trace)
  {
    // write to log file

    const string logName = "output-trace.log";
    const shared_ptr<IWriter> writer = createFileLogger(logName);

    Logger logger{writer};
    logger.setLogLevel(LogLevelValue::TRACE);
    logger.showLogLevel();
    logger.showTimestamp();
    logger.fatal("1. line!");
    logger.error("2. line!");
    logger.warn("3. line!");
    logger.info("4. line!");
    logger.debug("5. line!");
    logger.trace("6. line!");

    // get content and check

    dynamic_pointer_cast<FileOutputStream>(writer)->close();
    const string content = getContentFromLogFile(logName);

    ASSERT_TRUE(content.find("1. line!") != string::npos);
    ASSERT_TRUE(content.find("2. line!") != string::npos);
    ASSERT_TRUE(content.find("3. line!") != string::npos);
    ASSERT_TRUE(content.find("4. line!") != string::npos);
    ASSERT_TRUE(content.find("5. line!") != string::npos);
    ASSERT_TRUE(content.find("6. line!") != string::npos);
  }

  TEST_F(LoggerTest, warn)
  {
    // write to log file

    const string logName = "output-warn.log";
    const shared_ptr<IWriter> writer = createFileLogger(logName);

    Logger logger{writer};
    logger.setLogLevel(LogLevelValue::WARN);
    logger.showLogLevel();
    logger.showTimestamp();
    logger.fatal("1. line!");
    logger.error("2. line!");
    logger.warn("3. line!");
    logger.info("4. line!");
    logger.debug("5. line!");
    logger.trace("6. line!");

    // get content and check

    dynamic_pointer_cast<FileOutputStream>(writer)->close();
    const string content = getContentFromLogFile(logName);

    ASSERT_TRUE(content.find("1. line!") != string::npos);
    ASSERT_TRUE(content.find("2. line!") != string::npos);
    ASSERT_TRUE(content.find("3. line!") != string::npos);
    ASSERT_FALSE(content.find("4. line!") != string::npos);
    ASSERT_FALSE(content.find("5. line!") != string::npos);
    ASSERT_FALSE(content.find("6. line!") != string::npos);
  }
}
