/**
 * Task 0-B (offline mirror): a prior fix corrected the shared/online affinity
 * bound to -100..100 in statUtils (STAT_BOUNDS.affinity). The OFFLINE path in
 * GameEngine still hard-clamped relationship affinity to 0..100, re-zeroing any
 * negative affinity for offline players. The clamp must now use -100..100 so a
 * disliked relationship's negative affinity survives an offline updateAge.
 */
import { describe, it, expect } from 'vitest';
import { Person } from '../../src/models/Person.js';
import { Player } from '../../src/models/Player.js';
import { getGameEngine } from '../../src/game/engine/GameEngine.js';

const engine = getGameEngine();

/** An alive relationship positioned so the per-relationship daily update block
 * runs and reaches the affinity clamp WITHOUT triggering the monthly decay
 * (ageDays % 30) or the yearly-birthday early-return (ageDays % 365). That keeps
 * affinity unchanged before the clamp, so the test isolates the clamp itself. */
function makeRelative(affinity: number): Person {
  const person = new Person({
    id: 'npc-1',
    firstname: 'Foe',
    lastname: 'Doe',
    sex: 'female',
    status: 'alive',
    relationships: ['friend'],
    ageYears: 30,
    ageDays: 100, // +1 -> 101: not a multiple of 30 or 365 -> no pre-clamp decay
    affinity,
    deathChance: 0,
    health: 0, // deathChance * health == 0 -> cannot randomly die mid-test
  } as never);
  return person;
}

/** Player poised so updateAge runs the daily block but the player itself is not
 * having a birthday. */
function makePlayer(relatives: Person[]): Player {
  const player = new Player({
    userId: 'player-1',
    character: new Person({
      id: 'me',
      firstname: 'You',
      lastname: 'Me',
      sex: 'Male',
      ageYears: 20,
      ageDays: 7300,
      status: 'alive',
    } as never),
    status: 'playing',
    controller: 'active',
    r: relatives,
  });
  player.c.ageHours = 23; // +1 -> 24 -> daily block runs
  return player;
}

describe('GameEngine offline affinity clamp mirror (Fix 0-B)', () => {
  it('does NOT reset a negative relationship affinity to 0', () => {
    const npc = makeRelative(-40);
    const player = makePlayer([npc]);

    engine.updateAge(player);

    expect(npc.affinity).toBe(-40);
    expect(npc.affinity).toBeLessThan(0);
  });

  it('still floors affinity at -100 (does not allow values below -100)', () => {
    const npc = makeRelative(-150);
    const player = makePlayer([npc]);

    engine.updateAge(player);

    expect(npc.affinity).toBe(-100);
  });

  it('still ceils affinity at 100', () => {
    const npc = makeRelative(150);
    const player = makePlayer([npc]);

    engine.updateAge(player);

    expect(npc.affinity).toBe(100);
  });
});
