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

import {
  SendMessageSchema,
  MessageIdSchema,
} from "./schema"

describe("SendMessageSchema", () => {
  it("should parse valid input with all fields", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "user-1",
      subject: "Hello",
      content: "World",
      parentMessageId: "msg-1",
    })

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.receiverId).toBe("user-1")
      expect(result.data.subject).toBe("Hello")
      expect(result.data.content).toBe("World")
      expect(result.data.parentMessageId).toBe("msg-1")
    }
  })

  it("should parse valid input with only required fields", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "user-1",
      content: "Hello",
    })

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.receiverId).toBe("user-1")
      expect(result.data.content).toBe("Hello")
      expect(result.data.subject).toBeNull()
      expect(result.data.parentMessageId).toBeNull()
    }
  })

  it("should transform empty subject string to null", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "user-1",
      subject: "",
      content: "Hello",
    })

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.subject).toBeNull()
    }
  })

  it("should transform empty parentMessageId string to null", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "user-1",
      content: "Hello",
      parentMessageId: "",
    })

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.parentMessageId).toBeNull()
    }
  })

  it("should accept nullable subject", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "user-1",
      subject: null,
      content: "Hello",
    })

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.subject).toBeNull()
    }
  })

  it("should accept nullable parentMessageId", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "user-1",
      content: "Hello",
      parentMessageId: null,
    })

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.parentMessageId).toBeNull()
    }
  })

  it("should reject empty receiverId", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "",
      content: "Hello",
    })

    expect(result.success).toBe(false)
  })

  it("should reject whitespace-only receiverId after trim", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "   ",
      content: "Hello",
    })

    expect(result.success).toBe(false)
  })

  it("should reject empty content", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "user-1",
      content: "",
    })

    expect(result.success).toBe(false)
  })

  it("should reject whitespace-only content after trim", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "user-1",
      content: "   ",
    })

    expect(result.success).toBe(false)
  })

  it("should reject missing receiverId", () => {
    const result = SendMessageSchema.safeParse({
      content: "Hello",
    })

    expect(result.success).toBe(false)
  })

  it("should reject missing content", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "user-1",
    })

    expect(result.success).toBe(false)
  })

  it("should reject subject exceeding 255 chars", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "user-1",
      subject: "a".repeat(256),
      content: "Hello",
    })

    expect(result.success).toBe(false)
  })

  it("should accept subject of exactly 255 chars", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "user-1",
      subject: "a".repeat(255),
      content: "Hello",
    })

    expect(result.success).toBe(true)
  })

  it("should trim whitespace from receiverId", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "  user-1  ",
      content: "Hello",
    })

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.receiverId).toBe("user-1")
    }
  })

  it("should trim whitespace from content", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "user-1",
      content: "  Hello  ",
    })

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.content).toBe("Hello")
    }
  })

  it("should trim whitespace from subject", () => {
    const result = SendMessageSchema.safeParse({
      receiverId: "user-1",
      subject: "  Hello  ",
      content: "Hello",
    })

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.subject).toBe("Hello")
    }
  })
})

describe("MessageIdSchema", () => {
  it("should parse valid messageId", () => {
    const result = MessageIdSchema.safeParse({ messageId: "msg-1" })

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.messageId).toBe("msg-1")
    }
  })

  it("should reject empty messageId", () => {
    const result = MessageIdSchema.safeParse({ messageId: "" })

    expect(result.success).toBe(false)
  })

  it("should reject whitespace-only messageId after trim", () => {
    const result = MessageIdSchema.safeParse({ messageId: "   " })

    expect(result.success).toBe(false)
  })

  it("should reject missing messageId", () => {
    const result = MessageIdSchema.safeParse({})

    expect(result.success).toBe(false)
  })

  it("should trim whitespace from messageId", () => {
    const result = MessageIdSchema.safeParse({ messageId: "  msg-1  " })

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.messageId).toBe("msg-1")
    }
  })
})

// UpdateNotificationPreferencesSchema 测试已迁移至
// src/modules/notifications/schema.test.ts（V3-P1-3: 通知偏好 Schema 迁移）
