import { executeBraveSearch } from "../brave-execute";
import { getSearchConfig } from "../config";
import type { SearchProvider, SearchRequest, SearchResponse } from "../types";

export class BraveSearchProvider implements SearchProvider {
  name() {
    return "brave" as const;
  }

  enabled(): boolean {
    const cfg = getSearchConfig().brave;
    return cfg.enabled && !!cfg.apiKey;
  }

  async search(req: SearchRequest): Promise<SearchResponse> {
    const cfg = getSearchConfig().brave;
    const executed = await executeBraveSearch({
      keyword: req.keyword,
      limit: req.limit,
      query: req.query,
      priority: req.priority,
      patrolMode: req.patrolMode,
      threadsOnly: req.threadsOnly,
    });

    return {
      provider: "brave",
      results: executed.results,
      status: executed.status,
      skipReason: executed.skipReason,
      quotaUsed: executed.quotaUsed,
      quotaLimit: executed.quotaLimit ?? cfg.dailyLimit,
    };
  }
}