import { describe, it, expect } from 'vitest'
import {
  loginSchema,
  registerSchema,
  updatePasswordSchema,
  updateProfileSchema,
} from './auth'

describe('Auth Validation Schemas', () => {
  describe('loginSchema', () => {
    it('should validate correct login data', () => {
      const result = loginSchema.safeParse({
        email: 'test@example.com',
        password: 'password123',
      })
      expect(result.success).toBe(true)
    })

    it('should reject invalid email', () => {
      const result = loginSchema.safeParse({
        email: 'invalid-email',
        password: 'password123',
      })
      expect(result.success).toBe(false)
    })

    it('should reject empty email', () => {
      const result = loginSchema.safeParse({
        email: '',
        password: 'password123',
      })
      expect(result.success).toBe(false)
    })

    it('should reject empty password', () => {
      const result = loginSchema.safeParse({
        email: 'test@example.com',
        password: '',
      })
      expect(result.success).toBe(false)
    })
  })

  describe('registerSchema', () => {
    it('should validate correct registration data', () => {
      const result = registerSchema.safeParse({
        email: 'test@example.com',
        password: 'password123',
        name: 'Test User',
      })
      expect(result.success).toBe(true)
    })

    it('should reject password shorter than 8 characters', () => {
      const result = registerSchema.safeParse({
        email: 'test@example.com',
        password: 'short',
        name: 'Test User',
      })
      expect(result.success).toBe(false)
      if (!result.success) {
        expect(result.error.issues[0].path).toContain('password')
      }
    })

    it('should reject invalid email format', () => {
      const result = registerSchema.safeParse({
        email: 'not-an-email',
        password: 'password123',
        name: 'Test User',
      })
      expect(result.success).toBe(false)
      if (!result.success) {
        expect(result.error.issues[0].path).toContain('email')
      }
    })

    it('should reject empty name', () => {
      const result = registerSchema.safeParse({
        email: 'test@example.com',
        password: 'password123',
        name: '',
      })
      expect(result.success).toBe(false)
      if (!result.success) {
        expect(result.error.issues[0].path).toContain('name')
      }
    })

    it('should accept minimum valid password (8 characters)', () => {
      const result = registerSchema.safeParse({
        email: 'test@example.com',
        password: '12345678',
        name: 'Test User',
      })
      expect(result.success).toBe(true)
    })
  })

  describe('updatePasswordSchema', () => {
    it('should validate correct password update data', () => {
      const result = updatePasswordSchema.safeParse({
        currentPassword: 'oldpassword',
        newPassword: 'newpassword123',
      })
      expect(result.success).toBe(true)
    })

    it('should reject new password shorter than 8 characters', () => {
      const result = updatePasswordSchema.safeParse({
        currentPassword: 'oldpassword',
        newPassword: 'short',
      })
      expect(result.success).toBe(false)
      if (!result.success) {
        expect(result.error.issues[0].path).toContain('newPassword')
      }
    })

    it('should reject empty current password', () => {
      const result = updatePasswordSchema.safeParse({
        currentPassword: '',
        newPassword: 'newpassword123',
      })
      expect(result.success).toBe(false)
    })
  })

  describe('updateProfileSchema', () => {
    it('should validate correct profile update with name', () => {
      const result = updateProfileSchema.safeParse({
        name: 'New Name',
      })
      expect(result.success).toBe(true)
    })

    it('should validate correct profile update with email', () => {
      const result = updateProfileSchema.safeParse({
        email: 'newemail@example.com',
      })
      expect(result.success).toBe(true)
    })

    it('should validate correct profile update with both fields', () => {
      const result = updateProfileSchema.safeParse({
        name: 'New Name',
        email: 'newemail@example.com',
      })
      expect(result.success).toBe(true)
    })

    it('should reject invalid email format', () => {
      const result = updateProfileSchema.safeParse({
        email: 'invalid-email',
      })
      expect(result.success).toBe(false)
    })

    it('should accept empty object (no updates)', () => {
      const result = updateProfileSchema.safeParse({})
      expect(result.success).toBe(true)
    })

    it('should accept undefined values', () => {
      const result = updateProfileSchema.safeParse({
        name: undefined,
        email: undefined,
      })
      expect(result.success).toBe(true)
    })
  })
})
