import type { StoryAct } from "./context-library";

/**
 * Configurable novel/storytelling structure — narrative framework only.
 * No characters, names, events, or fictional facts from any specific work.
 * The business supplies the real-world subject.
 */
export const DEFAULT_STORYTELLING_FRAMEWORK_ID = "customer-journey-v1";

export type StoryFrameworkRole =
  | "situation"
  | "character"
  | "problem"
  | "conflict"
  | "impact"
  | "attempt"
  | "insight"
  | "solution"
  | "transformation"
  | "resolution";

export interface StoryFrameworkBeat {
  index: number;
  role: StoryFrameworkRole;
  title: string;
  /** How this story beat maps onto real business/customer facts. */
  customerMapping: string;
  structureGuidance: string;
  emotions: string[];
  visualMood: string;
  lesson: string;
  contentAct: StoryAct;
}

export const STORYTELLING_FRAMEWORK: StoryFrameworkBeat[] = [
  {
    index: 0,
    role: "situation",
    title: "Situation",
    customerMapping: "Customer's real-world situation",
    structureGuidance:
      "Open in the customer's current world using only verified business context — setting, stakes, and everyday work.",
    emotions: ["recognition", "familiarity"],
    visualMood: "Everyday professional setting, grounded and specific",
    lesson: "Start where the customer already is.",
    contentAct: "challenge",
  },
  {
    index: 1,
    role: "character",
    title: "Character / Customer",
    customerMapping: "Customer",
    structureGuidance:
      "Name the real audience (roles, industries, regions) from brand or website. Do not invent a fictional protagonist.",
    emotions: ["empathy", "relevance"],
    visualMood: "Credible professionals in context",
    lesson: "The customer is the character.",
    contentAct: "challenge",
  },
  {
    index: 2,
    role: "problem",
    title: "Problem",
    customerMapping: "Real customer or business problem",
    structureGuidance:
      "State a problem that follows from the company's actual audience and offer. If unknown, say so and stay educational.",
    emotions: ["concern", "clarity"],
    visualMood: "Thoughtful tension without melodrama",
    lesson: "A specific problem beats a generic slogan.",
    contentAct: "challenge",
  },
  {
    index: 3,
    role: "conflict",
    title: "Conflict",
    customerMapping: "Real challenge",
    structureGuidance:
      "Show why the problem persists — process, tools, incentives, or market pressure drawn from verified context.",
    emotions: ["urgency", "frustration"],
    visualMood: "Contrast between old friction and needed change",
    lesson: "Conflict is the gap between need and current capability.",
    contentAct: "recognition",
  },
  {
    index: 4,
    role: "impact",
    title: "Impact",
    customerMapping: "Business or customer impact",
    structureGuidance:
      "Describe operational or customer cost in general, educational terms. Do not invent metrics or named results.",
    emotions: ["consequence", "stakes"],
    visualMood: "Serious, credible business realism",
    lesson: "Impact must stay hypothetical unless proof is verified.",
    contentAct: "recognition",
  },
  {
    index: 5,
    role: "attempt",
    title: "Attempt",
    customerMapping: "Common approaches that fall short",
    structureGuidance:
      "Cover typical workarounds or mistakes relevant to this industry — without claiming they happened to a named client.",
    emotions: ["recognition", "caution"],
    visualMood: "Busy but ineffective process",
    lesson: "Show the attempt before the insight.",
    contentAct: "recognition",
  },
  {
    index: 6,
    role: "insight",
    title: "Insight",
    customerMapping: "Educational reframe grounded in the offer",
    structureGuidance:
      "Teach one useful idea connected to what the company actually provides. No hype, no invented research.",
    emotions: ["clarity", "curiosity"],
    visualMood: "Light-bulb professionalism — calm, instructive",
    lesson: "Insight educates; it does not invent proof.",
    contentAct: "solution",
  },
  {
    index: 7,
    role: "solution",
    title: "Solution",
    customerMapping: "Verified solution or capability",
    structureGuidance:
      "Connect only products, services, or features present in the Business Context. If a capability is unknown, do not invent it.",
    emotions: ["confidence", "trust"],
    visualMood: "Focused product or service in real use",
    lesson: "The solution must be a verified capability.",
    contentAct: "solution",
  },
  {
    index: 8,
    role: "transformation",
    title: "Transformation",
    customerMapping: "Verified or appropriately framed outcome",
    structureGuidance:
      "If verified claims exist, use them. Otherwise frame outcomes as typical, educational, or hypothetical — never as fake case studies.",
    emotions: ["relief", "momentum"],
    visualMood: "Collaborative, results-oriented, human",
    lesson: "Unverified results stay hypothetical.",
    contentAct: "proof",
  },
  {
    index: 9,
    role: "resolution",
    title: "Resolution / CTA",
    customerMapping: "Clear next step aligned to the brand",
    structureGuidance:
      "Close with a CTA that matches contact details, website, and offer. Do not invent phone, email, or guarantees.",
    emotions: ["resolve", "invitation"],
    visualMood: "Calm confidence, clear next step",
    lesson: "Resolution is a real action the company can support.",
    contentAct: "path_forward",
  },
];

