/**
 * T007b: starvation pressure. When hunger OR thirst is pegged at the cap (100),
 * health drains hard (-3/hr) and the mild +0.5/hr regen is SUPPRESSED until the
 * offending stat drops back below the recovery level (60).
 */
import { describe, it, expect } from 'vitest';
import { Person } from '../../src/models/Person.js';
import {
  applyHourlySurvival,
  STARVATION_HEALTH_DRAIN_PER_HOUR,
  STARVATION_RECOVERY_LEVEL,
} from '../../src/game/economyConstants.js';

function person(over: Partial<Record<string, number>> = {}): Person {
  return new Person({
    id: 'p',
    firstname: 'A',
    lastname: 'B',
    sex: 'Male',
    hunger: 0,
    thirst: 0,
    health: 100,
    energy: 100,
    calcEnergy: 100,
    happiness: 50,
    ...over,
  } as never);
}

describe('starvation pressure', () => {
  it('hunger at 100 drains health by 3/hr', () => {
    const p = person({ hunger: 100, thirst: 0, health: 80 });
    applyHourlySurvival(p);
    // hunger stays clamped at 100 (3 added then clamped); health -3
    expect(p.hunger).toBe(100);
    expect(p.health).toBe(80 - STARVATION_HEALTH_DRAIN_PER_HOUR);
  });

  it('thirst at 100 also drains health by 3/hr', () => {
    const p = person({ hunger: 0, thirst: 100, health: 50 });
    applyHourlySurvival(p);
    expect(p.thirst).toBe(100);
    expect(p.health).toBe(50 - STARVATION_HEALTH_DRAIN_PER_HOUR);
  });

  it('suppresses health regen until the starving stat drops below the recovery level', () => {
    // Hunger just below the cap but at/above recovery level: in the high band,
    // health still decays (mild), NOT regenerates.
    const p = person({ hunger: STARVATION_RECOVERY_LEVEL + 25, thirst: 0, health: 70 });
    applyHourlySurvival(p);
    // hunger 85 -> +3 = 88 (> 80 decay threshold) -> health drains 1, no regen
    expect(p.health).toBeLessThan(70);
  });

  it('resumes regen once fed/hydrated below recovery level', () => {
    const p = person({ hunger: 10, thirst: 10, health: 70 });
    applyHourlySurvival(p);
    // hunger 13, thirst 14 — both well below recovery level -> regen +0.5
    expect(p.health).toBe(70.5);
  });

  it('health never goes below 0', () => {
    const p = person({ hunger: 100, thirst: 100, health: 1 });
    applyHourlySurvival(p);
    expect(p.health).toBeGreaterThanOrEqual(0);
  });
});
