import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
import { describe, it, expect } from 'vitest';
import { ModerateListingDto } from '../dto/moderate-listing.dto';

describe('ModerateListingDto', () => {
  it('should pass validation with approve action', async () => {
    const dto = plainToInstance(ModerateListingDto, {
      action: 'approve',
    });

    const errors = await validate(dto);
    expect(errors).toHaveLength(0);
    expect(dto.action).toBe('approve');
  });

  it('should pass validation with reject action and notes', async () => {
    const dto = plainToInstance(ModerateListingDto, {
      action: 'reject',
      notes: 'Ảnh không rõ ràng',
      moderationScore: 30,
    });

    const errors = await validate(dto);
    expect(errors).toHaveLength(0);
    expect(dto.action).toBe('reject');
    expect(dto.notes).toBe('Ảnh không rõ ràng');
    expect(dto.moderationScore).toBe(30);
  });

  it('should fail validation with invalid action', async () => {
    const dto = plainToInstance(ModerateListingDto, {
      action: 'suspend',
    });

    const errors = await validate(dto);
    expect(errors.length).toBeGreaterThan(0);
    const actionError = errors.find((e) => e.property === 'action');
    expect(actionError).toBeDefined();
  });

  it('should fail validation when moderationScore exceeds 100', async () => {
    const dto = plainToInstance(ModerateListingDto, {
      action: 'approve',
      moderationScore: 150,
    });

    const errors = await validate(dto);
    expect(errors.length).toBeGreaterThan(0);
    const scoreError = errors.find((e) => e.property === 'moderationScore');
    expect(scoreError).toBeDefined();
  });

  it('should fail validation when moderationScore is negative', async () => {
    const dto = plainToInstance(ModerateListingDto, {
      action: 'approve',
      moderationScore: -10,
    });

    const errors = await validate(dto);
    expect(errors.length).toBeGreaterThan(0);
    const scoreError = errors.find((e) => e.property === 'moderationScore');
    expect(scoreError).toBeDefined();
  });

  it('should pass validation with optional fields omitted', async () => {
    const dto = plainToInstance(ModerateListingDto, {
      action: 'reject',
    });

    const errors = await validate(dto);
    expect(errors).toHaveLength(0);
    expect(dto.moderationScore).toBeUndefined();
    expect(dto.notes).toBeUndefined();
  });
});
