/**
 * Lifecycle milestone smoke tests.
 *
 * These focus on stable, short-window checks so the suite stays fast and
 * reliable in the full backend matrix.
 */

import { describe, expect, it } from 'vitest';
import { HeadlessGame, type HeadlessGameOptions } from '../../src/testing/index.js';

function createGame(options: HeadlessGameOptions = {}): HeadlessGame {
  return new HeadlessGame({
    autoRespond: true,
    ...options,
    romanceOptions: {
      matchProbability: 1,
      progressProbability: 1,
      ...options.romanceOptions,
    },
  });
}

describe('Lifecycle Milestone Smoke', () => {
  it('triggers learnedWalk in early childhood', async () => {
    const game = createGame({ startAge: 1 });
    await game.advanceToAge(2);
    expect(game.hasEvent('learnedWalk')).toBe(true);
  });

  it('triggers learningColors in early childhood', async () => {
    const game = createGame({ startAge: 2 });
    await game.advanceToAge(3);
    expect(game.hasEvent('learningColors')).toBe(true);
  });

  it('triggers learnedBike in early childhood', async () => {
    const game = createGame({ startAge: 2 });
    await game.advanceToAge(4);
    expect(game.hasEvent('learnedBike')).toBe(true);
  });

  it('tracks birthdays for each simulated year', async () => {
    const game = createGame();
    await game.advanceToAge(3);

    expect(game.hasEvent('birthday_1')).toBe(true);
    expect(game.hasEvent('birthday_2')).toBe(true);
    expect(game.hasEvent('birthday_3')).toBe(true);
  });

  it('creates a romantic partner when romance simulation is enabled', async () => {
    const game = createGame({ startAge: 18, simulateRomance: true });
    await game.advanceToAge(19);

    expect(game.player.c.partner).toBeDefined();
    expect(game.getRomanceLog().some((e) => e.event === 'found_match')).toBe(true);
  });

  it('does not create romance when simulateRomance is disabled', async () => {
    const game = createGame({ startAge: 18, simulateRomance: false });
    await game.advanceToAge(19);

    expect(game.player.c.partner).toBeUndefined();
    expect(game.getRomanceLog().length).toBe(0);
  });

  it('keeps core stats in valid ranges during short simulation', async () => {
    const game = createGame({ startAge: 18 });
    await game.advanceDays(30);

    expect(game.player.c.health).toBeGreaterThanOrEqual(0);
    expect(game.player.c.health).toBeLessThanOrEqual(100);
    expect(Number.isFinite(game.player.c.happiness)).toBe(true);
    expect(Number.isFinite(game.player.c.energy)).toBe(true);
  });

  it('does not duplicate learnedWalk event', async () => {
    const game = createGame({ startAge: 1 });
    await game.advanceToAge(4);

    const walkEvents = game.getEvents().filter((e) => e.event.id === 'learnedWalk');
    expect(walkEvents.length).toBe(1);
  });

  it('runs a medium simulation without errors', async () => {
    const game = createGame({
      startAge: 18,
      simulateRomance: true,
      enableHappinessRecovery: true,
    });

    await game.advanceDays(180);
    const summary = game.getSummary();

    expect(summary.endAge).toBeGreaterThanOrEqual(18);
    expect(summary.eventsCount).toBeGreaterThan(0);
    expect(summary.questionsCount).toBeGreaterThan(0);
  });
});
