// Fix 1 (Session 7f) — computeFeaturesForProp must never throw, and
// must return the engine1 input shape (features/trap/consistency/prop)
// even when every upstream is missing.

const mockSupabaseState = {
  rosterRow: null,
  error: null,
};
jest.mock('../../src/utils/supabase', () => ({
  getSupabaseServiceClient: () => ({
    from() {
      const proxy = {
        select() { return proxy; },
        eq() { return proxy; },
        limit() { return proxy; },
        maybeSingle: () => Promise.resolve({ data: mockSupabaseState.rosterRow, error: mockSupabaseState.error }),
      };
      return proxy;
    },
  }),
}));

const mockAxiosGet = jest.fn();
jest.mock('axios', () => ({ get: (...args) => mockAxiosGet(...args) }));

const mockFeatures = { current: {}, throws: false };
jest.mock('../../src/services/intelligence/featureCache', () => ({
  getFeatures: async () => {
    if (mockFeatures.throws) throw new Error('feature-fetch boom');
    return { features: mockFeatures.current, meta: {} };
  },
  // Session 63 — computeFeatures now sources normalized per-game rows here
  // (feeds consistency + the probability estimator + game_count_in_7d).
  // Routed through the SAME mockLogs fixture the old gameLogService mock used,
  // so "logs available → consistency computed" keeps its original meaning.
  getStatRows: async () => mockLogs.current || [],
  gameCountInWindow: () => null,
}));

const mockTrap = { current: null, throws: false };
jest.mock('../../src/services/intelligence/trapDetection', () => ({
  getTrapScore: async () => {
    if (mockTrap.throws) throw new Error('trap boom');
    return mockTrap.current;
  },
  normalizeName: (n) => n,
}));

const mockLogs = { current: null };
const mockConsistency = { current: null };
jest.mock('../../src/services/intelligence/gameLogService', () => ({
  getGameLogs: async () => mockLogs.current,
  getCareerPlayoffGames: async () => null,
  getWithWithoutStats: async () => null,
}));
jest.mock('../../src/services/intelligence/consistencyScore', () => ({
  getConsistency: async () => mockConsistency.current || { consistency: 'reliable', cv: 0.2, score: 0.7, games: 20 },
}));

const { computeFeaturesForProp } = require('../../src/services/intelligence/computeFeatures');

beforeEach(() => {
  mockSupabaseState.rosterRow = null;
  mockSupabaseState.error = null;
  mockAxiosGet.mockReset();
  mockFeatures.current = {};
  mockFeatures.throws = false;
  mockTrap.current = null;
  mockTrap.throws = false;
  mockLogs.current = null;
  mockConsistency.current = null;
});

function nbaScoreboard(events) {
  return { status: 200, data: { events } };
}
function game(id, homeAbbr, awayAbbr) {
  return {
    id,
    competitions: [{
      competitors: [
        { homeAway: 'home', team: { abbreviation: homeAbbr } },
        { homeAway: 'away', team: { abbreviation: awayAbbr } },
      ],
    }],
  };
}

describe('computeFeaturesForProp — happy path', () => {
  test('resolves player + game + features + trap + consistency', async () => {
    mockSupabaseState.rosterRow = {
      display_name: 'Jalen Brunson', normalized_name: 'jalen brunson',
      espn_id: '3934672', team_abbr: 'NYK', sport: 'nba',
    };
    mockAxiosGet.mockResolvedValue(nbaScoreboard([game('ev-1', 'NYK', 'BOS')]));
    mockFeatures.current = { l5_avg: 28.4, l20_avg: 26.1, home_away: 1.0, opp_rank_stat: 0.82 };
    mockTrap.current = { composite: 0.12, signals: {}, active_count: 1, recommendation: 'proceed' };
    mockLogs.current = [{ points: 28 }, { points: 26 }];

    const out = await computeFeaturesForProp({
      player: 'Jalen Brunson', stat_type: 'points', line: 25.5, direction: 'over', sport: 'nba',
      // Session 64 — grading binds opponent features to the prop's REAL game.
      // Without a bound date it no longer guesses ESPN's "today".
      game_date: '2026-07-20',
    });

    expect(out.features.l5_avg).toBe(28.4);
    expect(out.features.home_away).toBe(1.0);
    expect(out.trap.composite).toBe(0.12);
    expect(out.consistency.consistency).toBe('reliable');
    expect(out.prop).toEqual({ line: 25.5, direction: 'over' });
    expect(out.meta.teamAbbr).toBe('NYK');
    expect(out.meta.opponentAbbr).toBe('BOS');
    expect(out.meta.gameId).toBe('ev-1');
    expect(out.meta.isHome).toBe(true);
    expect(out.meta.errors).toHaveLength(0);
  });
});

