import { describe, it, expect } from 'vitest';
import { CreateUserSchema, UpdateUserSchema } from './user.schemas.js';

describe('CreateUserSchema', () => {
  it('accepts valid input', () => {
    const result = CreateUserSchema.safeParse({
      keycloakId: 'kc-1',
      displayName: 'Test User',
      email: 'test@example.com',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.householdIds).toEqual([]);
    }
  });

  it('rejects missing keycloakId', () => {
    const result = CreateUserSchema.safeParse({
      displayName: 'Test',
      email: 'test@example.com',
    });
    expect(result.success).toBe(false);
  });

  it('rejects empty keycloakId', () => {
    const result = CreateUserSchema.safeParse({
      keycloakId: '',
      displayName: 'Test',
      email: 'test@example.com',
    });
    expect(result.success).toBe(false);
  });

  it('rejects invalid email', () => {
    const result = CreateUserSchema.safeParse({
      keycloakId: 'kc-1',
      displayName: 'Test',
      email: 'not-email',
    });
    expect(result.success).toBe(false);
  });

  it('trims displayName', () => {
    const result = CreateUserSchema.safeParse({
      keycloakId: 'kc-1',
      displayName: '  Test  ',
      email: 'test@example.com',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.displayName).toBe('Test');
    }
  });

  it('rejects displayName exceeding 100 chars', () => {
    const result = CreateUserSchema.safeParse({
      keycloakId: 'kc-1',
      displayName: 'x'.repeat(101),
      email: 'test@example.com',
    });
    expect(result.success).toBe(false);
  });
});

describe('UpdateUserSchema', () => {
  it('accepts partial updates', () => {
    const result = UpdateUserSchema.safeParse({ displayName: 'New Name' });
    expect(result.success).toBe(true);
  });

  it('accepts empty object (all fields optional)', () => {
    const result = UpdateUserSchema.safeParse({});
    expect(result.success).toBe(true);
  });

  it('does not allow keycloakId', () => {
    const result = UpdateUserSchema.safeParse({ keycloakId: 'kc-1' });
    // keycloakId is omitted, so it should be stripped or rejected
    expect(result.success).toBe(true);
    if (result.success) {
      expect('keycloakId' in result.data).toBe(false);
    }
  });
});
