import { COMBO_NAMESPACE } from "../../combos/identifiers";
import type { CatalogModel, RawEntry } from "./parsing";

/**
 * Authorship of OpenCodex-generated routed catalog rows (#855, #7229).
 *
 * A generated routed row normally carries the description prefix below, and that prefix is what
 * removal, template selection, auto-review and residue detection have always recognized. A combo
 * may replace its row description (#7229), so such a row carries a stable marker instead. Rows
 * with the generated description stay byte-identical and keep prefix recognition; only an exact
 * known marker value confers authorship.
 */
export const ROUTED_DESCRIPTION_PREFIX = "Routed via opencodex → ";
export const ROUTED_ORIGIN_FIELD = "opencodex_routed_origin";
export const COMBO_ROUTED_ORIGIN = "combo-v1";
const COMBO_DESCRIPTION_PREFIX = `${ROUTED_DESCRIPTION_PREFIX}${COMBO_NAMESPACE} (`;

function descriptionStartsWith(entry: RawEntry, prefix: string): boolean {
  return typeof entry.description === "string" && entry.description.startsWith(prefix);
}

/** True for a row whose description was overridden by a combo and stamped by OpenCodex. */
export function hasComboRoutedOriginMarker(entry: RawEntry): boolean {
  return entry[ROUTED_ORIGIN_FIELD] === COMBO_ROUTED_ORIGIN;
}

/** Generated by OpenCodex routing: the stable marker, or the legacy generated description. */
export function hasOcxRoutedAuthorship(entry: RawEntry): boolean {
  return hasComboRoutedOriginMarker(entry) || descriptionStartsWith(entry, ROUTED_DESCRIPTION_PREFIX);
}

/** Generated for a combo: the stable marker, or the legacy generated combo description. */
export function hasOcxComboAuthorship(entry: RawEntry): boolean {
  return hasComboRoutedOriginMarker(entry) || descriptionStartsWith(entry, COMBO_DESCRIPTION_PREFIX);
}

/** The operator's combo description override, if this routed model has one. */
export function comboDescriptionOverride(model: CatalogModel | undefined): string | undefined {
  if (model?.provider !== COMBO_NAMESPACE) return undefined;
  const description = typeof model.description === "string" ? model.description.trim() : "";
  return description || undefined;
}

/**
 * Set a routed row's description and its authorship marker together. A template clone may carry
 * another row's marker, so the marker is always rewritten from this model's own override.
 */
export function applyRoutedDescription(entry: RawEntry, generated: string, model: CatalogModel | undefined): void {
  const override = comboDescriptionOverride(model);
  entry.description = override ?? generated;
  if (override !== undefined) entry[ROUTED_ORIGIN_FIELD] = COMBO_ROUTED_ORIGIN;
  else delete entry[ROUTED_ORIGIN_FIELD];
}

/**
 * A combo row retained from the previous catalog (its fresh derivation failed, e.g. a member
 * provider is temporarily unavailable) bypasses deriveEntry. Re-project the combo's CURRENT
 * description onto it so clearing or changing the description in config still reaches the
 * picker. A cleared override returns the row to the generated combo text without the marker;
 * an unmarked row without a current override is left exactly as it was.
 */
export function reapplyRetainedComboDescription(entry: RawEntry, current: string | undefined): void {
  const description = current?.trim();
  if (description) {
    entry.description = description;
    entry[ROUTED_ORIGIN_FIELD] = COMBO_ROUTED_ORIGIN;
  } else if (hasComboRoutedOriginMarker(entry)) {
    entry.description = `${ROUTED_DESCRIPTION_PREFIX}${COMBO_NAMESPACE} (${COMBO_NAMESPACE}).`;
    delete entry[ROUTED_ORIGIN_FIELD];
  }
}
