<?php

declare(strict_types=1);

namespace App\Filters;

use Openguru\OpenCartFramework\CriteriaBuilder\Criterion;
use Openguru\OpenCartFramework\CriteriaBuilder\Rules\BaseRule;
use Openguru\OpenCartFramework\QueryBuilder\Builder;

class ProductStatus extends BaseRule
{
    public const NAME = 'RULE_PRODUCT_STATUS';

    public static function initWithDefaults(): BaseRule
    {
        return new self(static::NAME, [
            'product_status' => new Criterion(static::CRITERIA_OPTION_BOOLEAN, [
                'operator' => static::CRITERIA_OPERATOR_EQUALS,
                'value' => true,
            ]),
        ]);
    }

    public function apply(Builder $builder, $operand): void
    {
        /** @var Criterion $criterion */
        foreach ($this->criteria as $criterion) {
            if ($criterion->type === static::CRITERIA_OPTION_BOOLEAN) {
                $value = $criterion->params['value'];
                $builder->where('products.status', '=', $value, $operand);
            }
        }
    }
}
