import { describe, it, expect } from 'vitest';
import { calculateLakeMetrics, LakeCalculationConfig } from '../utils/calculations';

describe('calculateLakeMetrics', () => {
  const config: LakeCalculationConfig = {
    minLevel: 100,
    maxLevel: 110,
    storageLevel: 108,
    maxVolume: 50,
  };

  it('should calculate capacity based on reported volume when available', () => {
    // 25 / 50 = 50%
    const result = calculateLakeMetrics(105, 25, config);
    expect(result.capacity).toBe(50);
    expect(result.volume).toBe(25);
  });

  it('should cap capacity at 100% when volume exceeds maxVolume', () => {
    const result = calculateLakeMetrics(111, 55, config);
    expect(result.capacity).toBe(100);
    expect(result.volume).toBe(55);
  });

  it('should floor capacity at 0% when volume is negative', () => {
    const result = calculateLakeMetrics(99, -5, config);
    expect(result.capacity).toBe(0);
    expect(result.volume).toBe(-5);
  });

  it('should estimate capacity and volume from level when reported volume is 0', () => {
    // Level 105 is exactly halfway between 100 and 110 -> 50%
    // 50% of 50 maxVolume = 25
    const result = calculateLakeMetrics(105, 0, config);
    expect(result.capacity).toBe(50);
    expect(result.volume).toBe(25);
  });

  it('should cap estimated capacity at 100% when level exceeds maxLevel', () => {
    const result = calculateLakeMetrics(115, 0, config);
    expect(result.capacity).toBe(100);
    expect(result.volume).toBe(50); // 100% of 50
  });

  it('should floor estimated capacity at 0% when level is below minLevel', () => {
    const result = calculateLakeMetrics(90, 0, config);
    expect(result.capacity).toBe(0);
    expect(result.volume).toBe(0); // 0% of 50
  });

  it('should correctly calculate storageDiff', () => {
    const result = calculateLakeMetrics(106, 25, config);
    // 106 - 108 = -2.00
    expect(result.storageDiff).toBe(-2);
  });

  it('should calculate positive storageDiff when above storageLevel', () => {
    const result = calculateLakeMetrics(109, 25, config);
    // 109 - 108 = 1.00
    expect(result.storageDiff).toBe(1);
  });

  it('should handle missing config gracefully', () => {
    const emptyConfig: LakeCalculationConfig = {};
    const result = calculateLakeMetrics(105, 0, emptyConfig);
    expect(result.capacity).toBe(0);
    expect(result.volume).toBe(0);
    expect(result.storageDiff).toBe(0);
  });
});
