<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */
class DBStructureInfo
{
  private $structure;

  public function __construct()
  {
    $this->structure = new DBStructureInfoCurrent();
  }

  public function getVersion()
  {
    return $this->structure->getVersion();
  }

  public function getHash()
  {
    $str = '';
    $str .= $this->getExceptionsTxt();
    $str .= $this->getFieldsTxt();
    $str .= $this->getTablesTxt();
    $str .= $this->getMysqlStrukturTxt();
    $str .= $this->getMysqlStrukturCodeTxt();
    $str .= $this->getMysqlStrukturHlGrundsys();
    $str .= $this->getMysqlStrukturTablesPk();

    return md5($str);
  }

  public function getExceptionsTxt()
  {
    return $this->structure->getExceptionsTxt();
  }

  public function getFieldsTxt()
  {
    return $this->structure->getFieldsTxt();
  }

  public function getTablesTxt()
  {
    return $this->structure->getTablesTxt();
  }

  public function getWinbasysTxt()
  {
    return $this->structure->getWinbasysTxt();
  }

  public function getMysqlStrukturTxt(?bool $hasKuferTools = null)
  {
    return $this->structure->getMysqlStrukturTxt($hasKuferTools);
  }

  public function getMysqlStrukturCodeTxt(?bool $hasKuferTools = null)
  {
    return $this->structure->getMysqlStrukturCodeTxt($hasKuferTools);
  }

  public function getMysqlStrukturHlGrundsys()
  {
    return $this->structure->getMysqlStrukturHlGrundsys();
  }

  public function getMysqlStrukturTablesPk()
  {
    return $this->structure->getMysqlStrukturTablesPk();
  }

  
  public function createTablesMappedByGroupNumber(): array
  {
    return (new \Kufer\Web\Classes\Stream\IterableStream(preg_split('/\\n\\r|\\r\\n|\\n|\\r/', $this->getTablesTxt())))
      ->map('\\trim')
      ->filter(\FunctionOperations::not(\FunctionOperations::argsWithPlaceholder(
        \StringOperations::class . '::startsWith',
        \FunctionOperations::placeholder(),
        '//'
      )))
      ->filter(\FunctionOperations::not(\FunctionOperations::isEmpty()))
      ->map(static function(string $str) {
        $matches = array();
        preg_match_all('/^group([0-9]+)=(.*)$/', $str, $matches);

        $numberStr = $matches[1][0];
        if (!is_numeric($numberStr)) {
          return new \Kufer\Web\Classes\Addfunctions\Pair(-1, array());
        }

        $groups = $matches[2][0];

        return new \Kufer\Web\Classes\Addfunctions\Pair((int)$numberStr, array_filter(\StringOperations::explodeAndTrim($groups, ';')));
      })
      ->toKeyValueArrayFromPairs();
  }

  
  public function findWebTables(): array
  {
    $structure = $this->getMysqlStrukturTxt();
    if (preg_match_all('/CREATE TABLE IF NOT EXISTS ([a-zA-Z0-9_]+)[ (]/', $structure, $matches)) {
      return $matches[1];
    }

    return array();
  }
}

class DBStructureInfoCurrent extends DBStructureInfo
{
  private $structurePath;

  public function __construct()
  {
    $this->structurePath = BASEDIR . 'classes' . DIRECTORY_SEPARATOR . 'db' . DIRECTORY_SEPARATOR . 'structure' . DIRECTORY_SEPARATOR;
  }

  public function getVersion()
  {
    return VERSION;
  }

  public function getExceptionsTxt()
  {
    include $this->structurePath . 'exceptions.php';

    return $content;
  }

  public function getFieldsTxt()
  {
    include $this->structurePath . 'fields.php';

    return $content;
  }

  public function getTablesTxt()
  {
    include $this->structurePath . 'tables.php';

    return $content;
  }

  public function getWinbasysTxt()
  {
    include $this->structurePath . 'winbasys.php';

    return $content;
  }

  public function getMysqlStrukturTxt(?bool $hasKuferTools = null)
  {
    include $this->structurePath . 'mysqlstruktur.php';

    return $content;
  }

  public function getMysqlStrukturCodeTxt(?bool $hasKuferTools = null)
  {
    include $this->structurePath . 'mysqlstrukturcode.php';

    return $content;
  }

  public function getMysqlStrukturHlgrundsys()
  {
    include $this->structurePath . 'hlgrundsys.php';

    return $content;
  }

  public function getMysqlStrukturTablesPk()
  {
    include $this->structurePath . 'tablespk.php';

    return $content;
  }
}