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

import {
  buildUnlockedPlayerPropPatch,
  extractMlbPropContext,
  formatPlayerPropLabel,
  normalizeForecastDirection,
  shouldUsePerPlayerProps,
} from '@/lib/player-props';

describe('shouldUsePerPlayerProps', () => {
  it('returns true when per-player mode has assets', () => {
    expect(shouldUsePerPlayerProps({ mode: 'per_player', playerProps: [{ assetId: 'a1' }] })).toBe(true);
  });

  it('returns false when per-player mode is empty', () => {
    expect(shouldUsePerPlayerProps({ mode: 'per_player', playerProps: [] })).toBe(false);
  });

  it('returns false for legacy team mode', () => {
    expect(shouldUsePerPlayerProps({ mode: 'team', playerProps: [{ assetId: 'a1' }] })).toBe(false);
  });

  it('extracts transformed mlb prop context from payload model_context', () => {
    expect(extractMlbPropContext({
      model_context: {
        k_rank: 4,
        park_factor: 108,
        weather_impact: 'positive',
        handedness_split: 'favorable',
        lineup_certainty: 'confirmed',
      },
    })).toEqual({
      kRank: 4,
      parkFactor: 108,
      weatherImpact: 'positive',
      handednessSplit: 'favorable',
      lineupCertainty: 'confirmed',
    });
  });

  it('builds an unlock patch that preserves mlb prop context for the popup', () => {
    expect(buildUnlockedPlayerPropPatch({
      confidence: 0.77,
      payload: {
        recommendation: 'over',
        market_line_value: 1.5,
        projected_probability: 68,
        forecast_direction: 'OVER',
        signal_tier: 'GOOD',
        agreement_score: 71,
        market_quality_label: 'FAIR',
        model_context: {
          k_rank: 4,
          park_factor: 108,
          weather_impact: 'positive',
        },
      },
    })).toMatchObject({
      locked: false,
      recommendation: 'over',
      confidence: 0.77,
      marketLine: 1.5,
      marketLineValue: 1.5,
      projectedProbability: 68,
      forecastDirection: 'OVER',
      signalTier: 'GOOD',
      agreementScore: 71,
      marketQualityLabel: 'FAIR',
      modelContext: {
        k_rank: 4,
        park_factor: 108,
        weather_impact: 'positive',
      },
      mlbPropContext: {
        kRank: 4,
        parkFactor: 108,
        weatherImpact: 'positive',
        handednessSplit: null,
        lineupCertainty: null,
      },
    });
  });

  it('normalizes contradictory prop labels from the stored recommendation', () => {
    expect(formatPlayerPropLabel('Rebounds Over 4.5', 'under', '4.5')).toBe('Rebounds Under 4.5');
    expect(formatPlayerPropLabel('Points 1.5', 'over', 1.5)).toBe('Points Over 1.5');
  });

  it('returns null for invalid forecast directions', () => {
    expect(normalizeForecastDirection('OVER')).toBe('OVER');
    expect(normalizeForecastDirection('under')).toBe('UNDER');
    expect(normalizeForecastDirection('lock')).toBeNull();
    expect(normalizeForecastDirection(null)).toBeNull();
  });
});
