import { describe, it, expect } from 'vitest';
import {
  HouseholdSettingsSchema,
  CreateHouseholdSchema,
  UpdateHouseholdSchema,
  JoinHouseholdSchema,
  HouseholdMemberSchema,
} from './household.schemas.js';
import { HouseholdRole } from '../enums/roles.enums.js';

describe('HouseholdSettingsSchema', () => {
  it('accepts empty object with defaults', () => {
    const result = HouseholdSettingsSchema.safeParse({});
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.timezone).toBe('UTC');
      expect(result.data.currency).toBe('USD');
      expect(result.data.language).toBe('en');
    }
  });

  it('accepts custom settings', () => {
    const result = HouseholdSettingsSchema.safeParse({
      timezone: 'Asia/Tokyo',
      currency: 'JPY',
      language: 'ja',
    });
    expect(result.success).toBe(true);
  });

  it('rejects currency exceeding 3 chars', () => {
    const result = HouseholdSettingsSchema.safeParse({ currency: 'LONG' });
    expect(result.success).toBe(false);
  });

  it('rejects language exceeding 5 chars', () => {
    const result = HouseholdSettingsSchema.safeParse({ language: 'toolong' });
    expect(result.success).toBe(false);
  });
});

describe('CreateHouseholdSchema', () => {
  it('accepts valid input', () => {
    const result = CreateHouseholdSchema.safeParse({ name: 'My Household' });
    expect(result.success).toBe(true);
  });

  it('rejects empty name', () => {
    const result = CreateHouseholdSchema.safeParse({ name: '' });
    expect(result.success).toBe(false);
  });

  it('rejects name exceeding 100 chars', () => {
    const result = CreateHouseholdSchema.safeParse({ name: 'x'.repeat(101) });
    expect(result.success).toBe(false);
  });

  it('trims name', () => {
    const result = CreateHouseholdSchema.safeParse({ name: '  Test  ' });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.name).toBe('Test');
    }
  });

  it('accepts optional settings', () => {
    const result = CreateHouseholdSchema.safeParse({
      name: 'Test',
      settings: { timezone: 'US/Eastern' },
    });
    expect(result.success).toBe(true);
  });
});

describe('UpdateHouseholdSchema', () => {
  it('accepts partial updates', () => {
    const result = UpdateHouseholdSchema.safeParse({ name: 'Updated' });
    expect(result.success).toBe(true);
  });

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

  it('accepts partial settings', () => {
    const result = UpdateHouseholdSchema.safeParse({ settings: { currency: 'EUR' } });
    expect(result.success).toBe(true);
  });
});

describe('JoinHouseholdSchema', () => {
  it('accepts valid invite code', () => {
    const result = JoinHouseholdSchema.safeParse({ inviteCode: 'ABC123' });
    expect(result.success).toBe(true);
  });

  it('rejects empty invite code', () => {
    const result = JoinHouseholdSchema.safeParse({ inviteCode: '' });
    expect(result.success).toBe(false);
  });

  it('rejects missing invite code', () => {
    const result = JoinHouseholdSchema.safeParse({});
    expect(result.success).toBe(false);
  });
});

describe('HouseholdMemberSchema', () => {
  it('accepts valid member', () => {
    const result = HouseholdMemberSchema.safeParse({
      userId: 'user-1',
      role: HouseholdRole.OWNER,
      joinedAt: new Date(),
    });
    expect(result.success).toBe(true);
  });

  it('rejects invalid role', () => {
    const result = HouseholdMemberSchema.safeParse({
      userId: 'user-1',
      role: 'superadmin',
      joinedAt: new Date(),
    });
    expect(result.success).toBe(false);
  });

  it('rejects empty userId', () => {
    const result = HouseholdMemberSchema.safeParse({
      userId: '',
      role: HouseholdRole.MEMBER,
      joinedAt: new Date(),
    });
    expect(result.success).toBe(false);
  });
});
