import type { Mode } from "./types";

export type WriterFieldId =
  | "ai-hero-body"
  | "skills-changelog-body"
  | "newsletter-copy"
  | "video-body"
  | "video-description"
  | "video-script";

export const FIELD_MODES: Record<WriterFieldId, Mode[]> = {
  "ai-hero-body": ["article", "article-plan"],
  "skills-changelog-body": ["article", "article-plan"],
  "newsletter-copy": ["newsletter"],
  "video-body": ["article", "article-plan"],
  "video-description": ["seo-description-document"],
  // No AI generation modes yet — the script is authored manually. Beat→script
  // generation is a follow-up (course-video-manager#1421); the field still gets
  // the full WritableField editing experience today.
  "video-script": [],
};

export const FIELD_LABELS: Record<WriterFieldId, string> = {
  "ai-hero-body": "AI Hero Body",
  "skills-changelog-body": "Skills Changelog Body",
  "newsletter-copy": "Newsletter Copy",
  "video-body": "Lesson Body",
  "video-description": "SEO Description",
  "video-script": "Script",
};

export function getFieldMessagesStorageKey(
  videoId: string,
  fieldId: WriterFieldId,
  mode: Mode
): string {
  return `writer-field-messages-${videoId}-${fieldId}-${mode}`;
}

// Only the conversation is persisted. A field's document is owned by the route
// loader (i.e. the database) and held in memory for the life of the writer —
// see `useDocumentFlow`.

export function loadFieldMessages(
  videoId: string,
  fieldId: WriterFieldId,
  mode: Mode
): unknown[] {
  if (typeof localStorage === "undefined") return [];
  try {
    const saved = localStorage.getItem(
      getFieldMessagesStorageKey(videoId, fieldId, mode)
    );
    return saved ? JSON.parse(saved) : [];
  } catch {
    return [];
  }
}

export function saveFieldMessages(
  videoId: string,
  fieldId: WriterFieldId,
  mode: Mode,
  messages: unknown[]
): void {
  if (typeof localStorage === "undefined") return;
  try {
    localStorage.setItem(
      getFieldMessagesStorageKey(videoId, fieldId, mode),
      JSON.stringify(messages)
    );
  } catch {
    // ignore
  }
}

export function constrainModes(
  modes: Mode[],
  currentMode: Mode
): { mode: Mode; isConstrained: boolean } {
  if (modes.length === 0) return { mode: currentMode, isConstrained: false };
  if (modes.includes(currentMode))
    return { mode: currentMode, isConstrained: true };
  return { mode: modes[0]!, isConstrained: true };
}
