/*
* 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 <ls-std/io/section-pair/validator/SectionPairDocumentValidator.hpp>
#include <ls-std/io/section-pair/validator/SectionPairSectionValidator.hpp>
#include <regex>

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

SectionPairDocumentValidator::SectionPairDocumentValidator(string _document) : Class("SectionPairDocumentValidator"), document(::move(_document))
{}

SectionPairDocumentValidator::~SectionPairDocumentValidator() noexcept = default;

bool SectionPairDocumentValidator::isValid()
{
  const string validationRegex = _getValidationRegex();
  static auto documentRegex = regex{"^" + validationRegex + "$"};

  return regex_match(this->document, documentRegex);
}

string SectionPairDocumentValidator::_getValidationRegex()
{
  const string newLine = R"(((\n)|(\r\n)))";
  const string documentHeader = R"((# {1}(section\-pair document))" + newLine + R"())";
  const string section = SectionPairSectionValidator::getValidationRegex();
  const string atLeastOneSection = R"(()" + section + R"())";
  const string optionalSections = R"(()" + section + R"()*)";

  return documentHeader + atLeastOneSection + optionalSections;
}
