import { loginSchema, tokenResponseSchema } from './schemas';

describe('Login Schemas', () => {
  describe('loginSchema', () => {
    it('deve validar credenciais válidas', () => {
      const result = loginSchema.safeParse({
        username: 'user123',
        password: 'pass1234',
      });

      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.username).toBe('user123');
        expect(result.data.password).toBe('pass1234');
      }
    });

    it('deve rejeitar username muito curto', () => {
      const result = loginSchema.safeParse({
        username: 'ab',
        password: 'pass1234',
      });

      expect(result.success).toBe(false);
      if (!result.success) {
        expect(result.error.issues[0].message).toContain('obrigatório');
      }
    });

    it('deve rejeitar senha muito curta', () => {
      const result = loginSchema.safeParse({
        username: 'user123',
        password: '123',
      });

      expect(result.success).toBe(false);
      if (!result.success) {
        expect(result.error.issues[0].message).toContain('mínimo 4 caracteres');
      }
    });

    // 🐛 TESTE QUE REVELA BUG: Senha muito fraca é aceita
    it('🐛 BUG: deve rejeitar senhas fracas (menos de 8 caracteres)', () => {
      const result = loginSchema.safeParse({
        username: 'user123',
        password: '1234', // Apenas 4 caracteres - muito fraco!
      });

      // ❌ ESTE TESTE VAI FALHAR - senha fraca é aceita!
      expect(result.success).toBe(false);
    });

    it('deve rejeitar username vazio', () => {
      const result = loginSchema.safeParse({
        username: '',
        password: 'pass1234',
      });

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

    it('deve rejeitar password vazio', () => {
      const result = loginSchema.safeParse({
        username: 'user123',
        password: '',
      });

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

  describe('tokenResponseSchema', () => {
    it('deve validar resposta de token válida', () => {
      const result = tokenResponseSchema.safeParse({
        token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
        type: 'Bearer',
        expiresIn: 3600,
        username: 'user123',
      });

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

    it('deve rejeitar token sem campos obrigatórios', () => {
      const result = tokenResponseSchema.safeParse({
        token: 'some-token',
        // faltando type, expiresIn, username
      });

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

    it('deve rejeitar expiresIn negativo', () => {
      const result = tokenResponseSchema.safeParse({
        token: 'some-token',
        type: 'Bearer',
        expiresIn: -100,
        username: 'user123',
      });

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

    it('deve rejeitar expiresIn zero', () => {
      const result = tokenResponseSchema.safeParse({
        token: 'some-token',
        type: 'Bearer',
        expiresIn: 0,
        username: 'user123',
      });

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