import {
  TemplateReceiverGroup,
  TemplateSampleData,
} from "@/user-management/services/TemplateConfiguration/types/templateTypes";

const toNonSmartReceivers = (names: string[]) =>
  names.map((name) => ({ type: "non_smart" as const, name }));

const buildReceiverGroup = (
  prefix: string,
  suffix: string,
  names: string[],
): TemplateReceiverGroup => ({
  prefix,
  suffix,
  receivers: toNonSmartReceivers(names),
});

const SAMPLE_CONTENT_EN = {
  groupedReceivers: [
    {
      prefix: "RE:",
      items: ["Department Head", "Project Manager", "Team Lead"],
      suffix: "---",
    },
  ],
  receiverList: [
    buildReceiverGroup(
      "RE:",
      "---",
      ["Department Head", "Project Manager", "Team Lead"],
    ),
  ],
  groupedCCReceivers: [
    {
      items: ["Finance Department", "HR Department"],
      suffix: "---",
    },
  ],
  groupedForYourReferenceReceivers: [
    {
      prefix: "For your information:",
      items: ["Archive Office"],
      suffix: "---",
    },
  ],
  forYourReferenceList: [
    buildReceiverGroup("For your information:", "---", ["Archive Office"]),
  ],
  content: {
    isWithDelegateSignature: false,
    delegatorName: "",
    body: `This is a sample letter body demonstrating the template styling.

The body can contain multiple paragraphs and formatting.

- First bullet point item
- Second bullet point item
- Third bullet point item

This allows you to see how list styles are applied.`,
    date: new Date().toISOString().split("T")[0],
    internalCC: ["Finance Department", "HR Department", "Legal Department"],
    externalCC: ["External Partner A", "External Partner B"],
    prefixCC: "CC:",
    suffixCC: "---",
    from: ["John Doe", "Mary Smith"],
    subject: "Sample Template Subject – Style Preview",
    sincerelyText: "Sincerely",
    to: ["Department Head", "Project Manager", "Team Lead"],
    prefix: "RE:",
    suffix: "---",
    forYourReferenceCC: ["Archive Office"],
  },
} as const;

const RECEIVER_NAMES_AM = ["ክፍል ኃላፊ", "የፕሮጀክት ሥራ አስኪያጅ", "የቡድን መሪ"];
const SAMPLE_SUFFIX_AM = "አዲስ አበባ";
const SAMPLE_FYR_PREFIX_AM = "እንዲያውቁት:";

const SAMPLE_CONTENT_AM = {
  groupedReceivers: [
    {
      prefix: "RE:",
      items: RECEIVER_NAMES_AM,
      suffix: SAMPLE_SUFFIX_AM,
    },
  ],
  receiverList: [
    buildReceiverGroup(
      "የኢትዮጵያ ፌዴራላዊ ዲሞክራሲያዊ ሪፐብሊክ",
      SAMPLE_SUFFIX_AM,
      RECEIVER_NAMES_AM,
    ),
  ],
  groupedCCReceivers: [
    {
      items: ["የፋይናንስ ክፍል", "የሰው ሀብት ክፍል"],
      suffix: SAMPLE_SUFFIX_AM,
    },
  ],
  groupedForYourReferenceReceivers: [
    {
      prefix: SAMPLE_FYR_PREFIX_AM,
      items: ["የመዝገብ ቤት"],
      suffix: SAMPLE_SUFFIX_AM,
    },
  ],
  forYourReferenceList: [
    buildReceiverGroup(SAMPLE_FYR_PREFIX_AM, SAMPLE_SUFFIX_AM, ["የመዝገብ ቤት"]),
  ],
  content: {
    isWithDelegateSignature: false,
    delegatorName: "",
    body: `ይህ የአብነት ደብዳቤ ይዘት የቅጥ ማቀናበሪያውን ለማሳየት የተዘጋጀ ነው።

ይህ ደብዳቤ በብዙ አንቀጽ እና ቅርጸት ሊቀርጽ ይችላል።

- የመጀመሪያው የነጥብ ዝርዝር
- የሁለተኛው የነጥብ ዝርዝር
- የሦስተኛው የነጥብ ዝርዝር

ይህም የዝርዝር ቅጦች እንዴት እንደሚታዩ ያሳያል።`,
    date: new Date().toISOString().split("T")[0],
    internalCC: ["የፋይናንስ ክፍል", "የሰው ሀብት ክፍል", "የህጋዊ ጉዳዮች ክፍል"],
    externalCC: ["የውጭ አጋር ድርጅት አ", "የውጭ አጋር ድርጅት ቢ"],
    prefixCC: "CC:",
    suffixCC: SAMPLE_SUFFIX_AM,
    from: ["ዮሃንስ ዶ", "ማርያም ስሚዝ"],
    subject: "የአብነት ደብዳቤ ጉዳይ - የቅጥ ቅድመ እይታ",
    sincerelyText: "በአክብሮት",
    to: RECEIVER_NAMES_AM,
    prefix: "የኢትዮጵያ ፌዴራላዊ ዲሞክራሲያዊ ሪፐብሊክ",
    suffix: SAMPLE_SUFFIX_AM,
    forYourReferenceCC: ["የመዝገብ ቤት"],
  },
} as const;

const SHARED_SAMPLE_DATA = {
  collaborators: [
    { am: "ዮሃንስ ዶ", en: "John Doe" },
    { am: "ማርያም ስሚዝ", en: "Mary Smith" },
  ],
  referenceNumber: "REF-2026/001",
  recordType: "external" as const,
};

const cloneGroupedItems = <T extends { items: readonly string[] }>(group: T) => ({
  ...group,
  items: [...group.items],
});

/**
 * Sample payload for style-settings PDF preview, localized by preview language.
 */
export function getDefaultSampleData(
  preferredLanguage: "am" | "en" = "am",
): Omit<TemplateSampleData, "unitId"> {
  const localized =
    preferredLanguage === "en" ? SAMPLE_CONTENT_EN : SAMPLE_CONTENT_AM;

  return {
    ...SHARED_SAMPLE_DATA,
    preferredLanguage,
    ...localized,
    content: {
      ...localized.content,
      internalCC: [...localized.content.internalCC],
      externalCC: [...localized.content.externalCC],
      from: [...localized.content.from],
      to: [...localized.content.to],
      forYourReferenceCC: localized.content.forYourReferenceCC
        ? [...localized.content.forYourReferenceCC]
        : undefined,
    },
    groupedReceivers: localized.groupedReceivers.map(cloneGroupedItems),
    groupedCCReceivers: localized.groupedCCReceivers.map(cloneGroupedItems),
    groupedForYourReferenceReceivers:
      localized.groupedForYourReferenceReceivers.map(cloneGroupedItems),
    receiverList: localized.receiverList.map((group) => ({
      ...group,
      receivers: group.receivers.map((receiver) => ({ ...receiver })),
    })),
    forYourReferenceList: localized.forYourReferenceList.map((group) => ({
      ...group,
      receivers: group.receivers.map((receiver) => ({ ...receiver })),
    })),
  };
}

/**
 * Default sample data (Amharic). Prefer `getDefaultSampleData(previewLanguage)`
 * when generating previews so English content is included.
 */
export const DEFAULT_SAMPLE_DATA: Omit<TemplateSampleData, "unitId"> =
  getDefaultSampleData("am");
