// Soccer reasoning tests. We mock computeFeaturesForProp so the test
// only exercises buildConcreteReasoning's soccer branch + the
// downstream toLegacyShape adapter; data layer is out of scope here.

const mockComputeFeaturesForProp = jest.fn();
jest.mock('../../src/services/intelligence/computeFeatures', () => ({
  computeFeaturesForProp: (...args) => mockComputeFeaturesForProp(...args),
}));

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

function soccerFeatureResult(features = {}, meta = {}) {
  return {
    features: {
      l5_avg: null,
      l20_avg: null,
      home_away: null,
      opp_rank_stat: null,
      rest_days: null,
      ...features,
    },
    trap: { composite: 0, signals: {}, active_count: 0, recommendation: 'proceed' },
    consistency: { consistency: 'unknown', score: null, games: 0 },
    prop: { line: 0.5, direction: 'over' },
    meta: {
      player: 'Test Player', statType: 'goals', line: 0.5, direction: 'over',
      book: 'unknown', sport: 'soccer', league: 'WC',
      teamAbbr: 'England', opponentAbbr: 'Brazil',
      venue: 'MetLife Stadium', referee: null,
      isHome: true, gameLogs: [], errors: [],
      ...meta,
    },
  };
}

beforeEach(() => {
  mockComputeFeaturesForProp.mockReset();
});

describe('analyzeViaEngine1 — soccer reasoning', () => {
  test('uses "matches" language and surfaces goals_per_90', async () => {
    mockComputeFeaturesForProp.mockResolvedValueOnce(soccerFeatureResult({
      goals_per_90: 0.82,
    }));
    const result = await analyzeViaEngine1({
      player: 'Harry Kane', stat_type: 'goals', line: 0.5, direction: 'over', sport: 'soccer',
    });
    expect(result.reasoning.summary).toMatch(/0\.82 goals per 90 minutes/);
    // Sanity: no NBA-flavored language.
    expect(result.reasoning.summary).not.toMatch(/last 5 games/);
    expect(result.reasoning.summary).not.toMatch(/back-to-back/i);
  });

  test('xG overperformance triggers the regression line', async () => {
    mockComputeFeaturesForProp.mockResolvedValueOnce(soccerFeatureResult({
      goals_per_90: 1.2, xg_per_90: 0.7, xg_delta: 0.71,
    }));
    const result = await analyzeViaEngine1({
      player: 'Striker', stat_type: 'goals', line: 0.5, direction: 'over', sport: 'soccer',
    });
    expect(result.reasoning.summary).toMatch(/Expected goals \(xG\): 0\.70 per 90/);
    expect(result.reasoning.summary).toMatch(/overperforming.*regression risk/i);
  });

  test('penalty taker status surfaced when true', async () => {
    mockComputeFeaturesForProp.mockResolvedValueOnce(soccerFeatureResult({
      goals_per_90: 0.5, is_penalty_taker: true,
    }));
    const result = await analyzeViaEngine1({
      player: 'PK Taker', stat_type: 'goals', line: 0.5, direction: 'over', sport: 'soccer',
    });
    expect(result.reasoning.summary).toMatch(/Designated penalty taker/);
  });

  test('altitude impact surfaces with venue context', async () => {
    // goals_per_90 present: the model needs a projection to grade at all
    // (Session 58 — no projection ⇒ INSUFFICIENT DATA refusal).
    mockComputeFeaturesForProp.mockResolvedValueOnce(soccerFeatureResult({
      goals_per_90: 0.4, altitude_impact: 'high', venue_altitude_ft: 7349, home_continent: false,
    }, { venue: 'Estadio Azteca' }));
    const result = await analyzeViaEngine1({
      player: 'Visitor', stat_type: 'goals', line: 0.5, direction: 'over', sport: 'soccer',
    });
    expect(result.reasoning.summary).toMatch(/7349ft altitude/);
    expect(result.reasoning.summary).toMatch(/non-acclimatized/i);
  });

  test('low minutes per game triggers the discount note', async () => {
    mockComputeFeaturesForProp.mockResolvedValueOnce(soccerFeatureResult({
      goals_per_90: 0.5, minutes_per_game: 58,
    }));
    const result = await analyzeViaEngine1({
      player: 'Rotation Player', stat_type: 'goals', line: 0.5, direction: 'over', sport: 'soccer',
    });
    expect(result.reasoning.summary).toMatch(/58 minutes per match/);
    expect(result.reasoning.summary).toMatch(/line may assume full 90/);
  });

  test('referee card rate surfaces when present', async () => {
    // l5_avg present: the model needs a projection to grade (Session 58).
    mockComputeFeaturesForProp.mockResolvedValueOnce(soccerFeatureResult({
      l5_avg: 1.2, referee_cards_per_game: 5.4, referee_name: 'Anthony Taylor',
    }));
    const result = await analyzeViaEngine1({
      player: 'Anyone', stat_type: 'cards', line: 0.5, direction: 'over', sport: 'soccer',
    });
    expect(result.reasoning.summary).toMatch(/Anthony Taylor averages 5\.4 cards per match/);
  });

  test('soccer path skips NBA-only sentences (no injuries / no back-to-back)', async () => {
    // Even if soccer features somehow carry an injury_severity_score (they
    // shouldn't), the soccer branch must not surface it with NBA language.
    mockComputeFeaturesForProp.mockResolvedValueOnce(soccerFeatureResult({
      goals_per_90: 0.5, injury_severity_score: 3, game_count_in_7d: 5,
    }));
    const result = await analyzeViaEngine1({
      player: 'Player', stat_type: 'goals', line: 0.5, direction: 'over', sport: 'soccer',
    });
    expect(result.reasoning.summary).not.toMatch(/opponent starter\(s\)/i);
    expect(result.reasoning.summary).not.toMatch(/games in the last week/i);
    expect(result.reasoning.summary).not.toMatch(/back-to-back/i);
  });

  test('engine1 grade closer still applies on soccer (sport-agnostic)', async () => {
    mockComputeFeaturesForProp.mockResolvedValueOnce(soccerFeatureResult({
      goals_per_90: 1.5, l5_avg: 1.5, l20_avg: 1.2,
    }));
    const result = await analyzeViaEngine1({
      player: 'Top Scorer', stat_type: 'goals', line: 0.5, direction: 'over', sport: 'soccer',
    });
    // The engine grade line is appended for every sport.
    expect(result.reasoning.summary).toMatch(/Engine 1 graded/);
  });
});
