package com.haha.service.impl;

import com.haha.entity.MarketingStatistics;
import com.haha.mapper.MarketingStatisticsMapper;
import com.haha.service.MarketingStatisticsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class MarketingStatisticsServiceImpl implements MarketingStatisticsService {

    private final MarketingStatisticsMapper statisticsMapper;

    @Override
    public List<MarketingStatistics> getByActivityId(Long activityId) {
        return statisticsMapper.selectByActivityId(activityId);
    }

    @Override
    public List<MarketingStatistics> getByActivityIdAndDateRange(Long activityId, LocalDate startDate, LocalDate endDate) {
        return statisticsMapper.selectByActivityIdAndDateRange(activityId, startDate, endDate);
    }

    @Override
    public Map<String, Object> getActivityOverview(Long activityId) {
        Map<String, Object> overview = new HashMap<>();

        Object[] sums = statisticsMapper.sumByActivityId(activityId);
        if (sums != null && sums.length > 0) {
            overview.put("totalParticipants", sums[0] != null ? sums[0] : 0);
            overview.put("totalNewUsers", sums[1] != null ? sums[1] : 0);
            overview.put("totalOrders", sums[2] != null ? sums[2] : 0);
            overview.put("totalOrderAmount", sums[3] != null ? sums[3] : BigDecimal.ZERO);
            overview.put("totalDiscountAmount", sums[4] != null ? sums[4] : BigDecimal.ZERO);
            overview.put("totalActualAmount", sums[5] != null ? sums[5] : BigDecimal.ZERO);
            overview.put("totalCouponReceived", sums[6] != null ? sums[6] : 0);
            overview.put("totalCouponUsed", sums[7] != null ? sums[7] : 0);
        }

        overview.put("roi", calculateROI(activityId));

        return overview;
    }

    @Override
    public Map<String, Object> getMarketingOverview() {
        Map<String, Object> overview = new HashMap<>();
        overview.put("message", "营销数据概览");
        return overview;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void generateDailyStatistics(Long activityId, LocalDate date) {
        log.info("生成活动每日统计数据: activityId={}, date={}", activityId, date);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void generateAllDailyStatistics() {
        log.info("生成所有活动每日统计数据");
    }

    @Override
    public Map<String, Object> getTrendData(Long activityId, LocalDate startDate, LocalDate endDate) {
        Map<String, Object> trendData = new HashMap<>();
        List<MarketingStatistics> stats = getByActivityIdAndDateRange(activityId, startDate, endDate);
        trendData.put("data", stats);
        return trendData;
    }

    @Override
    public BigDecimal calculateROI(Long activityId) {
        Object[] sums = statisticsMapper.sumByActivityId(activityId);
        if (sums == null || sums.length < 6) {
            return BigDecimal.ZERO;
        }

        BigDecimal totalActualAmount = sums[5] != null ? new BigDecimal(sums[5].toString()) : BigDecimal.ZERO;
        BigDecimal totalDiscountAmount = sums[4] != null ? new BigDecimal(sums[4].toString()) : BigDecimal.ZERO;

        if (totalDiscountAmount.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        }

        return totalActualAmount.divide(totalDiscountAmount, 2, RoundingMode.HALF_UP);
    }
}
