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

#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::type::byte_field;
using ls::standard::io::NewLine;
using ls::standard::io::SectionPairRow;
using ls::standard::io::SectionPairRowEnumType;
using ls::standard::io::SectionPairRowListValue;
using ls::standard::io::SectionPairRowSingleValue;
using ls::standard::io::SectionPairRowValue;
using ls::standard::io::section_pair_identifier;
using std::dynamic_pointer_cast;
using std::make_shared;
using std::shared_ptr;
using std::string;
using testing::Test;

namespace
{
  class SectionPairRowTest : public Test
  {
    public:

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

  TEST_F(SectionPairRowTest, constructor_empty_key)
  {
    EXPECT_THROW(
        {
          try
          {
            SectionPairRow row("", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
          }
          catch (const IllegalArgumentException &_exception)
          {
            throw;
          }
        },
        IllegalArgumentException);
  }

  TEST_F(SectionPairRowTest, constructor_invalid_key)
  {
    EXPECT_THROW(
        {
          try
          {
            SectionPairRow row("-tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
          }
          catch (const IllegalArgumentException &_exception)
          {
            throw;
          }
        },
        IllegalArgumentException);
  }

  TEST_F(SectionPairRowTest, getClassName)
  {
    ASSERT_STREQ("SectionPairRow", SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE).getClassName().c_str());
  }

  TEST_F(SectionPairRowTest, getKey)
  {
    const section_pair_identifier key = "tmp-key";

    const SectionPairRow row(key, SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
    ASSERT_STREQ(key.c_str(), row.getKey().c_str());
  }

  TEST_F(SectionPairRowTest, getValue)
  {
    const SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
    const shared_ptr<SectionPairRowValue> value = row.getValue();

    ASSERT_TRUE(value != nullptr);
    ASSERT_EQ(SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE, value->getType());
  }

  TEST_F(SectionPairRowTest, isSingleValue)
  {
    ASSERT_TRUE(SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE).isSingleValue());
  }

  TEST_F(SectionPairRowTest, isList)
  {
    ASSERT_FALSE(SectionPairRow("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE).isList());
  }

  TEST_F(SectionPairRowTest, marshal_single_value)
  {
    const auto row = make_shared<SectionPairRow>("favourite-color", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
    const shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());
    singleValue->set("blue");
    const byte_field expected = "favourite-color=blue" + NewLine::get();
    const byte_field actual = row->marshal();

    ASSERT_STREQ(expected.c_str(), actual.c_str());
  }

  TEST_F(SectionPairRowTest, marshal_list_value)
  {
    const auto row = make_shared<SectionPairRow>("favourite-colors", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
    const shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
    listValue->add("blue");
    listValue->add("red");
    listValue->add("purple");
    const string newLine = NewLine::get();

    const string expected = "favourite-colors:" + newLine + "  blue" + newLine + "  red" + newLine + "  purple" + newLine;

    ASSERT_STREQ(expected.c_str(), row->marshal().c_str());
  }

  TEST_F(SectionPairRowTest, setKey)
  {
    SectionPairRow row("hobbies", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
    row.setKey("colors");

    ASSERT_STREQ("colors", row.getKey().c_str());
  }

  TEST_F(SectionPairRowTest, setKey_empty_key)
  {
    SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);

    EXPECT_THROW(
        {
          try
          {
            row.setKey("");
          }
          catch (const IllegalArgumentException &_exception)
          {
            throw;
          }
        },
        IllegalArgumentException);
  }

  TEST_F(SectionPairRowTest, setKey_invalid_key)
  {
    SectionPairRow row("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);

    EXPECT_THROW(
        {
          try
          {
            row.setKey("=33");
          }
          catch (const IllegalArgumentException &_exception)
          {
            throw;
          }
        },
        IllegalArgumentException);
  }

  TEST_F(SectionPairRowTest, unmarshal_single_value)
  {
    const auto row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE);
    const shared_ptr<SectionPairRowSingleValue> singleValue = dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue());

    row->unmarshal("favourite-color=blue");

    ASSERT_STREQ("favourite-color", row->getKey().c_str());
    ASSERT_STREQ("blue", singleValue->get().c_str());
  }

  TEST_F(SectionPairRowTest, unmarshal_list_value)
  {
    const auto row = make_shared<SectionPairRow>("tmp-key", SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE);
    const shared_ptr<SectionPairRowListValue> listValue = dynamic_pointer_cast<SectionPairRowListValue>(row->getValue());
    const string newLine = NewLine::get();

    const string data = "favourite-colors:" + newLine + "  blue" + newLine + "  red" + newLine + "  purple" + newLine;
    row->unmarshal(data);

    ASSERT_STREQ("favourite-colors", row->getKey().c_str());
    ASSERT_STREQ("blue", listValue->get(0).c_str());
    ASSERT_STREQ("red", listValue->get(1).c_str());
    ASSERT_STREQ("purple", listValue->get(2).c_str());
    ASSERT_EQ(3, listValue->getSize());
  }
}
