/**
 * Health/death stakes (T5): lifestyle + chronic illness raise mortality, so a
 * life can end from something other than old age, and quitting a habit pays off.
 */
import { describe, it, expect } from 'vitest';
import { updateDeathChance } from '../../src/services/health/health_manager.js';

function person(age: number, extra: Record<string, unknown> = {}): any {
  return { ageYears: age, health: 100, deathChance: 0, habits: [], healthConditions: [], ...extra };
}

const chronic = { date: '01-01', isCured: false, averageDuration: 99999, healthModifier: 20, title: 'Heart Disease', id: 'c11' };
const vice = { name: 'smoking', habitType: 'negative', status: 'active' };

describe('updateDeathChance mortality multiplier', () => {
  it('chronic illness raises death chance vs a healthy peer', () => {
    const healthy = updateDeathChance(person(60));
    const ill = updateDeathChance(person(60, { healthConditions: [chronic] }));
    expect(ill).toBeGreaterThan(healthy);
    expect(ill).toBeCloseTo(healthy * 1.5, 6); // +50% per chronic condition
  });

  it('an active vice raises death chance', () => {
    const clean = updateDeathChance(person(60));
    const smoker = updateDeathChance(person(60, { habits: [vice] }));
    expect(smoker).toBeCloseTo(clean * 1.25, 6); // +25% per active vice
  });

  it('chronic illness + vice stack', () => {
    const base = updateDeathChance(person(60));
    const both = updateDeathChance(person(60, { healthConditions: [chronic], habits: [vice] }));
    expect(both).toBeCloseTo(base * 1.75, 6);
  });

  it('does not meaningfully threaten the young (base age risk dominates)', () => {
    const young = updateDeathChance(person(25, { healthConditions: [chronic], habits: [vice] }));
    // Even with both, a 25-year-old's daily death chance stays tiny.
    expect(young).toBeLessThan(0.0001);
  });

  it('a cured/acute condition does NOT raise mortality', () => {
    const clean = updateDeathChance(person(60));
    const acute = updateDeathChance(person(60, { healthConditions: [{ date: '01-01', isCured: false, averageDuration: 7, healthModifier: 10 }] }));
    const cured = updateDeathChance(person(60, { healthConditions: [{ ...chronic, isCured: true }] }));
    expect(acute).toBeCloseTo(clean, 6); // acute illness isn't a mortality vector
    expect(cured).toBeCloseTo(clean, 6); // cured chronic no longer counts
  });
});
