package com.njcn.gather.tool.adddata.component;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 时间槽计算测试。
 */
class AddDataTimeSlotCalculatorTest {

    private final AddDataTimeSlotCalculator calculator = new AddDataTimeSlotCalculator();

    @Test
    void shouldAlignToNextNaturalSlot() {
        LocalDateTime start = LocalDateTime.of(2026, 4, 28, 10, 7, 12);
        LocalDateTime end = LocalDateTime.of(2026, 4, 28, 10, 22, 0);

        List<LocalDateTime> slots = calculator.buildTimeSlots(start, end, 5);

        Assertions.assertEquals(3, slots.size());
        Assertions.assertEquals(LocalDateTime.of(2026, 4, 28, 10, 10, 0), slots.get(0));
        Assertions.assertEquals(LocalDateTime.of(2026, 4, 28, 10, 20, 0), slots.get(2));
    }

    @Test
    void shouldReturnEmptyWhenRangeDoesNotContainAnySlot() {
        LocalDateTime start = LocalDateTime.of(2026, 4, 28, 10, 7, 0);
        LocalDateTime end = LocalDateTime.of(2026, 4, 28, 10, 9, 59);

        List<LocalDateTime> slots = calculator.buildTimeSlots(start, end, 10);

        Assertions.assertTrue(slots.isEmpty());
    }

    @Test
    void shouldBuildHourlySlotsFromNextNaturalHour() {
        LocalDateTime start = LocalDateTime.of(2026, 4, 28, 10, 7, 0);
        LocalDateTime end = LocalDateTime.of(2026, 4, 28, 13, 0, 0);

        List<LocalDateTime> slots = calculator.buildHourlyTimeSlots(start, end);

        Assertions.assertEquals(3, slots.size());
        Assertions.assertEquals(LocalDateTime.of(2026, 4, 28, 11, 0, 0), slots.get(0));
        Assertions.assertEquals(LocalDateTime.of(2026, 4, 28, 13, 0, 0), slots.get(2));
    }

    @Test
    void shouldIncludeStartWhenAlreadyAtNaturalHour() {
        LocalDateTime start = LocalDateTime.of(2026, 4, 28, 10, 0, 0);
        LocalDateTime end = LocalDateTime.of(2026, 4, 28, 12, 30, 0);

        List<LocalDateTime> slots = calculator.buildHourlyTimeSlots(start, end);

        Assertions.assertEquals(3, slots.size());
        Assertions.assertEquals(LocalDateTime.of(2026, 4, 28, 10, 0, 0), slots.get(0));
        Assertions.assertEquals(LocalDateTime.of(2026, 4, 28, 12, 0, 0), slots.get(2));
    }
}
