import { describe, it, expect } from 'vitest'
import { computeMixResult, ENERGY_SOURCES, EnergiemixSimulation } from '../../src/sims/sim-09-energiemix/logic'

describe('Energiemix — Mix-Berechnung', () => {

  it('100% Kohle → hoher CO₂-Ausstoß', () => {
    const result = computeMixResult({ coal: 100 })
    expect(result.totalCO2).toBeGreaterThan(700)
    expect(result.renewableShare).toBe(0)
  })

  it('100% Wind → niedriger CO₂, niedrige Zuverlässigkeit', () => {
    const result = computeMixResult({ wind: 100 })
    expect(result.totalCO2).toBeLessThan(20)
    expect(result.totalReliability).toBeLessThan(0.5)
  })

  it('100% Solar → billigste Option', () => {
    const solarCost = computeMixResult({ solar: 100 }).totalCost
    const coalCost = computeMixResult({ coal: 100 }).totalCost
    expect(solarCost).toBeLessThan(coalCost)
  })

  it('gemischter Mix → Werte dazwischen', () => {
    const result = computeMixResult({ coal: 30, wind: 30, solar: 20, hydro: 20 })
    expect(result.totalCO2).toBeGreaterThan(50)
    expect(result.totalCO2).toBeLessThan(400)
    expect(result.renewableShare).toBe(70)
  })

  it('Erneuerbare-Anteil wird korrekt berechnet', () => {
    const result = computeMixResult({ wind: 50, solar: 50 })
    expect(result.renewableShare).toBe(100)
  })

  it('Score bevorzugt saubere + zuverlässige + günstige Mixes', () => {
    const dirty = computeMixResult({ coal: 100 })
    const balanced = computeMixResult({ nuclear: 30, wind: 30, hydro: 20, solar: 20 })
    expect(balanced.score).toBeGreaterThan(dirty.score)
  })

  it('leerer Mix gibt Nullwerte', () => {
    const result = computeMixResult({})
    expect(result.totalCO2).toBe(0)
  })
})

describe('Energiemix — Quellenddaten', () => {

  it('hat 6 Energiequellen', () => {
    expect(ENERGY_SOURCES).toHaveLength(6)
  })

  it('Wind und Solar sind erneuerbar', () => {
    const wind = ENERGY_SOURCES.find(s => s.id === 'wind')!
    const solar = ENERGY_SOURCES.find(s => s.id === 'solar')!
    expect(wind.renewable).toBe(true)
    expect(solar.renewable).toBe(true)
  })

  it('Kohle hat den höchsten CO₂-Wert', () => {
    const coal = ENERGY_SOURCES.find(s => s.id === 'coal')!
    const maxCO2 = Math.max(...ENERGY_SOURCES.map(s => s.co2PerGWh))
    expect(coal.co2PerGWh).toBe(maxCO2)
  })
})

describe('EnergiemixSimulation — Klasse', () => {

  it('hat korrekte Metadaten', () => {
    const sim = new EnergiemixSimulation()
    expect(sim.meta.id).toBe('sim-09')
    expect(sim.meta.primaryLevel).toBe(6)
  })

  it('Startwerte summieren sich auf ~100%', () => {
    const sim = new EnergiemixSimulation()
    const total = ENERGY_SOURCES.reduce((s, src) => s + sim.getVariable(src.id), 0)
    expect(total).toBe(100)
  })

  it('compute() gibt sinnvolle Werte', () => {
    const sim = new EnergiemixSimulation()
    const result = sim.compute()
    expect(result.totalCO2).toBeGreaterThan(0)
    expect(result.totalCost).toBeGreaterThan(0)
  })

  it('Variablenänderung loggt im Assessment', () => {
    const sim = new EnergiemixSimulation()
    sim.setVariable('coal', 0)
    sim.setVariable('solar', 50)
    const assessment = sim.getAssessmentData()
    expect(assessment.processLog.length).toBeGreaterThanOrEqual(2)
  })
})
