// M4 desired behavior: dead PC stays in turn order, turn still comes up,
// deathSave fires. Current code filters isActive (set false on death) so
// dead participants are SKIPPED. Test asserts desired state = RED on current.

const shared = require('@ttrpg/shared');
const { makeParticipant, startEncounter, nextTurn, applyHpChange, deathSave } = shared;

function p(id, init, extra = {}) {
  return makeParticipant({
    id, name: id, type: 'monster',
    initiative: init, maxHp: 100, currentHp: 100,
    ...extra,
  });
}
function pc(id, init, extra = {}) {
  return makeParticipant({
    id, name: id, type: 'character',
    initiative: init, maxHp: 100, currentHp: 100,
    ...extra,
  });
}
function enc(ps) {
  return { name:'t', participants:ps, isStarted:false, isPaused:false,
    round:0, currentTurnParticipantId:null, turnOrderIds:[] };
}

describe('M4: dead participants stay in turn order', () => {
  test('dead PC not removed from turnOrderIds', () => {
    const ps = [pc('a', 20), pc('b', 15), pc('c', 10)];
    let e = enc(ps);
    e = { ...e, ...startEncounter(e).patch };
    const orderBefore = e.turnOrderIds.slice();
    // kill b
    e = { ...e, ...applyHpChange(e, 'b', 'damage', 100).patch };
    expect(e.turnOrderIds).toEqual(orderBefore);
  });

  test('dead PC turn still comes up (nextTurn visits them)', () => {
    const ps = [pc('a', 20), pc('b', 15), pc('c', 10)];
    let e = enc(ps);
    e = { ...e, ...startEncounter(e).patch };
    // kill b
    e = { ...e, ...applyHpChange(e, 'b', 'damage', 100).patch };
    // advance: a→b→c. b's turn should come up.
    e = { ...e, ...nextTurn(e).patch };
    expect(e.currentTurnParticipantId).toBe('b');
  });

  test('dead PC on their turn can deathSave', () => {
    const ps = [pc('a', 20), pc('b', 15)];
    let e = enc(ps);
    e = { ...e, ...startEncounter(e).patch };
    // kill b (current = a)
    e = { ...e, ...applyHpChange(e, 'b', 'damage', 100).patch };
    // advance to b's turn
    e = { ...e, ...nextTurn(e).patch };
    expect(e.currentTurnParticipantId).toBe('b');
    // b is dead, on their turn: deathSave should not throw
    const r = deathSave(e, 'b', 1);
    expect(r.patch).toBeTruthy();
    const b = r.patch.participants.find(x => x.id === 'b');
    expect(b.deathSaves).toBe(1);
  });

  test('dead PC not auto-set isActive=false by applyHpChange', () => {
    const ps = [pc('a', 20), pc('b', 15)];
    let e = enc(ps);
    e = { ...e, ...startEncounter(e).patch };
    e = { ...e, ...applyHpChange(e, 'b', 'damage', 100).patch };
    const b = e.participants.find(x => x.id === 'b');
    expect(b.isActive).toBe(true);
  });
});
