import { describe, it, expect } from "vitest";
import { createChapterSchema, updateChapterSchema } from "./chapters.dto.js";

describe("chapters.dto", () => {
  describe("createChapterSchema", () => {
    it("should parse valid input with all fields", () => {
      const result = createChapterSchema.parse({
        textbookId: "tb-1",
        title: "Chapter One",
        order: 5,
        parentId: "parent-1",
      });
      expect(result.textbookId).toBe("tb-1");
      expect(result.title).toBe("Chapter One");
      expect(result.order).toBe(5);
      expect(result.parentId).toBe("parent-1");
    });

    it("should default order to 0 when not provided", () => {
      const result = createChapterSchema.parse({
        textbookId: "tb-1",
        title: "Chapter One",
      });
      expect(result.order).toBe(0);
    });

    it("should accept input without optional parentId", () => {
      const result = createChapterSchema.parse({
        textbookId: "tb-1",
        title: "Chapter One",
      });
      expect(result.parentId).toBeUndefined();
    });

    it("should reject empty textbookId", () => {
      expect(() =>
        createChapterSchema.parse({ textbookId: "", title: "Chapter One" }),
      ).toThrow();
    });

    it("should reject empty title", () => {
      expect(() =>
        createChapterSchema.parse({ textbookId: "tb-1", title: "" }),
      ).toThrow();
    });

    it("should reject negative order", () => {
      expect(() =>
        createChapterSchema.parse({
          textbookId: "tb-1",
          title: "Chapter One",
          order: -1,
        }),
      ).toThrow();
    });
  });

  describe("updateChapterSchema", () => {
    it("should parse partial update with title only", () => {
      const result = updateChapterSchema.parse({ title: "New Title" });
      expect(result.title).toBe("New Title");
    });

    it("should accept valid status enum", () => {
      const result = updateChapterSchema.parse({ status: "published" });
      expect(result.status).toBe("published");
    });

    it("should reject invalid status value", () => {
      expect(() => updateChapterSchema.parse({ status: "invalid" })).toThrow();
    });
  });
});
