import {
  EnableTOTPDto,
  VerifyTOTPDto,
} from '../mfa.dto';

describe('MFA DTOs', () => {
  describe('EnableTOTPDto', () => {
    it('should validate valid enable TOTP data', () => {
      const validData = {
        token: '123456',
      };

      const result = EnableTOTPDto.safeParse(validData);
      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data).toEqual(validData);
      }
    });

    it('should reject token shorter than 6 digits', () => {
      const invalidData = {
        token: '12345',
      };

      const result = EnableTOTPDto.safeParse(invalidData);
      expect(result.success).toBe(false);
      if (!result.success) {
        expect(result.error.issues[0].path).toContain('token');
        expect(result.error.issues[0].message).toContain('6 digits');
      }
    });

    it('should reject token longer than 6 digits', () => {
      const invalidData = {
        token: '1234567',
      };

      const result = EnableTOTPDto.safeParse(invalidData);
      expect(result.success).toBe(false);
      if (!result.success) {
        expect(result.error.issues[0].path).toContain('token');
        expect(result.error.issues[0].message).toContain('6 digits');
      }
    });

    it('should accept any 6-character token', () => {
      const validData = {
        token: 'abcdef',
      };

      const result = EnableTOTPDto.safeParse(validData);
      expect(result.success).toBe(true);
    });

    it('should reject empty token', () => {
      const invalidData = {
        token: '',
      };

      const result = EnableTOTPDto.safeParse(invalidData);
      expect(result.success).toBe(false);
      if (!result.success) {
        expect(result.error.issues[0].path).toContain('token');
      }
    });
  });

  describe('VerifyTOTPDto', () => {
    it('should validate valid verify TOTP data with secret', () => {
      const validData = {
        token: '123456',
        secret: 'JBSWY3DPEHPK3PXP',
      };

      const result = VerifyTOTPDto.safeParse(validData);
      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data).toEqual(validData);
      }
    });

    it('should validate valid verify TOTP data without secret', () => {
      const validData = {
        token: '123456',
      };

      const result = VerifyTOTPDto.safeParse(validData);
      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.token).toBe(validData.token);
        expect(result.data.secret).toBeUndefined();
      }
    });

    it('should reject token shorter than 6 digits', () => {
      const invalidData = {
        token: '12345',
        secret: 'JBSWY3DPEHPK3PXP',
      };

      const result = VerifyTOTPDto.safeParse(invalidData);
      expect(result.success).toBe(false);
      if (!result.success) {
        expect(result.error.issues[0].path).toContain('token');
        expect(result.error.issues[0].message).toContain('6 digits');
      }
    });

    it('should reject token longer than 6 digits', () => {
      const invalidData = {
        token: '1234567',
        secret: 'JBSWY3DPEHPK3PXP',
      };

      const result = VerifyTOTPDto.safeParse(invalidData);
      expect(result.success).toBe(false);
      if (!result.success) {
        expect(result.error.issues[0].path).toContain('token');
        expect(result.error.issues[0].message).toContain('6 digits');
      }
    });

    it('should accept any 6-character token', () => {
      const validData = {
        token: 'abcdef',
        secret: 'JBSWY3DPEHPK3PXP',
      };

      const result = VerifyTOTPDto.safeParse(validData);
      expect(result.success).toBe(true);
    });

    it('should accept any string as secret', () => {
      const validData = {
        token: '123456',
        secret: 'any-secret-string',
      };

      const result = VerifyTOTPDto.safeParse(validData);
      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.secret).toBe(validData.secret);
      }
    });
  });
});