'use strict';

const svc = require('../../src/services/outcomeService');
const { settleResult, gradeBucket, dateStrings, statValue, outcomeKey } = svc.__internals;

// A tiny in-memory Redis so settleSnapshot round-trips through cacheGet/cacheSet.
function memCache(seed = {}) {
  const store = { ...seed };
  return {
    store,
    cacheGet: async (k) => (k in store ? store[k] : null),
    cacheSet: async (k, v) => { store[k] = v; return true; },
  };
}

const ISO = '2026-07-10T02:00:00.000Z'; // ~10pm ET Jul 9 — exercises the ET rollover

function snapshot(grades) {
  return { sport: 'mlb', updated_at: ISO, grades };
}
function grade(over = {}) {
  return {
    player: over.player || 'Aaron Judge',
    stat_type: over.stat || 'hits',
    line: over.line != null ? over.line : 1.5,
    direction: over.side || 'over',
    grade: over.grade || 'A',
    gradedAt: { line: over.line != null ? over.line : 1.5, odds: -115, timestamp: ISO },
  };
}
// Game-log rows keyed to the ET or UTC date of ISO.
function log(date, stat) { return [{ date, opponent: 'BOS', stat }]; }

describe('outcomeService — settlement math', () => {
  test('over: actual above line = hit, below = miss, equal = push', () => {
    expect(settleResult('over', 2, 1.5)).toBe('hit');
    expect(settleResult('over', 1, 1.5)).toBe('miss');
    expect(settleResult('over', 2, 2)).toBe('push');
  });
  test('under: actual below line = hit, above = miss', () => {
    expect(settleResult('under', 1, 1.5)).toBe('hit');
    expect(settleResult('under', 2, 1.5)).toBe('miss');
    expect(settleResult('U', 3, 3)).toBe('push');
  });
  test('non-numeric actual/line → null (unsettleable)', () => {
    expect(settleResult('over', null, 1.5)).toBeNull();
    expect(settleResult('over', 2, undefined)).toBeNull();
  });
  test('gradeBucket tiers: A+ stands alone, letters collapse', () => {
    expect(gradeBucket('A+')).toBe('A+');
    expect(gradeBucket('A-')).toBe('A');
    expect(gradeBucket('B+')).toBe('B');
    expect(gradeBucket('C')).toBe('C');
    expect(gradeBucket('')).toBeNull();
  });
  test('statValue maps VYNDR stat_type → game-log field', () => {
    expect(statValue({ totalBases: 3 }, 'total_bases')).toBe(3);
    expect(statValue({ homeRuns: 1 }, 'home_runs')).toBe(1);
    expect(statValue({ strikeOuts: 7 }, 'strikeouts')).toBe(7);
    expect(statValue({ hits: 2 }, 'unknown_stat')).toBeNull();
  });
  test('Session 56 — newly-wired MLB fields settle (rbi, doubles, outs)', () => {
    expect(statValue({ rbi: 2 }, 'rbi')).toBe(2);
    expect(statValue({ doubles: 1 }, 'doubles')).toBe(1);
    expect(statValue({ outs: 18 }, 'outs')).toBe(18);
    expect(statValue({ baseOnBalls: 3 }, 'walks')).toBe(3);
    expect(statValue({ earnedRuns: 1 }, 'earned_runs')).toBe(1);
  });
  test('dateStrings yields both UTC and ET calendar dates', () => {
    const ds = dateStrings(ISO);
    expect(ds).toContain('2026-07-10'); // UTC
    expect(ds).toContain('2026-07-09'); // ET (10pm prior day)
  });
});

