/**
 * T010d — Conversation affinity can now go negative.
 *
 * The fallback-response path (and the AI affinity-application path) previously
 * floored affinity at 0 inside the conversation code, contradicting the global
 * -100..100 STAT_BOUNDS fix. A sour interaction must now be able to push affinity
 * below zero so a disliked NPC's negative feeling persists.
 */
import { describe, it, expect } from 'vitest';
import { Person } from '../../../src/models/Person.js';
import { Player } from '../../../src/models/Player.js';
import { ConversationObj } from '../../../src/events/conversations/types.js';
import { getFallbackResponse } from '../../../src/events/conversations/ai_response.js';

function makeSetup(affinity: number): { conversation: ConversationObj; character: Person; player: Player } {
  const character = new Person({
    id: 'npc-1',
    firstname: 'Riley',
    lastname: 'Q',
    sex: 'Female',
    ageYears: 30,
    affinity,
    relationships: ['acquaintance'],
  } as never);
  const player = new Player({
    userId: 'p1',
    character: new Person({ id: 'me', firstname: 'A', lastname: 'B', sex: 'Male', ageYears: 30 } as never),
    status: 'playing',
    controller: 'active',
    r: [character],
    date: '01-01',
  });
  const conversation = new ConversationObj(character, 'chat');
  return { conversation, character, player };
}

describe('conversation affinity bounds (-100..100)', () => {
  it('lets a low-affinity interaction push affinity below zero', async () => {
    const { conversation, character, player } = makeSetup(0);

    await getFallbackResponse(conversation, character, player);

    // Low-affinity branch applies -2; starting at 0, affinity must go negative.
    expect(character.affinity).toBeLessThan(0);
    expect(character.affinity).toBe(-2);
  });

  it('floors negative affinity at -100, not 0', async () => {
    const { conversation, character, player } = makeSetup(-100);

    await getFallbackResponse(conversation, character, player);

    expect(character.affinity).toBe(-100); // clamped at -100, NOT reset to 0
  });

  it('keeps a high-affinity interaction non-negative (no spurious decay)', async () => {
    const { conversation, character, player } = makeSetup(80);

    await getFallbackResponse(conversation, character, player);

    // High-affinity branch does not modify affinity.
    expect(character.affinity).toBe(80);
  });
});
