import { validate } from 'class-validator';
import { describe, it, expect } from 'vitest';
import { RegisterDto } from '../dto/register.dto';

describe('RegisterDto', () => {
  const createValidDto = (): RegisterDto => {
    const dto = new RegisterDto();
    dto.phone = '0901234567';
    dto.password = 'P@ssw0rd!';
    dto.fullName = 'Nguyen Van A';
    return dto;
  };

  it('accepts valid registration data', async () => {
    const dto = createValidDto();
    const errors = await validate(dto);
    expect(errors).toHaveLength(0);
  });

  it('accepts valid data with optional email', async () => {
    const dto = createValidDto();
    dto.email = 'user@example.com';
    const errors = await validate(dto);
    expect(errors).toHaveLength(0);
  });

  it('rejects missing phone', async () => {
    const dto = createValidDto();
    delete (dto as any).phone;
    const errors = await validate(dto);
    expect(errors.length).toBeGreaterThan(0);
    expect(errors.some((e) => e.property === 'phone')).toBe(true);
  });

  it('rejects password shorter than 8 characters', async () => {
    const dto = createValidDto();
    dto.password = 'short';
    const errors = await validate(dto);
    expect(errors.length).toBeGreaterThan(0);
    expect(errors.some((e) => e.property === 'password')).toBe(true);
  });

  it('rejects empty fullName', async () => {
    const dto = createValidDto();
    dto.fullName = '';
    const errors = await validate(dto);
    expect(errors.length).toBeGreaterThan(0);
    expect(errors.some((e) => e.property === 'fullName')).toBe(true);
  });

  it('rejects invalid email format', async () => {
    const dto = createValidDto();
    dto.email = 'not-an-email';
    const errors = await validate(dto);
    expect(errors.length).toBeGreaterThan(0);
    expect(errors.some((e) => e.property === 'email')).toBe(true);
  });

  it('accepts dto without email (optional field)', async () => {
    const dto = createValidDto();
    // email not set
    const errors = await validate(dto);
    expect(errors).toHaveLength(0);
  });
});
