package com.ruoyi.web.modules.trading.support;

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

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Collections;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.trading.domain.dto.AppSupplierAmountBucketDto;
import com.ruoyi.web.modules.trading.domain.dto.AppSupplierSettlementSummaryDto;
import com.ruoyi.web.modules.trading.domain.dto.AppSupplierTradeSummaryDto;
import com.ruoyi.web.modules.trading.domain.vo.AppSupplierTradeSummaryVo;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("AppSupplierHomeSupport")
class AppSupplierHomeSupportTest
{
    @Test
    @DisplayName("交易汇总")
    void buildTradeSummary()
    {
        AppSupplierTradeSummaryDto raw = new AppSupplierTradeSummaryDto();
        raw.setOrderCount(2L);
        raw.setTotalAmount(new BigDecimal("74300"));
        raw.setTotalHeads(6L);
        raw.setPayingBuyerCount(2L);

        AppSupplierTradeSummaryVo vo = AppSupplierHomeSupport.buildTradeSummary(raw);
        assertEquals(2L, vo.getOrderCount());
        assertEquals(0, new BigDecimal("74300").compareTo(vo.getTotalAmount()));
        assertEquals(6L, vo.getTotalHeads());
        assertEquals(2L, vo.getPayingBuyerCount());
    }

    @Test
    @DisplayName("隔离中耳标去重并剔除病死")
    void countIsolationEarTags()
    {
        assertEquals(2L, AppSupplierHomeSupport.countIsolationEarTags(
                Arrays.asList("A,B", "B,C"),
                Collections.singletonList("B")));
    }

    @Test
    @DisplayName("7日按天补零")
    void buildDailyTrendSevenDays()
    {
        LocalDate end = LocalDate.now(ZoneId.systemDefault());
        LocalDate start = end.minusDays(6);
        AppSupplierAmountBucketDto row = new AppSupplierAmountBucketDto();
        row.setBucketKey(end.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        row.setAmount(new BigDecimal("1000"));

        assertEquals(7, AppSupplierHomeSupport.buildDailyTrend(
                Collections.singletonList(row), start, end).size());
    }

    @Test
    @DisplayName("今日24小时桶")
    void buildHourlyTrend24()
    {
        AppSupplierAmountBucketDto row = new AppSupplierAmountBucketDto();
        row.setBucketKey("16");
        row.setAmount(new BigDecimal("74300"));
        assertEquals(24, AppSupplierHomeSupport.buildHourlyTrend(Arrays.asList(row)).size());
        assertEquals(0, new BigDecimal("74300").compareTo(
                AppSupplierHomeSupport.buildHourlyTrend(Arrays.asList(row)).get(16).getAmount()));
    }

    @Test
    @DisplayName("结算汇总默认值")
    void buildSettlementSummaryEmpty()
    {
        assertEquals(0, BigDecimal.ZERO.compareTo(
                AppSupplierHomeSupport.buildSettlementSummary(null).getPayableAmount()));
    }

    @Test
    @DisplayName("非法时段")
    void invalidRange()
    {
        assertThrows(ServiceException.class, () -> AppSupplierHomeSupport.normalizeRange("ALL"));
    }

    @Test
    @DisplayName("7日粒度为按天")
    void days7Granularity()
    {
        AppSupplierHomeSupport.DateRange r = AppSupplierHomeSupport.resolveRange(
                AppSupplierHomeRules.RANGE_DAYS_7);
        assertEquals(AppSupplierHomeRules.TREND_GRANULARITY_DAY, r.trendGranularity);
    }
}
