import { describe, it, expect } from 'vitest';
import {
  getTimePeriod,
  getTimeModifier,
  TIME_MODIFIERS,
  type TimePeriod,
  type TimeModifier,
} from './time-modifiers.js';

describe('getTimePeriod', () => {
  it('should return morning for hours 7-11', () => {
    expect(getTimePeriod(7)).toBe('morning');
    expect(getTimePeriod(8)).toBe('morning');
    expect(getTimePeriod(11)).toBe('morning');
  });

  it('should return afternoon for hours 12-17', () => {
    expect(getTimePeriod(12)).toBe('afternoon');
    expect(getTimePeriod(15)).toBe('afternoon');
    expect(getTimePeriod(17)).toBe('afternoon');
  });

  it('should return evening for hours 18-22', () => {
    expect(getTimePeriod(18)).toBe('evening');
    expect(getTimePeriod(20)).toBe('evening');
    expect(getTimePeriod(22)).toBe('evening');
  });

  it('should return night for hours 23-6', () => {
    expect(getTimePeriod(23)).toBe('night');
    expect(getTimePeriod(0)).toBe('night');
    expect(getTimePeriod(3)).toBe('night');
    expect(getTimePeriod(6)).toBe('night');
  });

  it('should handle boundary hours correctly', () => {
    expect(getTimePeriod(6)).toBe('night');
    expect(getTimePeriod(7)).toBe('morning');
    expect(getTimePeriod(11)).toBe('morning');
    expect(getTimePeriod(12)).toBe('afternoon');
    expect(getTimePeriod(17)).toBe('afternoon');
    expect(getTimePeriod(18)).toBe('evening');
    expect(getTimePeriod(22)).toBe('evening');
    expect(getTimePeriod(23)).toBe('night');
  });

  it('should wrap hours >= 24 using modulo', () => {
    expect(getTimePeriod(24)).toBe('night');   // 24 % 24 = 0
    expect(getTimePeriod(31)).toBe('morning'); // 31 % 24 = 7
  });
});

describe('getTimeModifier', () => {
  it('should return morning modifier with günaydın greeting', () => {
    const mod = getTimeModifier(9);
    expect(mod.period).toBe('morning');
    expect(mod.speedMultiplier).toBe(1.0);
    expect(mod.greetingStyle).toBe('günaydın');
    expect(mod.sleepActive).toBe(false);
  });

  it('should return afternoon modifier with normal speed', () => {
    const mod = getTimeModifier(14);
    expect(mod.period).toBe('afternoon');
    expect(mod.speedMultiplier).toBe(1.0);
    expect(mod.sleepActive).toBe(false);
  });

  it('should return evening modifier with faster speed', () => {
    const mod = getTimeModifier(20);
    expect(mod.period).toBe('evening');
    expect(mod.speedMultiplier).toBeLessThan(1.0);
    expect(mod.sleepActive).toBe(false);
  });

  it('should return night modifier with slow speed and sleep active', () => {
    const mod = getTimeModifier(2);
    expect(mod.period).toBe('night');
    expect(mod.speedMultiplier).toBeGreaterThan(1.0);
    expect(mod.sleepActive).toBe(true);
  });

  it('should return a copy, not a reference to TIME_MODIFIERS', () => {
    const mod = getTimeModifier(9);
    mod.speedMultiplier = 999;
    expect(TIME_MODIFIERS.morning.speedMultiplier).toBe(1.0);
  });
});

describe('TIME_MODIFIERS', () => {
  it('should have all four time periods defined', () => {
    const periods: TimePeriod[] = ['morning', 'afternoon', 'evening', 'night'];
    for (const p of periods) {
      expect(TIME_MODIFIERS[p]).toBeDefined();
      expect(TIME_MODIFIERS[p].period).toBe(p);
    }
  });

  it('should have consistent period field in each modifier', () => {
    for (const [key, mod] of Object.entries(TIME_MODIFIERS)) {
      expect(mod.period).toBe(key);
    }
  });

  it('night should be the only period with sleepActive true', () => {
    expect(TIME_MODIFIERS.morning.sleepActive).toBe(false);
    expect(TIME_MODIFIERS.afternoon.sleepActive).toBe(false);
    expect(TIME_MODIFIERS.evening.sleepActive).toBe(false);
    expect(TIME_MODIFIERS.night.sleepActive).toBe(true);
  });

  it('morning should be the only period with a greeting style', () => {
    expect(TIME_MODIFIERS.morning.greetingStyle).toBe('günaydın');
    expect(TIME_MODIFIERS.afternoon.greetingStyle).toBeUndefined();
    expect(TIME_MODIFIERS.evening.greetingStyle).toBeUndefined();
    expect(TIME_MODIFIERS.night.greetingStyle).toBeUndefined();
  });
});