export interface ContentSequenceBeatTemplate {
  id: string;
  title: string;
  summary: string;
  tag: string;
  frameworkRole: StoryFrameworkRole;
}

/** Default connected sequence — configurable; grounded in Business Context at generation time. */
export const DEFAULT_CONTENT_SEQUENCE: ContentSequenceBeatTemplate[] = [
  {
    id: "seq-1",
    title: "Problem introduction",
    summary: "Introduce a real customer problem from the Business Context.",
    tag: "Problem",
    frameworkRole: "problem",
  },
  {
    id: "seq-2",
    title: "Why the problem exists",
    summary: "Explain the underlying challenge without inventing market history.",
    tag: "Conflict",
    frameworkRole: "conflict",
  },
  {
    id: "seq-3",
    title: "Impact of the problem",
    summary: "Show business or customer impact in educational language unless proof is verified.",
    tag: "Impact",
    frameworkRole: "impact",
  },
  {
    id: "seq-4",
    title: "Realistic customer scenario",
    summary: "A plausible scenario for the stated audience — not a fake case study.",
    tag: "Customer",
    frameworkRole: "character",
  },
  {
    id: "seq-5",
    title: "Common mistakes",
    summary: "Typical attempts that miss the mark in this space.",
    tag: "Attempt",
    frameworkRole: "attempt",
  },
  {
    id: "seq-6",
    title: "Educational insight",
    summary: "One useful idea tied to what the company actually does.",
    tag: "Insight",
    frameworkRole: "insight",
  },
  {
    id: "seq-7",
    title: "Relevant solution",
    summary: "The verified product or service that addresses the problem.",
    tag: "Solution",
    frameworkRole: "solution",
  },
  {
    id: "seq-8",
    title: "How the solution works",
    summary: "Explain the capability using only listed features and services.",
    tag: "How it works",
    frameworkRole: "situation",
  },
  {
    id: "seq-9",
    title: "Proof / differentiation",
    summary: "Verified claims, or a framed differentiator — never fabricated results.",
    tag: "Proof",
    frameworkRole: "transformation",
  },
  {
    id: "seq-10",
    title: "Call to action",
    summary: "Resolution with a CTA using real contact and website details.",
    tag: "CTA",
    frameworkRole: "resolution",
  },
];

export function getStoryFrameworkBeat(index: number): StoryFrameworkBeat {
  const len = STORYTELLING_FRAMEWORK.length;
  const normalized = ((index % len) + len) % len;
  return STORYTELLING_FRAMEWORK[normalized]!;
}

export function getSequenceTemplate(index: number): ContentSequenceBeatTemplate {
  const len = DEFAULT_CONTENT_SEQUENCE.length;
  const normalized = ((index % len) + len) % len;
  return DEFAULT_CONTENT_SEQUENCE[normalized]!;
}

export function getFrameworkBeatForSequenceIndex(index: number): StoryFrameworkBeat {
  const template = getSequenceTemplate(index);
  return (
    STORYTELLING_FRAMEWORK.find((b) => b.role === template.frameworkRole) ??
    getStoryFrameworkBeat(index)
  );
}
