import { refreshSalesOpportunityMetricsCache } from "../cache/salesOpportunityMetricsCache";

const LOG_PREFIX = "[job:salesMetrics]";

/**
 * Execute the sales opportunity metrics cache refresh.
 * This is the handler function registered with PgBoss for the
 * REFRESH_SALES_METRICS queue.
 */
export async function executeSalesMetricsRefresh(opts?: {
  forceColdLoad?: boolean;
}): Promise<void> {
  const start = Date.now();
  console.log(
    `${LOG_PREFIX} refresh started | forceColdLoad=${opts?.forceColdLoad ?? false}`
  );
  try {
    await refreshSalesOpportunityMetricsCache({ forceColdLoad: opts?.forceColdLoad });
    console.log(`${LOG_PREFIX} refresh completed in ${Date.now() - start}ms`);
  } catch (err) {
    console.error(`${LOG_PREFIX} refresh failed in ${Date.now() - start}ms`, err);
    throw err;
  }
}
