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

#include <ls-std/io/section-pair/validator/SectionPairIdentifierValidator.hpp>
#include <ls-std/io/section-pair/validator/SectionPairRowValidator.hpp>
#include <ls-std/io/section-pair/validator/SectionPairSectionValidator.hpp>
#include <regex>
#include <string>

using ls::standard::core::Class;
using ls::standard::io::SectionPairIdentifierValidator;
using ls::standard::io::SectionPairRowValidator;
using ls::standard::io::SectionPairSectionValidator;
using std::move;
using std::regex;
using std::regex_match;
using std::string;

SectionPairSectionValidator::SectionPairSectionValidator(string _section) : Class("SectionPairSectionValidator"), section(::move(_section))
{}

SectionPairSectionValidator::~SectionPairSectionValidator() noexcept = default;

string SectionPairSectionValidator::getValidationRegex()
{
  return _getValidationRegex();
}

bool SectionPairSectionValidator::isValid()
{
  const string validationRegex = _getValidationRegex();
  static auto sectionRegex = regex{"^" + validationRegex};

  return regex_match(this->section, sectionRegex);
}

string SectionPairSectionValidator::_getValidationRegex()
{
  const string newLine = R"(((\n)|(\r\n)))";
  const string identifier = SectionPairIdentifierValidator::getValidationRegex();
  const string sectionHeader = newLine + R"(\[{1}()" + identifier + R"()\]{1}()" + newLine + R"({2}))";
  const string row = SectionPairRowValidator::getValidationRegex();
  const string atLeastOneRow = R"((()" + row + R"(){1}))";
  const string optionalRows = R"((()" + row + R"()*))";

  return sectionHeader + atLeastOneRow + optionalRows;
}
