// Diagnostic probe for the lifetime simulator. Not a regression test —
// prints baseline event firing counts so we know what realistic bounds look
// like for Task 2.3 invariants. Keep this test passing trivially; the
// numbers it logs inform the thresholds in lifetime-simulator.test.ts.

import { describe, it, vi } from 'vitest';

vi.mock('../../src/database/players.js', () => ({
  savePlayer: vi.fn(async () => undefined),
  saveConversation: vi.fn(async () => undefined),
  saveConversationMessage: vi.fn(async () => undefined),
  loadPlayer: vi.fn(async () => null),
  loadConversations: vi.fn(async () => []),
  loadPlayerConversations: vi.fn(async () => []),
  markConversationAsRead: vi.fn(async () => undefined),
  updateConnectionStatus: vi.fn(async () => undefined),
  ensureTables: vi.fn(async () => undefined),
  saveGameAsync: vi.fn(async () => undefined),
  loadGameAsync: vi.fn(async () => null),
  loadAllPlayerIds: vi.fn(async () => []),
  loadGames: vi.fn(async () => []),
}));

type MemInstance = {
  instanceId: string;
  eventId: string;
  playerId: string;
  status: 'pending' | 'answered' | 'resolved' | 'cancelled';
  createdAt: string;
};
const memInstances: MemInstance[] = [];

vi.mock('../../src/database/eventInstances.js', () => ({
  createEventInstance: vi.fn(async (input: Omit<MemInstance, 'createdAt' | 'status'>) => {
    const inst: MemInstance = { ...input, createdAt: new Date().toISOString(), status: 'pending' };
    memInstances.push(inst);
    return inst;
  }),
  getPendingEventInstances: vi.fn(async (playerId: string) =>
    memInstances.filter((i) => i.playerId === playerId && i.status === 'pending')
  ),
  answerEventInstance: vi.fn(async (instanceId: string) => {
    const inst = memInstances.find((i) => i.instanceId === instanceId);
    if (!inst) return false;
    inst.status = 'answered';
    return true;
  }),
  resolveEventInstance: vi.fn(async (instanceId: string) => {
    const inst = memInstances.find((i) => i.instanceId === instanceId);
    if (!inst) return false;
    inst.status = 'resolved';
    return true;
  }),
  ensureEventInstancesTable: vi.fn(async () => undefined),
}));

vi.mock('../../src/services/notifications/notificationManager.js', () => ({
  notifyRealtimeEvent: vi.fn(async () => undefined),
  queueRealtimeNotification: vi.fn(async () => undefined),
  clearThrottle: vi.fn(() => undefined),
  notificationManager: {},
}));

vi.mock('../../src/events/conversations/npc_initiative.js', () => ({
  checkNPCInitiatedMessages: vi.fn(async () => undefined),
  checkRelationshipAtRiskNudges: vi.fn(async () => []),
  clearNPCInitiativeState: vi.fn(() => undefined),
  isAppropriateHour: vi.fn(() => true),
  detectNPCTriggers: vi.fn(() => []),
}));

vi.mock('../../src/services/retention/integration.js', () => ({
  onJobObtained: vi.fn(async () => undefined),
  onPromotion: vi.fn(async () => undefined),
  onFired: vi.fn(async () => undefined),
  onMarriage: vi.fn(async () => undefined),
  onDating: vi.fn(async () => undefined),
  onChildBorn: vi.fn(async () => undefined),
  onFriendMade: vi.fn(async () => undefined),
  onBirthday: vi.fn(async () => undefined),
  onGraduation: vi.fn(async () => undefined),
}));

import { simulateLifetime } from './lifetime-simulator.js';

const ONE_YEAR_MIN = 365 * 24 * 60;

describe('simulator diagnostics (baseline probe, not a real assertion)', () => {
  it('reports 1-year message type counts and events fired', async () => {
    const r = await simulateLifetime({
      seed: 7,
      characterAge: 10,
      maxMinutes: ONE_YEAR_MIN,
    });

    const counts = Object.entries(r.messageTypeCounts)
      .sort((a, b) => b[1] - a[1])
      .slice(0, 15);

    // eslint-disable-next-line no-console
    console.log('\n=== SIMULATOR DIAGNOSTICS (seed=7, age=10, 1 year) ===');
    // eslint-disable-next-line no-console
    console.log('minutesSimulated:', r.minutesSimulated);
    // eslint-disable-next-line no-console
    console.log('finalAge:', r.finalAge);
    // eslint-disable-next-line no-console
    console.log('eventsFired (unique):', r.eventsFired.length, r.eventsFired.slice(0, 20));
    // eslint-disable-next-line no-console
    console.log('messageTypeCounts (top 15):', Object.fromEntries(counts));
    // eslint-disable-next-line no-console
    console.log('errors:', r.errors.length);
    // eslint-disable-next-line no-console
    console.log('invariantViolations:', r.invariantViolations.length);
    // eslint-disable-next-line no-console
    console.log('======================================================\n');
  }, 60_000);
});
