<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace Kufer\Web\Classes\Dbfilter;

use Kufer\Web\Classes\Identifier\Identifier;
use Kufer\Web\Classes\Identifier\IdentifierFactory;


class DozUrlaubeSQLFilter extends BasicSQLFilter
{
  protected function __construct()
  {
    parent::__construct();

    $this->registerDb('Id', 'id', static::TYPE_INTEGER);
    $this->registerDb('Dozent', 'dnr', static::TYPE_IDENTIFIER_NO_GUID);
    $this->registerDb('Date', 'datum', static::TYPE_DATE);
    $this->registerDb('ZeitBeginn', 'zeit_beg', static::TYPE_TIME);
    $this->registerDb('ZeitEnde', 'zeit_ende', static::TYPE_TIME);
    $this->registerDb('GanzerTag', 'gh_versendung', static::TYPE_BOOLEAN_HALF_DEFAULT_TRUE);
    $this->registerDb('Grund', 'ort', static::TYPE_STRING);
  }

  public function getTablePraeffix(): string
  {
    return 'kt';
  }

  
  public function equalsId($id): IFilter
  {
    $this->applyFilterCurrent(
      static::FILTER_OPTION_ID,
      SQLFilterUtils::filterEqualsIdInt('id', $id)
    );

    return $this;
  }

  
  public function inId(iterable $ids): IFilter
  {
    $this->applyFilterCurrent(
      static::FILTER_OPTION_ID,
      SQLFilterUtils::filterInIdInt('id', $ids)
    );

    return $this;
  }

  
  public function equalsDozent($id): IFilter
  {
    $id = IdentifierFactory::createFromIdIfNecessary($id);

    $this->applyFilterCurrent(
      static::FILTER_OPTION_ID,
      \FunctionOperations::combine(
        SQLFilterUtils::filterEqualsIdInt('dnr', $id->getId()),
        SQLFilterUtils::filterEqualsString(':table:', 'knr', '__DO' . $id->getId())
      )
    );

    return $this;
  }

  
  public function inDozent(iterable $ids): IFilter
  {
    $ids = IdentifierFactory::createFromIdsIfNecessary(iterator_to_array($ids));
    $intIds = array_map(static function(Identifier $id): int {
      return $id->getId();
    }, $ids);

    $this->applyFilterCurrent(
      static::FILTER_OPTION_ID,
      \FunctionOperations::combine(
        SQLFilterUtils::filterInIdInt('dnr', $intIds),
        SQLFilterUtils::filterInStringDefault(':table:', 'knr', array_map(\FunctionOperations::prefixString('__DO'), $intIds), '')
      )
    );

    return $this;
  }
}