// src/lib/keywords/gsc-positioning.ts — pure GSC positioning helpers.
import type { GscTracking } from "./types";

export type GscBand = "top10" | "quickwin" | "beyond" | "none";

/** Classify a keyword's GSC positioning into a band. */
export function gscBand(gsc: GscTracking | null | undefined): GscBand {
  if (!gsc) return "none";
  if (gsc.position <= 10) return "top10";
  if (gsc.position <= 20) return "quickwin";
  return "beyond";
}

/** Striking-distance opportunity: rewards impressions AND proximity to page 1.
 *  Non-quick-win rows score 0 so they sink in the opportunity sort. */
export function opportunityScore(gsc: GscTracking | null | undefined): number {
  if (!gsc || gscBand(gsc) !== "quickwin") return 0;
  return gsc.impressions * (21 - gsc.position);
}

export type PositionBucket =
  | "top3"
  | "top10"
  | "quickwin"
  | "deep"
  | "far"
  | "none";

/** Finer position bucketing for the overview distribution. `quickwin` (11–20)
 *  matches gscBand's quick-win band, so counts stay consistent. */
export function positionBucket(
  gsc: GscTracking | null | undefined,
): PositionBucket {
  if (!gsc) return "none";
  if (gsc.position <= 3) return "top3";
  if (gsc.position <= 10) return "top10";
  if (gsc.position <= 20) return "quickwin";
  if (gsc.position <= 50) return "deep";
  return "far";
}
