// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef TIMERANGEUTILS_H
#define TIMERANGEUTILS_H

#include <QDateTime>

#include <lucene++/LuceneHeaders.h>

#include <dfm-search/dsearch_global.h>

DFM_SEARCH_BEGIN_NS

class TimeRangeFilter;

/**
 * @brief TimeRangeUtils provides utility functions for time range operations
 */
namespace TimeRangeUtils {

/**
 * @brief Convert QDateTime to Unix timestamp in seconds
 * @param dt The datetime to convert
 * @return Unix timestamp in seconds (0 if invalid)
 */
qint64 toEpochSecs(const QDateTime &dt);

/**
 * @brief Build a Lucene NumericRangeQuery for time range filtering
 * @param fieldName The field name to query (e.g., L"birth_time", L"modify_time")
 * @param startEpoch The start timestamp (in seconds), use 0 for no lower bound
 * @param endEpoch The end timestamp (in seconds), use INT64_MAX for no upper bound
 * @param includeLower Whether to include the lower bound
 * @param includeUpper Whether to include the upper bound
 * @return A NumericRangeQuery pointer, or nullptr if range is invalid
 */
Lucene::QueryPtr buildNumericRangeQuery(
        const wchar_t *fieldName,
        qint64 startEpoch,
        qint64 endEpoch,
        bool includeLower,
        bool includeUpper);

/**
 * @brief Build a Lucene query for time range filtering, supporting TimeField::Both
 *
 * When filter.timeField() is TimeField::Both, this builds a BooleanQuery with SHOULD
 * clauses for both birth_time and modify_time fields. Otherwise, it builds a single
 * NumericRangeQuery for the specified field.
 *
 * The returned query is designed to be added to an outer query with BooleanClause::MUST.
 *
 * @param filter The time range filter containing field selection and range
 * @param birthTimeField The Lucene field name for birth time
 * @param modifyTimeField The Lucene field name for modification time
 * @return A Lucene query (single NumericRangeQuery or BooleanQuery for Both), or nullptr if invalid
 */
Lucene::QueryPtr buildTimeRangeFilterQuery(
        const TimeRangeFilter &filter,
        const wchar_t *birthTimeField,
        const wchar_t *modifyTimeField);

}   // namespace TimeRangeUtils

DFM_SEARCH_END_NS

#endif   // TIMERANGEUTILS_H
