// [nexus] Schema validation tests for voiceMode field (Plan 36-02)
import { describe, it, expect } from "vitest";
import { createMessageSchema } from "@paperclipai/shared/validators/chat";
import { nexusSettingsSchema } from "../services/nexus-settings.js";

describe("createMessageSchema — voiceMode field", () => {
  it("parses voiceMode 'full_voice' and returns the value", () => {
    const result = createMessageSchema.parse({
      role: "user",
      content: "hi",
      voiceMode: "full_voice",
    });
    expect(result.voiceMode).toBe("full_voice");
  });

  it("parses voiceMode 'voice_input' and returns the value", () => {
    const result = createMessageSchema.parse({
      role: "user",
      content: "hi",
      voiceMode: "voice_input",
    });
    expect(result.voiceMode).toBe("voice_input");
  });

  it("parses voiceMode 'text' and returns the value", () => {
    const result = createMessageSchema.parse({
      role: "user",
      content: "hi",
      voiceMode: "text",
    });
    expect(result.voiceMode).toBe("text");
  });

  it("parses without voiceMode and returns undefined for voiceMode", () => {
    const result = createMessageSchema.parse({
      role: "user",
      content: "hi",
    });
    expect(result.voiceMode).toBeUndefined();
  });

  it("throws ZodError when voiceMode is invalid", () => {
    expect(() =>
      createMessageSchema.parse({
        role: "user",
        content: "hi",
        voiceMode: "invalid",
      })
    ).toThrow();
  });

  it("preserves existing fields — role, content, agentId, messageType", () => {
    const result = createMessageSchema.parse({
      role: "assistant",
      content: "hello world",
      agentId: "00000000-0000-0000-0000-000000000001",
      messageType: "markdown",
    });
    expect(result.role).toBe("assistant");
    expect(result.content).toBe("hello world");
    expect(result.agentId).toBe("00000000-0000-0000-0000-000000000001");
    expect(result.messageType).toBe("markdown");
  });
});

describe("nexusSettingsSchema — voiceMode and telegramToken fields", () => {
  it("parses settings without voiceMode and defaults to 'text'", () => {
    const result = nexusSettingsSchema.parse({ mode: "both" });
    expect(result.voiceMode).toBe("text");
  });

  it("parses settings with voiceMode 'full_voice' and preserves the value", () => {
    const result = nexusSettingsSchema.parse({ mode: "both", voiceMode: "full_voice" });
    expect(result.voiceMode).toBe("full_voice");
  });

  it("parses settings with telegramToken and preserves the value", () => {
    const result = nexusSettingsSchema.parse({ mode: "both", telegramToken: "123:ABC" });
    expect(result.telegramToken).toBe("123:ABC");
  });

  it("parses settings without telegramToken and returns undefined", () => {
    const result = nexusSettingsSchema.parse({ mode: "both" });
    expect(result.telegramToken).toBeUndefined();
  });

  it("parses existing settings without voiceMode/telegramToken without error", () => {
    expect(() =>
      nexusSettingsSchema.parse({ mode: "personal_ai", voiceEnabled: true })
    ).not.toThrow();
  });
});
