import { describe, it, expect } from "vitest";
import {
  createQuestionSchema,
  updateQuestionSchema,
  listQuestionsSchema,
  questionTypeSchema,
  questionSourceSchema,
} from "./questions.dto.js";

describe("questions.dto", () => {
  describe("questionTypeSchema", () => {
    it("should accept all valid question types", () => {
      const validTypes = [
        "single_choice",
        "multiple_choice",
        "short_answer",
        "essay",
      ];
      for (const t of validTypes) {
        expect(questionTypeSchema.parse(t)).toBe(t);
      }
    });

    it("should reject invalid question type", () => {
      expect(() => questionTypeSchema.parse("invalid_type")).toThrow();
    });
  });

  describe("questionSourceSchema", () => {
    it("should accept all valid question sources", () => {
      const validSources = ["manual", "ai_generated", "imported"];
      for (const s of validSources) {
        expect(questionSourceSchema.parse(s)).toBe(s);
      }
    });
  });

  describe("createQuestionSchema", () => {
    it("should parse valid input and apply defaults", () => {
      const result = createQuestionSchema.parse({
        knowledgePointId: "kp-1",
        type: "single_choice",
        content: "What is 2+2?",
        answer: "4",
        createdBy: "user-1",
      });
      expect(result.knowledgePointId).toBe("kp-1");
      expect(result.type).toBe("single_choice");
      expect(result.content).toBe("What is 2+2?");
      expect(result.answer).toBe("4");
      expect(result.difficulty).toBe(3);
      expect(result.source).toBe("manual");
    });

    it("should reject missing answer", () => {
      expect(() =>
        createQuestionSchema.parse({
          knowledgePointId: "kp-1",
          type: "essay",
          content: "Write an essay",
          createdBy: "user-1",
        }),
      ).toThrow();
    });

    it("should reject invalid type", () => {
      expect(() =>
        createQuestionSchema.parse({
          knowledgePointId: "kp-1",
          type: "invalid",
          content: "content",
          answer: "answer",
          createdBy: "user-1",
        }),
      ).toThrow();
    });
  });

  describe("updateQuestionSchema", () => {
    it("should parse partial update with status", () => {
      const result = updateQuestionSchema.parse({ status: "published" });
      expect(result.status).toBe("published");
    });

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

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

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