import { describe, expect, it, vi } from "vitest";
import {
  createContentPage,
  deleteContentPage,
  getAdminPages,
  getPageBySlug,
  updateContentPage
} from "./contentApi";

vi.mock("@shared/api/client", () => ({
  apiClient: {
    get: vi.fn(async (url: string) => {
      if (url === "/api/v1/content/pages/manage/all") {
        return { data: { data: [{ id: "1", slug: "about", title: "About", body: "", status: "published" }] } };
      }
      return { data: { slug: "about", title: "About" } };
    }),
    post: vi.fn(async () => ({ data: { id: "2", slug: "new", title: "New", body: "", status: "draft" } })),
    patch: vi.fn(async () => ({ data: { id: "1", slug: "about", title: "Updated", body: "", status: "published" } })),
    delete: vi.fn(async () => ({ data: {} }))
  }
}));

describe("contentApi", () => {
  it("loads page by slug", async () => {
    const data = await getPageBySlug("about");
    expect(data.slug).toBe("about");
  });

  it("loads admin pages", async () => {
    const pages = await getAdminPages();
    expect(pages[0].slug).toBe("about");
  });

  it("creates content page", async () => {
    const page = await createContentPage({
      slug: "new",
      title: "New",
      body: "<p>x</p>",
      status: "draft"
    });
    expect(page.slug).toBe("new");
  });

  it("updates content page", async () => {
    const page = await updateContentPage("1", { title: "Updated" });
    expect(page.title).toBe("Updated");
  });

  it("deletes content page", async () => {
    await expect(deleteContentPage("1")).resolves.toBeUndefined();
  });
});
