<?php
namespace SabaiApps\Directories\Component\Payment\Controller;

use SabaiApps\Directories\Component\Entity\Controller\QueryEntities as QueryEntitiesBase;
use SabaiApps\Directories\Context;

class QueryEntities extends QueryEntitiesBase {

	protected function _getQuery( Context $context, $entityType, $bundle, $q, $userId ) {
		$query = parent::_getQuery( $context, $entityType, $bundle, $q, $userId );
		if ( $plan = $context->getRequest()->asInt( 'payment_plan' ) ) {
			switch ( $plan ) {
				case 1: // expired
					$query->fieldIsOrSmallerThan( 'payment_plan', time(), 'expires_at' )
						->fieldIsGreaterThan( 'payment_plan', 0, 'expires_at' );
					break;
				case 2: // expiring
					$expiring_ts = time() + 86400 * $this->_application->getComponent( 'Payment' )->getConfig( 'plan', 'expiring_days' );
					$query->fieldIsGreaterThan( 'payment_plan', time(), 'expires_at' )
						->fieldIsOrSmallerThan( 'payment_plan', $expiring_ts, 'expires_at' );
					break;
				case 3: // deactivated
					$query->fieldIsGreaterThan( 'payment_plan', 0, 'deactivated_at' );
					break;
			}
		}

		return $query;
	}
}
