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

#ifndef CLI_OPTIONS_H
#define CLI_OPTIONS_H

#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QStringList>

#include <dfm-search/dsearch_global.h>
#include <dfm-search/searchquery.h>
#include <dfm-search/timerangefilter.h>
#include <dfm-search/sizerangefilter.h>

namespace dfmsearch {

/**
 * @brief 命令行搜索参数配置
 *
 * 封装所有命令行解析结果，遵循单一职责原则
 */
struct SearchCliConfig
{
    // Subcommand: if non-empty, the first positional arg is treated as a subcommand
    QString subcommand;   // "" (search), "preview"

    // Basic parameters
    QString keyword;
    QString searchPath;
    SearchType searchType = SearchType::FileName;
    SearchMethod searchMethod = SearchMethod::Indexed;
    SearchQuery::Type queryType = SearchQuery::Type::Simple;

    // 开关选项
    bool caseSensitive = false;
    bool includeHidden = false;
    bool pinyinEnabled = false;
    bool pinyinAcronymEnabled = false;
    bool wildcardEnabled = false;
    bool jsonOutput = false;
    bool verbose = false;   // 详细输出模式

    // 过滤选项
    QStringList fileTypes;
    QStringList fileExtensions;
    QStringList excludedPaths;   // 排除的搜索路径
    int maxResults = 0;   // 0 表示不限制
    int maxPreviewLength = 200;
    int offset = 0;   // 内容偏移值，preview 子命令使用
    bool maxPreviewSet = false;   // --max-preview 是否被显式设置（preview 子命令使用）

    // 文件名搜索选项
    QString filenameKeyword;

    // Semantic mode
    bool semanticMode = false;

    // Time range filtering
    bool hasTimeFilter = false;
    DFMSEARCH::TimeRangeFilter timeFilter;

    // File size range filtering
    bool hasSizeFilter = false;
    DFMSEARCH::SizeRangeFilter sizeFilter;
};

/**
 * @brief 命令行选项管理器
 *
 * 负责定义和解析命令行参数，遵循单一职责原则
 */
class CliOptions
{
public:
    CliOptions();
    ~CliOptions() = default;

    /**
     * @brief 解析命令行参数
     * @param app QCoreApplication实例
     * @param config 输出的配置结构
     * @return 解析成功返回true，失败返回false
     */
    bool parse(QCoreApplication &app, SearchCliConfig &config);

    /**
     * @brief 打印帮助信息
     */
    void printHelp() const;

    /**
     * @brief 返回 --features 输出的静态特性列表（编译时确定，26 项）
     *
     * 提取为静态方法以便单元测试验证列表完整性，无需启动 CLI 进程。
     */
    static QStringList supportedFeatures();

private:
    void setupOptions();
    bool validateConfig(SearchCliConfig &config);
    bool parseTimeOptions(SearchCliConfig &config);

private:
    QCommandLineParser m_parser;

    // 基本选项
    QCommandLineOption m_typeOption;
    QCommandLineOption m_methodOption;
    QCommandLineOption m_queryOption;
    QCommandLineOption m_caseSensitiveOption;
    QCommandLineOption m_includeHiddenOption;
    QCommandLineOption m_pinyinOption;
    QCommandLineOption m_pinyinAcronymOption;
    QCommandLineOption m_fileTypesOption;
    QCommandLineOption m_fileExtensionsOption;
    QCommandLineOption m_excludeOption;
    QCommandLineOption m_maxResultsOption;
    QCommandLineOption m_maxPreviewOption;
    QCommandLineOption m_offsetOption;
    QCommandLineOption m_filenameOption;
    QCommandLineOption m_wildcardOption;
    QCommandLineOption m_jsonOption;
    QCommandLineOption m_verboseOption;
    QCommandLineOption m_semanticOption;

    // Time range filtering options
    QCommandLineOption m_timeFieldOption;
    QCommandLineOption m_timeLastOption;
    QCommandLineOption m_timeTodayOption;
    QCommandLineOption m_timeYesterdayOption;
    QCommandLineOption m_timeThisWeekOption;
    QCommandLineOption m_timeLastWeekOption;
    QCommandLineOption m_timeThisMonthOption;
    QCommandLineOption m_timeLastMonthOption;
    QCommandLineOption m_timeThisYearOption;
    QCommandLineOption m_timeLastYearOption;
    QCommandLineOption m_timeRangeOption;

    // File size range filtering options
    QCommandLineOption m_sizeMinOption;
    QCommandLineOption m_sizeMaxOption;
    QCommandLineOption m_versionOption;
    QCommandLineOption m_featuresOption;
};

}   // namespace dfmsearch

#endif   // CLI_OPTIONS_H