describe('outcomeService — settleSnapshot', () => {
  const judgeStats = async (name) => {
    if (name === 'Aaron Judge') return { found: true, last10: log('2026-07-09', { hits: 2, totalBases: 4 }) };
    return { found: false };
  };

  test('settles a hit against the real game log', async () => {
    const cache = memCache({ 'snapshot:mlb:latest': snapshot([grade({ stat: 'hits', line: 1.5, side: 'over', grade: 'A' })]) });
    const res = await svc.settleSnapshot('mlb', { ...cache, getPlayerStats: judgeStats, now: () => '2026-07-10T15:00:00Z' });
    expect(res.settled).toBe(1);
    expect(res.log[0]).toMatchObject({ result: 'hit', actual: 2, grade: 'A', side: 'O' });
    expect(cache.store['accuracy:mlb'].byGrade['A'].hits).toBe(1);
    expect(cache.store['accuracy:mlb'].overall.pct).toBe(100);
  });

  test('settles a miss (under, actual above line)', async () => {
    const cache = memCache({ 'snapshot:mlb:latest': snapshot([grade({ stat: 'hits', line: 1.5, side: 'under', grade: 'B+' })]) });
    const res = await svc.settleSnapshot('mlb', { ...cache, getPlayerStats: judgeStats, now: () => '2026-07-10T15:00:00Z' });
    expect(res.log[0].result).toBe('miss');
    expect(cache.store['accuracy:mlb'].byGrade['B'].misses).toBe(1);
  });

  test('is idempotent — a second run does not double-count', async () => {
    const cache = memCache({ 'snapshot:mlb:latest': snapshot([grade({ grade: 'A' })]) });
    const deps = { ...cache, getPlayerStats: judgeStats, now: () => '2026-07-10T15:00:00Z' };
    await svc.settleSnapshot('mlb', deps);
    const res2 = await svc.settleSnapshot('mlb', deps);
    expect(res2.settled).toBe(0);
    expect(res2.log.length).toBe(1);
    expect(cache.store['accuracy:mlb'].overall.total).toBe(1);
  });

  test('a prop with no matching game stays pending', async () => {
    const noGame = async () => ({ found: true, last10: log('2026-01-01', { hits: 0 }) });
    const cache = memCache({ 'snapshot:mlb:latest': snapshot([grade()]) });
    const res = await svc.settleSnapshot('mlb', { ...cache, getPlayerStats: noGame, now: () => '2026-07-10T15:00:00Z' });
    expect(res.settled).toBe(0);
    expect(res.pending).toBe(1);
  });

  test('offline stats (found:false) → pending, never throws', async () => {
    const cache = memCache({ 'snapshot:nba:latest': snapshot([grade()]) });
    const res = await svc.settleSnapshot('nba', { ...cache, getPlayerStats: async () => ({ found: false }), now: () => '2026-07-10T15:00:00Z' });
    expect(res.settled).toBe(0);
    expect(res.pending).toBe(1);
  });
});

describe('outcomeService — accuracy aggregation', () => {
  test('pct excludes pushes and gates on the 30-day window', () => {
    const nowIso = '2026-07-10T00:00:00Z';
    const mk = (grade, result, date) => ({ grade, result, date, player: 'X', stat: 'hits', line: 1.5, side: 'O' });
    const logRows = [
      mk('A', 'hit', '2026-07-09'),
      mk('A', 'hit', '2026-07-08'),
      mk('A', 'miss', '2026-07-07'),
      mk('A', 'push', '2026-07-06'),
      mk('A', 'hit', '2026-01-01'), // outside 30d — excluded
    ];
    const acc = svc.computeAccuracy('mlb', logRows, nowIso);
    expect(acc.byGrade['A'].hits).toBe(2);
    expect(acc.byGrade['A'].misses).toBe(1);
    expect(acc.byGrade['A'].pushes).toBe(1);
    expect(acc.byGrade['A'].pct).toBe(67); // 2/(2+1) rounded
    expect(acc.sample).toBe(4);            // 4 in-window, push counts toward sample
  });

  test('accuracyBuckets flattens only non-empty tiers', () => {
    const acc = svc.computeAccuracy('mlb', [
      { grade: 'A', result: 'hit', date: '2026-07-09' },
      { grade: 'B', result: 'miss', date: '2026-07-09' },
    ], '2026-07-10T00:00:00Z');
    const buckets = svc.accuracyBuckets(acc);
    expect(buckets.map((b) => b.grade).sort()).toEqual(['A', 'B']);
  });

  test('getAccuracy is cold-cache safe', async () => {
    const cache = memCache();
    const out = await svc.getAccuracy(cache);
    expect(out.overall.overall.total).toBe(0);
    expect(out.sports).toEqual({});
  });

  test('recomputeOverall merges every sport log', async () => {
    const cache = memCache({
      'outcomes:mlb:log': [{ grade: 'A', result: 'hit', date: '2026-07-09' }],
      'outcomes:nba:log': [{ grade: 'A', result: 'miss', date: '2026-07-09' }],
    });
    const acc = await svc.recomputeOverall({ ...cache, now: () => '2026-07-10T00:00:00Z' });
    expect(acc.overall.total).toBe(2);
    expect(acc.overall.pct).toBe(50);
    expect(cache.store['accuracy:overall']).toBeTruthy();
  });
});
