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

describe('model_signals compatibility helpers', () => {
  beforeEach(() => {
    delete process.env.FORECAST_PAYLOAD_V2;
    delete process.env.RIE_NATIVE_OUTPUT;
  });

  it('reads both legacy and native shapes through getLegacyCompositeView/getStoredPayloadExtensions', async () => {
    const { getLegacyCompositeView, getStoredPayloadExtensions } = await import('../rie');

    const legacy = {
      compositeConfidence: 0.78,
      stormCategory: 4,
      modelSignals: {
        grok: { confidence: 0.73, valueRating: 8 },
        piff: { avgEdge: 6.1, legCount: 2, topPicks: [] },
        digimon: null,
        dvp: null,
      },
      edgeBreakdown: { grokScore: 0.73, piffScore: 0.61, digimonScore: null, weights: { grok: 0.6, piff: 0.4 } },
      compositeVersion: 'v2',
      rieSignals: [{ signalId: 'piff' }],
      ragInsights: [{ query: 'pace' }],
      strategyId: 'nba',
    };

    const native = {
      compositeConfidence: 0.81,
      stormCategory: 5,
      signals: [{ signalId: 'piff', available: true, score: 0.82, weight: 0.4, rawData: { avgEdge: 5.2, legCount: 2, topPicks: [] }, metadata: { latencyMs: 12, source: 'db', freshness: '2026-03-27' } }],
      strategyProfile: { league: 'nba', signalWeights: { grok: 0.6, piff: 0.4 }, requiredSignals: [], optionalSignals: [], ragQueries: [] },
      ragInsights: [{ query: 'tempo', topChunks: [], applicableTheory: 'pace', cached: true }],
      edgeBreakdown: { grok: { score: 0.77, weight: 0.6, contribution: 0.46 }, piff: { score: 0.82, weight: 0.4, contribution: 0.33 } },
      inputQuality: { piff: 'A', dvp: 'B', digimon: 'N/A', odds: 'A', rag: 'A', overall: 'A' },
      compositeVersion: 'rm_2.0',
    };

    expect(getLegacyCompositeView(legacy)?.modelSignals.grok?.confidence).toBe(0.73);
    expect(getLegacyCompositeView(native, 0.77, 8)?.modelSignals.piff?.avgEdge).toBe(5.2);
    expect(getStoredPayloadExtensions(legacy)).toEqual({
      rieSignals: [{ signalId: 'piff' }],
      ragInsights: [{ query: 'pace' }],
      strategyId: 'nba',
    });
    expect(getStoredPayloadExtensions(native)).toEqual({
      rieSignals: native.signals,
      ragInsights: native.ragInsights,
      strategyId: 'nba',
    });
  });
});
