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

declare(strict_types = 1);

namespace Kufer\Web\Classes\Dbfilter;


class BeziehungenSQLFilter extends BasicSQLFilter
{
  public const FILTER_OPTION_HLBEZIEHUNGEN = 'hlbeziehungen';

  protected function __construct()
  {
    parent::__construct();

    $this->registerDb('Id', 'id', static::TYPE_INTEGER);
    $this->registerDb('Typ1', 'typ1', static::TYPE_STRING);
    $this->registerDb('Typ2', 'typ2', static::TYPE_STRING);
    $this->registerDb('Referenz1', 'nr1', static::TYPE_IDENTIFIER_NO_GUID);
    $this->registerDb('Referenz2', 'nr2', static::TYPE_IDENTIFIER_NO_GUID);
    $this->registerDb('KursReferenz1', 'nr1', static::TYPE_STRING);
    $this->registerDb('KursReferenz2', 'nr2', static::TYPE_STRING);
    $this->registerDb('Beziehungstyp', 'bezid', static::TYPE_INTEGER);
    $this->registerDb('GueltigVon', 'gueltig_von', static::TYPE_DATE);
    $this->registerDb('GueltigBis', 'gueltig_bis', static::TYPE_DATE);
    $this->registerDb('Inaktiv', 'inaktiv', static::TYPE_BOOLEAN_W_F);
    $this->registerDb('Bemerkung', 'bemerkung', static::TYPE_STRING);
    $this->registerDb('Abgleich', 'abgleich', static::TYPE_BOOLEAN_W_F);
  }

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

  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 applyHLBeziehungenFilter(HLBeziehungenSQLFilter $filter): IFilter
  {
    $filter->makeImmutable();

    if ($filter->isEmpty()) {
      return $filter;
    }

    $this->applyFilterCurrent(
      static::FILTER_OPTION_HLBEZIEHUNGEN,
      function(\SelectQueryBuilder $builder) use ($filter): \SelectQueryBuilder {
        $hlBezBuilder = $filter->createBuilder($builder->getDBOperation())
          ->select('id')
          ->from('hlbeziehung', 'hlbz');

        $var = $builder->uniqueVar();

        return $builder->and(":table:.bezid IN (:$var:)")
          ->paramSelectQuery($var, $hlBezBuilder);
      }
    );

    return $this;
  }

  
  public function hasCurrentlyValidArt($art): BeziehungenSQLFilter
  {
    if (!is_array($art)) {
      $art = array($art);
    }

    $filterHLBez = \Kufer\Web\Classes\Dbfilter\HLBeziehungenSQLFilter::create();
    $filterHLBez->inArt($art);
    $filterHLBez->notAny(static function($filter) {
      $filter->isInaktiv();

      return $filter;
    });

    $this->applyHLBeziehungenFilter($filterHLBez);

    $this->notAny(static function(BeziehungenSQLFilter $filter) {
      $filter->isInaktiv();

      return $filter;
    });

    $datenow = \DateTimeOperations::getDateNow();
    $this->any(static function(BeziehungenSQLFilter $filter) use ($datenow) {
      $filter->equalsGueltigVon(DEFAULTDATE);
      $filter->lessEqualsGueltigVon($datenow);

      return $filter;
    });

    $this->any(static function(BeziehungenSQLFilter $filter) use ($datenow) {
      $filter->equalsGueltigBis(DEFAULTDATE);
      $filter->greaterEqualsGueltigBis($datenow);

      return $filter;
    });

    return $this;
  }
}