import type { ChatConfig } from '@genesislcap/foundation-ai';
import { assert, createLogicSuite } from '@genesislcap/foundation-testing';
import type { SessionPreferences } from '../state/persistence/session-persistence-provider';
import { resolvePreferenceBaseline } from './resolve-preference-baseline';

// GENC-1351 #2: the preference baseline resolves with precedence
//   slice defaults  <  chatConfig.ui defaults  <  stored user preferences.
// The regression this guards: when stored prefs are absent (first load / wiped
// backend) the result must fall through to the chatConfig.ui defaults, NOT to
// everything-disabled.

const FULL_UI: ChatConfig['ui'] = {
  showToolCalls: true,
  showThinkingSteps: true,
  showNarration: true,
  showAgentSwitchIndicator: true,
  animations: { userConfigurable: true },
};

const Suite = createLogicSuite('resolvePreferenceBaseline');

Suite('with no stored prefs, falls back to chatConfig.ui defaults (the nuke case)', () => {
  const r = resolvePreferenceBaseline(FULL_UI, null);
  assert.is(r.showToolCalls, true);
  assert.is(r.showThinkingSteps, true);
  assert.is(r.showNarration, true);
  assert.is(r.showAgentSwitchIndicator, true);
  assert.ok(r.enabledAnimations.length > 0, 'config animations enabled by default');
  assert.is(r.enabled, undefined, 'no stored enabled → leave host default untouched');
});

Suite('with no stored prefs AND empty/unbound chatConfig, everything is off', () => {
  // The transient state during connect before chatConfig binds — all-false is
  // correct HERE; the element re-runs the baseline once chatConfig arrives.
  const r = resolvePreferenceBaseline(undefined, null);
  assert.equal(
    [r.showToolCalls, r.showThinkingSteps, r.showNarration, r.showAgentSwitchIndicator],
    [false, false, false, false],
  );
  assert.equal(r.enabledAnimations, []);
});

Suite('stored prefs override the config defaults field-by-field', () => {
  const stored: SessionPreferences = {
    enabled: true,
    showToolCalls: false,
    showThinkingSteps: false,
    showNarration: true,
    showAgentSwitchIndicator: false,
    animations: ['halo'],
  };
  const r = resolvePreferenceBaseline(FULL_UI, stored);
  assert.is(r.showToolCalls, false, 'stored false beats config true');
  assert.is(r.showThinkingSteps, false);
  assert.is(r.showNarration, true);
  assert.is(r.showAgentSwitchIndicator, false);
  assert.equal(r.enabledAnimations, ['halo'], 'stored animations applied as-is');
  assert.is(r.enabled, true, 'stored enabled surfaces');
});

Suite('a partial stored blob overrides only present fields; others keep config default', () => {
  const stored = { showToolCalls: false } as SessionPreferences;
  const r = resolvePreferenceBaseline(FULL_UI, stored);
  assert.is(r.showToolCalls, false, 'present field overridden');
  assert.is(r.showThinkingSteps, true, 'absent field keeps config default');
  assert.is(r.showAgentSwitchIndicator, true);
  assert.is(r.enabled, undefined, 'absent enabled stays undefined');
});

Suite('stored prefs over an empty config still apply (config layer is all-false)', () => {
  const stored: SessionPreferences = { showToolCalls: true, showThinkingSteps: true };
  const r = resolvePreferenceBaseline(undefined, stored);
  assert.is(r.showToolCalls, true);
  assert.is(r.showThinkingSteps, true);
  assert.is(r.showNarration, false, 'not in stored, not in config → false');
});

Suite('de-dupes stored animations (heals blobs from the old settings-modal append bug)', () => {
  const stored = { animations: ['loading', 'halo', 'loading', 'halo'] } as SessionPreferences;
  const r = resolvePreferenceBaseline(FULL_UI, stored);
  assert.equal(r.enabledAnimations, ['loading', 'halo'], 'duplicates collapsed, order preserved');
});

Suite.run();
