import type { BrandVertical } from "@/lib/generation/brand-visual-style";

/**
 * Positive, non-hype vocabulary used on-image and in image prompts.
 *
 * Selection is deterministic on the layout seed so a regenerate produces a
 * different-but-stable set rather than random drift. Everything here is
 * outcome language — the banned list in the image prompts still rejects
 * "revolutionary" / "best ever" style slogan dumps.
 */

/** Outcome adjectives — safe for any B2B or consumer service. */
const UNIVERSAL_POWER_WORDS = [
  "Effortless",
  "Reliable",
  "Instant",
  "Secure",
  "Transparent",
  "Precise",
  "Seamless",
  "Proven",
  "Responsive",
  "Scalable",
] as const;

/** Short benefit chips — pair with a service label on the banner. */
const UNIVERSAL_BENEFIT_CHIPS = [
  "Zero guesswork",
  "Built to scale",
  "Always on",
  "Fully tracked",
  "Set up in minutes",
  "No busywork",
] as const;

const VERTICAL_POWER_WORDS: Partial<Record<BrandVertical, readonly string[]>> = {
  sms: ["Instant", "Delivered", "Two-way", "Opt-in safe"],
  fax: ["Encrypted", "Audit-ready", "Paperless", "Compliant"],
  email: ["Inbox-ready", "Deliverable", "Personalised", "Measurable"],
};

const VERTICAL_BENEFIT_CHIPS: Partial<Record<BrandVertical, readonly string[]>> = {
  sms: ["Delivered in seconds", "Replies in one thread"],
  fax: ["Encrypted end to end", "Every send logged"],
  email: ["Lands in the inbox", "Opens you can prove"],
};

/** Stable index rotation so repeat generations vary without going random. */
function rotate<T>(pool: readonly T[], seed: number, count: number): T[] {
  if (!pool.length) return [];
  const start = Math.abs(seed) % pool.length;
  const out: T[] = [];
  for (let i = 0; i < Math.min(count, pool.length); i += 1) {
    out.push(pool[(start + i) % pool.length]!);
  }
  return out;
}

function dedupe(values: string[]): string[] {
  const seen = new Set<string>();
  return values.filter((v) => {
    const key = v.toLowerCase();
    if (seen.has(key)) return false;
    seen.add(key);
    return true;
  });
}

export interface PowerWordInput {
  seed: number;
  vertical?: BrandVertical | null;
  /** Active service / solution name — anchors the vocabulary to what is sold. */
  service?: string | null;
  count?: number;
}

/** Single-word outcome adjectives, vertical-specific first. */
export function pickPowerWords(input: PowerWordInput): string[] {
  const count = input.count ?? 3;
  const vertical = input.vertical ? VERTICAL_POWER_WORDS[input.vertical] : undefined;
  const merged = dedupe([
    ...rotate(vertical ?? [], input.seed, 2),
    ...rotate(UNIVERSAL_POWER_WORDS, input.seed, count + 2),
  ]);
  return merged.slice(0, count);
}

/** Short benefit phrases for chips/badges under the headline. */
export function pickBenefitChips(input: PowerWordInput): string[] {
  const count = input.count ?? 2;
  const vertical = input.vertical ? VERTICAL_BENEFIT_CHIPS[input.vertical] : undefined;
  const merged = dedupe([
    ...rotate(vertical ?? [], input.seed, 1),
    ...rotate(UNIVERSAL_BENEFIT_CHIPS, input.seed, count + 1),
  ]);
  return merged.slice(0, count);
}

/**
 * Prompt block binding brand mark, service, and approved vocabulary together
 * so the model reinforces the offer instead of inventing generic copy.
 */
export function formatPowerWordsForPrompt(input: PowerWordInput & { companyName?: string }): string {
  const words = pickPowerWords(input);
  const chips = pickBenefitChips(input);
  const service = input.service?.trim();

  return [
    "## Brand vocabulary lock",
    input.companyName ? `- Brand: ${input.companyName}` : "",
    service ? `- Active service (must be visually evident): ${service}` : "",
    words.length ? `- Approved tone words: ${words.join(", ")}` : "",
    chips.length ? `- Approved benefit phrases: ${chips.join(" · ")}` : "",
    "- Express these through the scene, not as rendered text.",
    "- Never introduce superlatives such as revolutionary, world-class, or best-ever.",
  ]
    .filter(Boolean)
    .join("\n");
}
