package com.aps.service.impl;

import com.aps.entity.RoutingHeader;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class LanuchServiceImplTest {

    @Test
    void autoInsertPrefersNewestRoutingAlreadyUsedBySameMaterialInScene() {
        RoutingHeader newestOtherRouting = routing(300, LocalDate.of(2026, 7, 20));
        RoutingHeader newestExistingRouting = routing(200, LocalDate.of(2026, 7, 19));
        RoutingHeader olderExistingRouting = routing(100, LocalDate.of(2026, 7, 18));

        RoutingHeader selected = LanuchServiceImpl.selectExistingSceneRouting(
                Arrays.asList(newestOtherRouting, newestExistingRouting, olderExistingRouting),
                new HashSet<>(Arrays.asList(100, 200)));

        assertEquals(Integer.valueOf(200), selected.getId());
    }

    @Test
    void autoInsertFallsBackWhenSceneHasNoRoutingForMaterial() {
        RoutingHeader selected = LanuchServiceImpl.selectExistingSceneRouting(
                Collections.singletonList(routing(300, LocalDate.of(2026, 7, 20))),
                Collections.singleton(100));

        assertNull(selected);
    }

    private RoutingHeader routing(int id, LocalDate creationTime) {
        RoutingHeader routingHeader = new RoutingHeader();
        routingHeader.setId(id);
        routingHeader.setCreationTime(creationTime);
        return routingHeader;
    }
}
