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

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

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.AppDistributorAmountBucketDto;
import com.ruoyi.web.modules.trading.domain.dto.AppDistributorTradeSummaryDto;
import com.ruoyi.web.modules.trading.domain.vo.AppDistributorTradeSummaryVo;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("AppDistributorHomeSupport")
class AppDistributorHomeSupportTest
{
    @Test
    @DisplayName("汇总指标与完成率、均价")
    void buildSummary()
    {
        AppDistributorTradeSummaryDto raw = new AppDistributorTradeSummaryDto();
        raw.setOrderCount(4L);
        raw.setCompletedCount(2L);
        raw.setTotalAmount(new BigDecimal("36800"));
        raw.setTotalHeads(3L);
        raw.setSupplierCount(2L);

        AppDistributorTradeSummaryVo vo = AppDistributorHomeSupport.buildSummary(raw);
        assertEquals(4L, vo.getOrderCount());
        assertEquals(2L, vo.getSupplierCount());
        assertEquals(0, new BigDecimal("36800").compareTo(vo.getTotalAmount()));
        assertEquals(3L, vo.getTotalHeads());
        assertEquals(0, new BigDecimal("50.0").compareTo(vo.getCompletionRate()));
        assertEquals(0, new BigDecimal("12266.67").compareTo(vo.getAvgUnitPrice()));
    }

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

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

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

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

    @Test
    @DisplayName("7日粒度为按天")
    void days7Granularity()
    {
        AppDistributorHomeSupport.DateRange r = AppDistributorHomeSupport.resolveRange(
                AppDistributorHomeRules.RANGE_DAYS_7);
        assertEquals(AppDistributorHomeRules.TREND_GRANULARITY_DAY, r.trendGranularity);
        assertTrue(r.endDay.minusDays(6).equals(r.startDay));
    }
}
