<?php

declare(strict_types=1);

namespace App\Concerns\Actions\Http\Api;

use App\Http\Api\Query\Query;
use App\Http\Api\Schema\Schema;
use App\Http\Api\Scope\Scope;
use Illuminate\Database\Eloquent\Builder;

trait FiltersModels
{
    public function filter(Builder $builder, Query $query, Schema $schema, Scope $scope): Builder
    {
        foreach ($query->getFilterCriteria() as $criteria) {
            foreach ($schema->filters() as $filter) {
                if ($criteria->shouldFilter($filter, $scope)) {
                    $criteria->filter($builder, $filter, $query, $schema);
                }
            }
        }

        return $builder;
    }
}
