import type { CreativeIntelligence, EngagementIntelligence } from "./types";

const HUMAN_WRITING_DNA = {
  principles: [
    "Write naturally — mix sentence lengths",
    "Use contractions where a human would",
    "Show before telling",
    "Tell one useful story per post",
    "Respect reader intelligence",
    "Leave the reader with one clear idea",
    "Use storytelling, not bullet dumps in captions",
  ],
  avoid: [
    "AI clichés (delve, landscape, game-changer, unlock, leverage)",
    "Generic marketing phrases",
    "Unnecessary adjectives",
    "Repeating the same word in adjacent sentences",
    "Invented metrics or client names",
    "Exaggeration or hype",
  ],
};

const AUTHENTICITY_RULES = [
  "Ground every claim in the creative brief Problem and Impact",
  "Match brand voice without sounding robotic",
  "Emotion must align with the story beat — hope, loss, faith, community, or irony",
  "Same message identity across LinkedIn, Facebook, Instagram, and YouTube",
];

function engagementForBeatIndex(beatIndex: number): EngagementIntelligence {
  const types = ["awareness", "discussion", "trust", "lead", "reflection"] as const;
  const conversations = ["opinion", "experience", "learning", "debate", "prediction"] as const;
  const idx = beatIndex % 5;

  return {
    desiredResults: [types[idx]!, "trust", "comment"],
    conversationType: conversations[idx]!,
    primaryCta:
      idx === 4
        ? "Invite honest reflection — realistic expectations over blame"
        : "Invite comments with the engagement question from the brief",
  };
}

export function buildCreativeIntelligence(beatIndex: number, emotions: string[]): CreativeIntelligence {
  return {
    humanWriting: HUMAN_WRITING_DNA,
    emotions,
    engagement: engagementForBeatIndex(beatIndex),
    authenticity: AUTHENTICITY_RULES,
  };
}

export function formatCreativeIntelligenceMarkdown(creative: CreativeIntelligence): string {
  return [
    "## Creative Intelligence",
    "### Human Writing DNA",
    ...creative.humanWriting.principles.map((p) => `- ${p}`),
    "### Avoid",
    ...creative.humanWriting.avoid.map((a) => `- ${a}`),
    "### Emotions for this beat",
    creative.emotions.join(", "),
    "### Engagement",
    `Desired results: ${creative.engagement.desiredResults.join(", ")}`,
    `Conversation type: ${creative.engagement.conversationType}`,
    `Primary CTA: ${creative.engagement.primaryCta}`,
    "### Authenticity",
    ...creative.authenticity.map((r) => `- ${r}`),
  ].join("\n");
}