describe('computeFeaturesForProp — graceful degradation', () => {
  test('player not in player_id_map → errors logged, partial result returned', async () => {
    mockSupabaseState.rosterRow = null;
    const out = await computeFeaturesForProp({
      player: 'Unknown Person', stat_type: 'points', line: 20, direction: 'over', sport: 'nba',
    });
    expect(out.meta.errors).toContain('player_not_found_in_id_map');
    expect(out.meta.errors).toContain('no_game_scheduled_today');
    expect(out.meta.teamAbbr).toBeNull();
    expect(out.meta.gameId).toBeNull();
    expect(out.features).toEqual({});
    expect(out.prop.line).toBe(20);
  });

  test('player found but no game today → still returns features attempt', async () => {
    mockSupabaseState.rosterRow = { team_abbr: 'NYK', espn_id: '1', sport: 'nba' };
    mockAxiosGet.mockResolvedValue(nbaScoreboard([])); // empty slate
    const out = await computeFeaturesForProp({
      player: 'Some Player', stat_type: 'points', line: 22, direction: 'over', sport: 'nba', game_date: '2026-07-20',
    });
    expect(out.meta.errors).toContain('no_game_scheduled_today');
    expect(out.meta.teamAbbr).toBe('NYK');
    expect(out.meta.gameId).toBeNull();
  });

  test('feature fetch throws → ESPN fields empty, but static-context augmentation still surfaces (Session 15)', async () => {
    mockSupabaseState.rosterRow = { team_abbr: 'NYK', espn_id: '1', sport: 'nba' };
    mockAxiosGet.mockResolvedValue(nbaScoreboard([game('e2', 'NYK', 'BOS')]));
    mockFeatures.throws = true;
    const out = await computeFeaturesForProp({
      player: 'Brunson', stat_type: 'points', line: 25, direction: 'over', sport: 'nba', game_date: '2026-07-20',
    });
    expect(out.meta.errors).toContain('no_features_computed');
    // Session 15 — static lookups (pace factor, park factor) populate
    // regardless of ESPN fetch state. The contract used to be
    // "features is empty when ESPN fails"; the contract is now
    // "features may contain static context even when ESPN fails."
    // ESPN-derived fields (l5_avg, opp_rank_stat, ...) ARE absent.
    expect(out.features.l5_avg).toBeUndefined();
    expect(out.features.opp_rank_stat).toBeUndefined();
    // Pace factor lookup is static and stable for known team codes.
    expect(out.features.pace_factor).toBe(95); // NYK pace
    expect(out.features.opp_pace_factor).toBe(99); // BOS pace
    expect(out.trap).toBeDefined();
  });

  test('trap detection throws → defaults to no signals', async () => {
    mockSupabaseState.rosterRow = { team_abbr: 'NYK', espn_id: '1', sport: 'nba' };
    mockAxiosGet.mockResolvedValue(nbaScoreboard([game('e3', 'NYK', 'BOS')]));
    mockFeatures.current = { l5_avg: 25 };
    mockTrap.throws = true;
    const out = await computeFeaturesForProp({
      player: 'Brunson', stat_type: 'points', line: 25, direction: 'over', sport: 'nba', game_date: '2026-07-20',
    });
    expect(out.trap).toMatchObject({ composite: 0, recommendation: 'proceed' });
  });

  test('missing required fields surfaces in errors but still returns shape', async () => {
    const out = await computeFeaturesForProp({});
    expect(out.meta.errors[0]).toMatch(/missing required fields/);
    expect(out.features).toBeDefined();
    expect(out.trap).toBeDefined();
    expect(out.consistency).toBeDefined();
    expect(out.prop).toBeDefined();
  });

  test('scoreboard fetch throws → no game noted, no crash', async () => {
    mockSupabaseState.rosterRow = { team_abbr: 'NYK', espn_id: '1', sport: 'nba' };
    mockAxiosGet.mockRejectedValue(new Error('espn down'));
    const out = await computeFeaturesForProp({
      player: 'B', stat_type: 'points', line: 25, direction: 'over', sport: 'nba',
    });
    expect(out.meta.gameId).toBeNull();
    expect(out.meta.errors).toContain('no_game_scheduled_today');
  });

  test('does not import from legacy path (no propAnalyzer/grader/UnifiedOddsProvider)', () => {
    const fs = require('fs');
    const src = fs.readFileSync(require.resolve('../../src/services/intelligence/computeFeatures.js'), 'utf8');
    expect(src).not.toMatch(/propAnalyzer/);
    expect(src).not.toMatch(/require.*['"]\.\.\/grader/);
    expect(src).not.toMatch(/UnifiedOddsProvider/);
  });
});

describe('Session 64 — grading binds to the BOUND game, never "today"', () => {
  test('no bound game date → opponent features are NOT bound to a guessed game', async () => {
    mockSupabaseState.rosterRow = { team_abbr: 'NYK', espn_id: '1', sport: 'nba' };
    // A full scoreboard is available — the old code would have happily bound
    // this (possibly yesterday's) game. Without a bound date we must not.
    mockAxiosGet.mockResolvedValue(nbaScoreboard([game('ev-9', 'NYK', 'BOS')]));
    const out = await computeFeaturesForProp({
      player: 'Brunson', stat_type: 'points', line: 25, direction: 'over', sport: 'nba',
      // no game_date / game_time
    });
    expect(out.meta.errors).toContain('no_bound_game_date');
    expect(out.meta.gameId).toBeNull();
    expect(out.meta.opponentAbbr).toBeNull();
    expect(out.meta.isHome).toBeNull();
  });

  test('a bound date resolves the opponent for THAT date', async () => {
    mockSupabaseState.rosterRow = { team_abbr: 'NYK', espn_id: '1', sport: 'nba' };
    mockAxiosGet.mockResolvedValue(nbaScoreboard([game('ev-10', 'NYK', 'MIA')]));
    const out = await computeFeaturesForProp({
      player: 'Brunson', stat_type: 'points', line: 25, direction: 'over', sport: 'nba',
      game_date: '2026-07-20',
    });
    expect(out.meta.opponentAbbr).toBe('MIA');
    expect(out.meta.errors).not.toContain('no_bound_game_date');
  });
});
