// Ein-Quellen-Adapter (Review-Befund 10.08.2026): rowsFromMetricDiffs muss den
// in-memory-Diff verlustfrei in die Row-Form übersetzen, damit Kacheln,
// Waterfall, Kostenstruktur und Brücke aus GENAU einer Quelle stammen.
import { describe, it, expect } from 'vitest'
import { rowsFromMetricDiffs, buildBridge, buildBuckets, type MetricDiffLike } from '../summary-view'

function diff(overrides: Partial<MetricDiffLike>): MetricDiffLike {
  return {
    metricKey: 'quotationPrice',
    altValue: 100,
    neuValue: 110,
    deltaAbsolute: 10,
    deltaPercent: 10,
    currency: 'EUR',
    status: 'anstieg',
    sourceAlt: 'P29',
    sourceNeu: 'P29',
    ...overrides,
  }
}

describe('rowsFromMetricDiffs', () => {
  it('maps every field 1:1 without recomputation', () => {
    const [row] = rowsFromMetricDiffs([
      diff({ labelFileAlt: 'Angebotspreis', labelVerifiedAlt: true }),
    ])
    expect(row).toEqual({
      metric_key: 'quotationPrice',
      alt_value: 100,
      neu_value: 110,
      delta_absolute: 10,
      delta_percent: 10,
      currency: 'EUR',
      status: 'anstieg',
      source_alt: 'P29',
      source_neu: 'P29',
      label_file_alt: 'Angebotspreis',
      label_file_neu: null,
      label_verified_alt: true,
      label_verified_neu: null,
    })
  })

  it('feeds buildBridge/buildBuckets: the translated rows produce a bridge from the quotationPrice line', () => {
    const rows = rowsFromMetricDiffs([
      diff({}),
      diff({ metricKey: 'materialCosts', altValue: 40, neuValue: 46, deltaAbsolute: 6, deltaPercent: 15 }),
      diff({ metricKey: 'totalProductionCosts', altValue: 60, neuValue: 66, deltaAbsolute: 6, deltaPercent: 10 }),
    ])
    expect(buildBridge(rows)).not.toBeNull()
    expect(buildBuckets(rows)).not.toBeNull()
  })

  it('returns an empty array for an empty diff (no fabricated rows)', () => {
    expect(rowsFromMetricDiffs([])).toEqual([])
  })
})
