package com.starry.admin.modules.withdraw.service.impl;

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

import com.starry.admin.modules.order.module.entity.PlayOrderInfoEntity;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;

class EarningsBackfillServiceImplTest {

    private final EarningsBackfillServiceImpl service = new EarningsBackfillServiceImpl();

    @SuppressWarnings("unchecked")
    private BigDecimal invokeResolveAmount(PlayOrderInfoEntity order, List<String> warnings) throws Exception {
        Method method = EarningsBackfillServiceImpl.class.getDeclaredMethod("resolveAmount", PlayOrderInfoEntity.class,
                List.class);
        method.setAccessible(true);
        return (BigDecimal) method.invoke(service, order, warnings);
    }

    @Test
    void resolveAmount_acceptsZeroAmountWithoutWarnings() throws Exception {
        PlayOrderInfoEntity order = new PlayOrderInfoEntity();
        order.setId("order-zero");
        order.setAcceptBy("clerk-001");
        order.setOrderEndTime(LocalDateTime.now());
        order.setOrderMoney(BigDecimal.ZERO);

        List<String> warnings = new ArrayList<>();
        BigDecimal amount = invokeResolveAmount(order, warnings);

        assertEquals(BigDecimal.ZERO, amount);
        assertTrue(warnings.isEmpty());
    }

    @Test
    void resolveAmount_rejectsNegativeAmount() throws Exception {
        PlayOrderInfoEntity order = new PlayOrderInfoEntity();
        order.setId("order-negative");
        order.setAcceptBy("clerk-001");
        order.setOrderEndTime(LocalDateTime.now());
        order.setEstimatedRevenue(BigDecimal.valueOf(-5));

        List<String> warnings = new ArrayList<>();
        BigDecimal amount = invokeResolveAmount(order, warnings);

        assertNull(amount);
        assertEquals(1, warnings.size());
        assertTrue(warnings.get(0).contains("order-negative"));
    }
}
