package com.rj.scheduler;

import com.rj.service.HxrAdminOrderSelectService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.bean.override.mockito.MockitoBean;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

/**
 * {@link LBAdminPullScheduler#pullAdminOrders()} 集成测试：全量 Spring 上下文 + {@link LBAdminPullSchedulerTestClockConfiguration}，
 * HTTP 拉单由 {@link MockitoBean} 替身，避免外网与真实 Cookie。
 *
 * <p>另见：{@link LBAdminPullSchedulerWhenHxrDisabledIntegrationTest}、{@link LBAdminPullSchedulerWhenGlobalSchedulerOffIntegrationTest}。
 */
@SpringBootTest
@TestPropertySource(
        properties = {
                "spring.task.scheduling.enabled=false",
                "hxr.admin.pull-initial-delay-ms=604800000",
                "app.scheduler.start=true",
                "hxr.admin.enabled=true",
                "app.timezone=Asia/Shanghai"
        })
@Import(LBAdminPullSchedulerTestClockConfiguration.class)
@ActiveProfiles("test")
class LBAdminPullSchedulerIntegrationTest {

    @Autowired
    private LBAdminPullScheduler lbAdminPullScheduler;

    @Autowired
    private LBAdminPullSchedulerTestClockConfiguration.MutableClock mutableClock;

    @MockitoBean
    private HxrAdminOrderSelectService hxrAdminOrderSelectService;

    @BeforeEach
    void resetInvocationHistory() {
        Mockito.clearInvocations(hxrAdminOrderSelectService);
    }

    @Test
    void pullAdminOrders_eveningWindow_invokesOrderSelectService() throws Exception {
        mutableClock.setTo(
                ZonedDateTime.of(LocalDate.of(2026, 5, 15), LocalTime.of(20, 0), ZoneId.of("Asia/Shanghai")));

        lbAdminPullScheduler.pullAdminOrders();

        verify(hxrAdminOrderSelectService).fetchAndLogEachOrder();
    }

    @Test
    void pullAdminOrders_exactly1810_invokesOrderSelectService() throws Exception {
        mutableClock.setTo(
                ZonedDateTime.of(LocalDate.of(2026, 5, 15), LocalTime.of(18, 10), ZoneId.of("Asia/Shanghai")));

        lbAdminPullScheduler.pullAdminOrders();

        verify(hxrAdminOrderSelectService).fetchAndLogEachOrder();
    }

    @Test
    void pullAdminOrders_beforeWindow_doesNotInvoke() throws Exception {
        mutableClock.setTo(
                ZonedDateTime.of(LocalDate.of(2026, 5, 15), LocalTime.of(12, 0), ZoneId.of("Asia/Shanghai")));

        lbAdminPullScheduler.pullAdminOrders();

        verify(hxrAdminOrderSelectService, never()).fetchAndLogEachOrder();
    }

    @Test
    void pullAdminOrders_saturdayEveningInTimeWindow_doesNotInvoke() throws Exception {
        mutableClock.setTo(
                ZonedDateTime.of(LocalDate.of(2026, 5, 16), LocalTime.of(20, 0), ZoneId.of("Asia/Shanghai")));

        lbAdminPullScheduler.pullAdminOrders();

        verify(hxrAdminOrderSelectService, never()).fetchAndLogEachOrder();
    }
}
