import type { Context } from "hono";
import { processBeloQuote } from "../belo.service";
import { processBlueQuote } from "../blue.service";
import { processBnaQuote } from "../bna.service";
import { logger } from "../../../lib/logger";
import { eventBus } from "../../../lib/event-bus";

export async function fetchQuotes(c: Context) {
  const results: { type: string; status: string; error?: string }[] = [];

  for (const [name, fn] of [["BELO", processBeloQuote], ["BLUE", processBlueQuote], ["BNA", processBnaQuote]] as const) {
    try {
      await fn();
      results.push({ type: name, status: "ok" });
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error);
      logger.error(error, `Error fetching ${name} quote via endpoint`);
      results.push({ type: name, status: "error", error: message });
    }
  }

  eventBus.emit("quotes-updated");

  return c.json(results);
}
