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

import { UpdateNotificationPreferencesSchema } from "./schema"

/**
 * 通知偏好 Schema 测试
 *
 * V3-P1-3: 从 messaging/schema.test.ts 迁移至 notifications/schema.test.ts，
 * 与 notifications/schema.ts 位于同一模块。
 */
describe("UpdateNotificationPreferencesSchema", () => {
  const validInput = {
    emailEnabled: true,
    smsEnabled: false,
    pushEnabled: true,
    homeworkNotifications: true,
    gradeNotifications: true,
    announcementNotifications: true,
    messageNotifications: true,
    attendanceNotifications: false,
    quietHoursEnabled: false,
  }

  it("should parse valid input without quiet hours times", () => {
    const result = UpdateNotificationPreferencesSchema.safeParse(validInput)

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

  it("should parse valid input with quiet hours times", () => {
    const result = UpdateNotificationPreferencesSchema.safeParse({
      ...validInput,
      quietHoursStart: "22:00",
      quietHoursEnd: "07:00",
    })

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.quietHoursStart).toBe("22:00")
      expect(result.data.quietHoursEnd).toBe("07:00")
    }
  })

  it("should accept null quiet hours times", () => {
    const result = UpdateNotificationPreferencesSchema.safeParse({
      ...validInput,
      quietHoursStart: null,
      quietHoursEnd: null,
    })

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

  it("should accept undefined quiet hours times", () => {
    const result = UpdateNotificationPreferencesSchema.safeParse(validInput)

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.quietHoursStart).toBeUndefined()
      expect(result.data.quietHoursEnd).toBeUndefined()
    }
  })

  it("should reject invalid time format for quietHoursStart", () => {
    const result = UpdateNotificationPreferencesSchema.safeParse({
      ...validInput,
      quietHoursStart: "25:00",
    })

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

  it("should reject invalid time format for quietHoursEnd", () => {
    const result = UpdateNotificationPreferencesSchema.safeParse({
      ...validInput,
      quietHoursEnd: "12:60",
    })

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

  it("should reject non-time string for quietHoursStart", () => {
    const result = UpdateNotificationPreferencesSchema.safeParse({
      ...validInput,
      quietHoursStart: "not-a-time",
    })

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

  it("should accept boundary time 00:00", () => {
    const result = UpdateNotificationPreferencesSchema.safeParse({
      ...validInput,
      quietHoursStart: "00:00",
    })

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

  it("should accept boundary time 23:59", () => {
    const result = UpdateNotificationPreferencesSchema.safeParse({
      ...validInput,
      quietHoursEnd: "23:59",
    })

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

  it("should reject non-boolean emailEnabled", () => {
    const result = UpdateNotificationPreferencesSchema.safeParse({
      ...validInput,
      emailEnabled: "yes",
    })

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

  it("should reject non-boolean smsEnabled", () => {
    const result = UpdateNotificationPreferencesSchema.safeParse({
      ...validInput,
      smsEnabled: 1,
    })

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

  it("should reject missing required boolean field", () => {
    const inputWithoutEmail = {
      smsEnabled: false,
      pushEnabled: true,
      homeworkNotifications: true,
      gradeNotifications: true,
      announcementNotifications: true,
      messageNotifications: true,
      attendanceNotifications: false,
      quietHoursEnabled: false,
    }
    const result = UpdateNotificationPreferencesSchema.safeParse(inputWithoutEmail)

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