"use client";

import type { Block, SectionBlockProps, TextBlockProps } from "@/features/editor/state/editorStore";

export function createTestimonialsTemplateBlocks(opts: {
  sectionId: string;
  createId: () => string;
}): { blocks: Block[]; lastSelectedId: string } {
  const { sectionId, createId } = opts;

  const columns: SectionBlockProps["columns"] = [
    { id: `${sectionId}_col_1`, span: 4 },
    { id: `${sectionId}_col_2`, span: 4 },
    { id: `${sectionId}_col_3`, span: 4 },
  ];

  const sectionProps: SectionBlockProps = {
    background: "default",
    paddingY: "lg",
    // 후기 섹션은 중간 폭과 적당한 카드 간격을 사용한다.
    paddingYPx: 80,
    maxWidthPx: 1040,
    gapXPx: 36,
    backgroundColorCustom: "#020617",
    columns,
  } as any;

  const sectionBlock: Block = {
    id: sectionId,
    type: "section",
    props: sectionProps,
    sectionId: null,
    columnId: null,
  };

  const testimonialDefinitions: Array<{ body: string; author: string }> = [
    { body: "이 서비스 덕분에 랜딩 페이지 제작 시간이 크게 줄었습니다.", author: "홍길동" },
    { body: "디자인을 잘 못해도 깔끔한 페이지를 만들 수 있어요.", author: "김영희" },
    { body: "팀 전체가 만족하는 빌더입니다.", author: "이철수" },
  ];

  const testimonialBlocks: Block[] = columns.flatMap((col, index) => {
    const t = testimonialDefinitions[index] ?? testimonialDefinitions[0];

    const bodyId = createId();
    const authorId = createId();

    const bodyProps: TextBlockProps = {
      text: t.body,
      align: "left",
      size: "base",
    } as any;

    const authorProps: TextBlockProps = {
      text: `- ${t.author}`,
      align: "left",
      size: "sm",
    } as any;

    const bodyBlock: Block = {
      id: bodyId,
      type: "text",
      props: bodyProps,
      sectionId,
      columnId: col.id,
    };

    const authorBlock: Block = {
      id: authorId,
      type: "text",
      props: authorProps,
      sectionId,
      columnId: col.id,
    };

    return [bodyBlock, authorBlock];
  });

  const blocks: Block[] = [sectionBlock, ...testimonialBlocks];
  const lastSelectedId = testimonialBlocks[testimonialBlocks.length - 1]?.id ?? sectionId;

  return { blocks, lastSelectedId };
}
