<?php
declare(strict_types=1);

function build_documents_where(array $filters): array {
    $where = ['status != "deleted"'];
    $params = [];

    if (!empty($filters['q'])) {
        $where[] = '(title LIKE :q OR filename LIKE :q OR doc_type LIKE :q)';
        $params[':q'] = '%' . $filters['q'] . '%';
    }
    if (!empty($filters['doc_type'])) {
        $where[] = 'doc_type = :doc_type';
        $params[':doc_type'] = $filters['doc_type'];
    }
    if (!empty($filters['status'])) {
        $where[] = 'status = :status';
        $params[':status'] = $filters['status'];
    }
    if (!empty($filters['created_from'])) {
        $where[] = 'created_at >= :created_from';
        $params[':created_from'] = $filters['created_from'] . ' 00:00:00';
    }
    if (!empty($filters['created_to'])) {
        $where[] = 'created_at <= :created_to';
        $params[':created_to'] = $filters['created_to'] . ' 23:59:59';
    }
    return [' WHERE ' . implode(' AND ', $where), $params];
}
