import { describe, it, expect } from "vitest";

import { createDiagramSchema } from "../../src/modules/diagram/router";

describe("createDiagramSchema", () => {
  describe("title field", () => {
    it("should accept a valid title", () => {
      const result = createDiagramSchema.safeParse({
        title: "My Diagram",
        type: "flowchart",
      });
      expect(result.success).toBe(true);
    });

    it("should reject empty title", () => {
      const result = createDiagramSchema.safeParse({
        title: "",
        type: "flowchart",
      });
      expect(result.success).toBe(false);
    });

    it("should reject title over 255 characters", () => {
      const result = createDiagramSchema.safeParse({
        title: "a".repeat(256),
        type: "flowchart",
      });
      expect(result.success).toBe(false);
    });

    it("should accept title at max length (255)", () => {
      const result = createDiagramSchema.safeParse({
        title: "a".repeat(255),
        type: "flowchart",
      });
      expect(result.success).toBe(true);
    });

    it("should reject missing title", () => {
      const result = createDiagramSchema.safeParse({
        type: "flowchart",
      });
      expect(result.success).toBe(false);
    });
  });

  describe("type field", () => {
    const validTypes = [
      "bpmn",
      "er",
      "orgchart",
      "architecture",
      "sequence",
      "flowchart",
    ] as const;

    validTypes.forEach((type) => {
      it(`should accept type '${type}'`, () => {
        const result = createDiagramSchema.safeParse({
          title: "Test",
          type,
        });
        expect(result.success).toBe(true);
      });
    });

    it("should reject invalid type", () => {
      const result = createDiagramSchema.safeParse({
        title: "Test",
        type: "invalid",
      });
      expect(result.success).toBe(false);
    });

    it("should reject missing type", () => {
      const result = createDiagramSchema.safeParse({
        title: "Test",
      });
      expect(result.success).toBe(false);
    });
  });

  describe("projectId field", () => {
    it("should accept optional projectId", () => {
      const result = createDiagramSchema.safeParse({
        title: "Test",
        type: "bpmn",
        projectId: "proj-123",
      });
      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.projectId).toBe("proj-123");
      }
    });

    it("should accept missing projectId", () => {
      const result = createDiagramSchema.safeParse({
        title: "Test",
        type: "bpmn",
      });
      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.projectId).toBeUndefined();
      }
    });
  });

  describe("complete valid input", () => {
    it("should parse valid input correctly", () => {
      const input = {
        title: "System Architecture",
        type: "architecture" as const,
        projectId: "proj-abc",
      };
      const result = createDiagramSchema.safeParse(input);
      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data).toEqual(input);
      }
    });

    it("should strip unknown fields", () => {
      const result = createDiagramSchema.safeParse({
        title: "Test",
        type: "flowchart",
        unknownField: "should be stripped",
      });
      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data).not.toHaveProperty("unknownField");
      }
    });
  });
});
