import { describe, it, expect } from "vitest";
import {
  createTextbookSchema,
  updateTextbookSchema,
  listTextbooksSchema,
} from "./textbooks.dto.js";

describe("textbooks.dto", () => {
  describe("createTextbookSchema", () => {
    it("should parse valid input", () => {
      const result = createTextbookSchema.parse({
        title: "Math",
        subjectId: "subj-1",
        gradeId: "g-1",
      });
      expect(result.title).toBe("Math");
      expect(result.version).toBe("1.0");
    });

    it("should reject empty title", () => {
      expect(() =>
        createTextbookSchema.parse({ title: "", subjectId: "s", gradeId: "g" }),
      ).toThrow();
    });

    it("should reject missing subjectId", () => {
      expect(() =>
        createTextbookSchema.parse({ title: "T", gradeId: "g" }),
      ).toThrow();
    });
  });

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

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

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

  describe("listTextbooksSchema", () => {
    it("should default page and pageSize", () => {
      const result = listTextbooksSchema.parse({});
      expect(result.page).toBe(1);
      expect(result.pageSize).toBe(20);
    });

    it("should coerce string numbers", () => {
      const result = listTextbooksSchema.parse({ page: "3", pageSize: "50" });
      expect(result.page).toBe(3);
      expect(result.pageSize).toBe(50);
    });
  });
});
