<?php

if (!defined('ABSPATH')) {
    exit;
}

class ACC_Runtime_Query_Engine {

    public static function build_where(array $filters): array {

        global $wpdb;

        $where = [];
        $values = [];

        foreach ($filters as $field => $value) {

            $field = sanitize_key($field);

            if (is_array($value)) {

                if (isset($value['from'])) {
                    $where[] = "{$field} >= %s";
                    $values[] = $value['from'];
                }

                if (isset($value['to'])) {
                    $where[] = "{$field} <= %s";
                    $values[] = $value['to'];
                }

            } else {

                $where[] = "{$field} = %s";
                $values[] = $value;

            }
        }

        $sql = '1=1';

        if ($where) {
            $sql .= ' AND ' . implode(' AND ', $where);
        }

        return [
            'sql'=>$sql,
            'values'=>$values
        ];
    }

}
