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

#include <gtest/gtest.h>
#include <ls-std-io-test.hpp>
#include <ls-std/ls-std-core.hpp>
#include <ls-std/ls-std-io.hpp>
#include <memory>

using ls::standard::core::FileNotFoundException;
using ls::standard::core::IllegalArgumentException;
using ls::standard::io::NewLine;
using ls::standard::io::SectionPairDocument;
using ls::standard::io::SectionPairFileReader;
using ls::standard::io::SectionPairFileReaderParameter;
using std::dynamic_pointer_cast;
using std::make_shared;
using std::shared_ptr;
using std::string;
using test::io::MockFileExistenceEvaluator;
using test::io::MockFileReader;
using testing::AtLeast;
using testing::Return;
using testing::Test;
using testing::TestWithParam;
using testing::Values;

namespace
{
  class SectionPairFileReaderTest : public Test
  {
    public:

      SectionPairFileReaderTest() = default;
      ~SectionPairFileReaderTest() override = default;

      static string getMockSectionPairFileContent()
      {
        return "# section-pair document\n"
               "\n"
               "[general]\n"
               "\n"
               "ports:\n"
               "  9090\n"
               "  8001\n"
               "  1989\n"
               "host=localhost\n";
      }

      static SectionPairFileReaderParameter createMockParameter(bool _fileExists)
      {
        SectionPairFileReaderParameter parameter{};
        parameter.setFileExistenceEvaluator(make_shared<MockFileExistenceEvaluator>(_fileExists));
        parameter.setReader(make_shared<MockFileReader>());
        parameter.setDocument(make_shared<SectionPairDocument>());
        parameter.setFilePath("settings.txt");

        return parameter;
      }
  };

  class SectionPairFileReaderTest_NotValidFileExtension : public TestWithParam<string>
  {
    public:

      SectionPairFileReaderTest_NotValidFileExtension() = default;
      ~SectionPairFileReaderTest_NotValidFileExtension() override = default;
  };

  TEST_F(SectionPairFileReaderTest, constructor_file_does_not_exist)
  {
    EXPECT_THROW(
        {
          try
          {
            SectionPairFileReader reader{SectionPairFileReaderTest::createMockParameter(false)};
          }
          catch (const FileNotFoundException &_exception)
          {
            throw;
          }
        },
        FileNotFoundException);
  }

  TEST_P(SectionPairFileReaderTest_NotValidFileExtension, constructor_no_valid_file_extension)
  {
    SectionPairFileReaderParameter parameter = SectionPairFileReaderTest::createMockParameter(true);
    parameter.setFilePath(GetParam());

    EXPECT_THROW(
        {
          try
          {
            SectionPairFileReader reader{parameter};
          }
          catch (const IllegalArgumentException &_exception)
          {
            const string message = _exception.what();
            const string expected = _exception.getName() + " thrown - \"" + GetParam() + "\" does not have a valid section pair file extension (.txt or .sp)!";

            ASSERT_STREQ(expected.c_str(), message.c_str());
            throw;
          }
        },
        IllegalArgumentException);
  }

  TEST_F(SectionPairFileReaderTest, getClassName)
  {
    ASSERT_STREQ("SectionPairFileReader", SectionPairFileReader{SectionPairFileReaderTest::createMockParameter(true)}.getClassName().c_str());
  }

  TEST_F(SectionPairFileReaderTest, getDocument)
  {
    const SectionPairFileReader reader{SectionPairFileReaderTest::createMockParameter(true)};
    ASSERT_FALSE(reader.getDocument() == nullptr);
  }

  TEST_F(SectionPairFileReaderTest, read)
  {
    const SectionPairFileReaderParameter parameter = SectionPairFileReaderTest::createMockParameter(true);
    parameter.getDocument()->reserveNewLine(NewLine::getUnixNewLine());
    SectionPairFileReader reader{parameter};
    const shared_ptr<MockFileReader> mockFileReader = dynamic_pointer_cast<MockFileReader>(parameter.getReader());

    EXPECT_CALL(*mockFileReader, read()).Times(AtLeast(1));
    ON_CALL(*mockFileReader, read()).WillByDefault(Return(SectionPairFileReaderTest::getMockSectionPairFileContent()));

    reader.read();

    ASSERT_TRUE(!reader.getDocument()->getSectionList().empty());
    ASSERT_STREQ("general", reader.getDocument()->get(0)->getSectionId().c_str());
    ASSERT_EQ(2, reader.getDocument()->get(0)->getRowAmount());
  }

  INSTANTIATE_TEST_SUITE_P(NotValidFileExtension, SectionPairFileReaderTest_NotValidFileExtension, Values("settings.json", "/var/log/document.html"));
}
