import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { render, screen, cleanup } from "@testing-library/react";
import type { Block, ProjectConfig } from "@/features/editor/state/editorStore";
import EditorPage from "@/app/editor/page";

let mockState: any;

beforeEach(() => {
  const baseProjectConfig: ProjectConfig = {
    title: "텍스트 블록 스타일 테스트",
    slug: "editor-text-style-test",
    canvasPreset: "full",
    canvasWidthPx: 1024,
    canvasBgColorHex: "#0f172a",
    bodyBgColorHex: "#020617",
  } as ProjectConfig;

  mockState = {
    blocks: [] as Block[],
    projectConfig: baseProjectConfig,
    selectedBlockId: null as string | null,
    selectedListItemId: null as string | null,
    undo: vi.fn(),
    redo: vi.fn(),
    removeBlock: vi.fn(),
    duplicateBlock: vi.fn(),
    selectBlock: vi.fn(),
    selectListItem: vi.fn(),
    addTextBlock: vi.fn(),
    addButtonBlock: vi.fn(),
    addImageBlock: vi.fn(),
    addDividerBlock: vi.fn(),
    addListBlock: vi.fn(),
    addSectionBlock: vi.fn(),
    addFormBlock: vi.fn(),
    addFormInputBlock: vi.fn(),
    addFormSelectBlock: vi.fn(),
    addFormCheckboxBlock: vi.fn(),
    addFormRadioBlock: vi.fn(),
    addHeroTemplateSection: vi.fn(),
    addFeaturesTemplateSection: vi.fn(),
    addCtaTemplateSection: vi.fn(),
    addFaqTemplateSection: vi.fn(),
    addPricingTemplateSection: vi.fn(),
    addTestimonialsTemplateSection: vi.fn(),
    addBlogTemplateSection: vi.fn(),
    addTeamTemplateSection: vi.fn(),
    addFooterTemplateSection: vi.fn(),
    updateBlock: vi.fn(),
    replaceBlocks: vi.fn(),
    reorderBlocks: vi.fn(),
    moveBlock: vi.fn(),
  };
});

afterEach(() => {
  cleanup();
});

vi.mock("@/features/editor/state/editorStore", () => {
  const useEditorStore = (selector: (state: any) => any) => selector(mockState);
  (useEditorStore as any).getState = () => mockState;

  return {
    __esModule: true,
    useEditorStore,
  };
});

vi.mock("next/link", () => {
  return {
    __esModule: true,
    default: ({ href, children }: any) => <a href={href}>{children}</a>,
  };
});

vi.mock("next/navigation", () => {
  return {
    __esModule: true,
    useRouter: () => ({
      push: vi.fn(),
    }),
    useSearchParams: () => ({
      get: () => null,
    }),
  };
});

describe("EditorPage - 텍스트 블록 스타일", () => {
  it("colorCustom / backgroundColorCustom / maxWidthCustom 이 에디터 텍스트 렌더에 반영되어야 한다", () => {
    const blocks: Block[] = [
      {
        id: "txt_style_1",
        type: "text",
        props: {
          text: "스타일 테스트 텍스트",
          align: "center",
          size: "base",
          colorCustom: "#ff0000",
          backgroundColorCustom: "#123456",
          maxWidthCustom: "320px",
        },
      } as any,
    ];

    mockState.blocks = blocks;
    mockState.selectedBlockId = "txt_style_1";

    render(<EditorPage />);

    const blockEl = screen.getByTestId("editor-block") as HTMLElement;
    const textEl = blockEl.querySelector("div.whitespace-pre-wrap") as HTMLElement;
    expect(textEl).toBeTruthy();

    // 텍스트 색상: #ff0000 → rgb(255, 0, 0)
    expect(textEl.style.color).toBe("rgb(255, 0, 0)");
    // 배경색: #123456 → rgb(18, 52, 86)
    expect(textEl.style.backgroundColor).toBe("rgb(18, 52, 86)");
    // 최대 너비: maxWidthCustom 이 style.maxWidth 로 반영되어야 한다.
    expect(textEl.style.maxWidth).toBe("320px");

    // 정렬 클래스는 pb-text-center 로 유지되어야 한다.
    expect(blockEl.className).toContain("pb-text-center");
  });
});
