import { resolveEffectivePreference } from './preference-precedence';
import type { UserPreferenceFields } from '../../users/src';

describe('resolveEffectivePreference', () => {
  const appDefault: UserPreferenceFields = {
    preferredPace: 'moderate',
    preferredBudgetLevel: 'medium',
    maxWalkingDistanceKm: 5,
    preferredStartTime: null,
    childFriendlyPreferred: false,
    interests: [],
    notes: null,
  };

  it('falls back to the application default when nothing is set', () => {
    expect(
      resolveEffectivePreference(undefined, undefined, appDefault),
    ).toEqual(appDefault);
  });

  it('prefers the persistent user preference over the application default', () => {
    const userPref = { ...appDefault, preferredPace: 'relaxed' };
    expect(
      resolveEffectivePreference(undefined, userPref, appDefault).preferredPace,
    ).toBe('relaxed');
  });

  it('prefers a trip-specific override over the persistent user preference', () => {
    const userPref = { ...appDefault, preferredPace: 'relaxed' };
    const override = { preferredPace: 'fast' };
    expect(
      resolveEffectivePreference(override, userPref, appDefault).preferredPace,
    ).toBe('fast');
  });

  it('merges field-by-field rather than replacing the whole object', () => {
    const userPref = {
      ...appDefault,
      preferredPace: 'relaxed',
      childFriendlyPreferred: true,
    };
    const override = { preferredPace: 'fast' };
    const result = resolveEffectivePreference(override, userPref, appDefault);
    expect(result.preferredPace).toBe('fast');
    expect(result.childFriendlyPreferred).toBe(true);
  });
});
