package com.haha.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.haha.admin.annotation.RequirePermission;
import com.haha.entity.dto.*;
import com.haha.common.vo.PageResult;
import com.haha.common.vo.Result;
import com.haha.service.StatisticsService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/statistics")
@RequiredArgsConstructor
public class StatisticsController {

    private final StatisticsService statisticsService;

    @RequirePermission("statistics:read")
    @GetMapping("/overview")
    public Result<StatisticsOverviewVO> getOverview(StatisticsQueryDTO queryDTO) {
        StatisticsOverviewVO overview = statisticsService.getOverview(queryDTO);
        return Result.success("查询成功", overview);
    }

    @RequirePermission("statistics:category")
    @GetMapping("/category/overview")
    public Result<List<CategoryStatVO>> getCategoryOverview(StatisticsQueryDTO queryDTO) {
        List<CategoryStatVO> list = statisticsService.getCategoryOverview(queryDTO);
        return Result.success("查询成功", list);
    }

    @RequirePermission("statistics:category")
    @GetMapping("/category/trend")
    public Result<TrendDataVO> getCategoryTrend(StatisticsQueryDTO queryDTO) {
        TrendDataVO trend = statisticsService.getCategoryTrend(queryDTO);
        return Result.success("查询成功", trend);
    }

    @RequirePermission("statistics:product")
    @GetMapping("/product/list")
    public Result<PageResult<ProductStatVO>> getProductList(StatisticsQueryDTO queryDTO) {
        queryDTO.validate();
        IPage<ProductStatVO> page = statisticsService.getProductList(queryDTO);
        return Result.success("查询成功", PageResult.of(page));
    }

    @RequirePermission("statistics:product")
    @GetMapping("/product/top")
    public Result<List<ProductStatVO>> getProductTop(StatisticsQueryDTO queryDTO) {
        List<ProductStatVO> list = statisticsService.getProductTop(queryDTO);
        return Result.success("查询成功", list);
    }

    @RequirePermission("statistics:product")
    @GetMapping("/product/{productId}/trend")
    public Result<TrendDataVO> getProductTrend(
            @PathVariable Long productId,
            StatisticsQueryDTO queryDTO) {
        TrendDataVO trend = statisticsService.getProductTrend(productId, queryDTO);
        return Result.success("查询成功", trend);
    }

    @RequirePermission("statistics:device")
    @GetMapping("/device/overview")
    public Result<Map<String, Object>> getDeviceOverview(StatisticsQueryDTO queryDTO) {
        Map<String, Object> overview = statisticsService.getDeviceOverview(queryDTO);
        return Result.success("查询成功", overview);
    }

    @RequirePermission("statistics:device")
    @GetMapping("/device/list")
    public Result<PageResult<DeviceStatVO>> getDeviceList(StatisticsQueryDTO queryDTO) {
        queryDTO.validate();
        IPage<DeviceStatVO> page = statisticsService.getDeviceList(queryDTO);
        return Result.success("查询成功", PageResult.of(page));
    }

    @RequirePermission("statistics:device")
    @GetMapping("/device/{deviceId}/trend")
    public Result<TrendDataVO> getDeviceTrend(
            @PathVariable String deviceId,
            StatisticsQueryDTO queryDTO) {
        TrendDataVO trend = statisticsService.getDeviceTrend(deviceId, queryDTO);
        return Result.success("查询成功", trend);
    }

    @RequirePermission("statistics:shop")
    @GetMapping("/shop/overview")
    public Result<Map<String, Object>> getShopOverview(StatisticsQueryDTO queryDTO) {
        Map<String, Object> overview = statisticsService.getShopOverview(queryDTO);
        return Result.success("查询成功", overview);
    }

    @RequirePermission("statistics:shop")
    @GetMapping("/shop/list")
    public Result<PageResult<ShopStatVO>> getShopList(StatisticsQueryDTO queryDTO) {
        queryDTO.validate();
        IPage<ShopStatVO> page = statisticsService.getShopList(queryDTO);
        return Result.success("查询成功", PageResult.of(page));
    }

    @RequirePermission("statistics:shop")
    @GetMapping("/shop/ranking")
    public Result<List<ShopStatVO>> getShopRanking(StatisticsQueryDTO queryDTO) {
        List<ShopStatVO> list = statisticsService.getShopRanking(queryDTO);
        return Result.success("查询成功", list);
    }

    @RequirePermission("statistics:shop")
    @GetMapping("/shop/{shopId}/trend")
    public Result<TrendDataVO> getShopTrend(
            @PathVariable Long shopId,
            StatisticsQueryDTO queryDTO) {
        TrendDataVO trend = statisticsService.getShopTrend(shopId, queryDTO);
        return Result.success("查询成功", trend);
    }

    @RequirePermission("statistics:profit")
    @GetMapping("/profit/overview")
    public Result<Map<String, Object>> getProfitOverview(StatisticsQueryDTO queryDTO) {
        Map<String, Object> overview = statisticsService.getProfitOverview(queryDTO);
        return Result.success("查询成功", overview);
    }

    @RequirePermission("statistics:profit")
    @GetMapping("/profit/list")
    public Result<PageResult<ProfitStatVO>> getProfitList(StatisticsQueryDTO queryDTO) {
        queryDTO.validate();
        IPage<ProfitStatVO> page = statisticsService.getProfitList(queryDTO);
        return Result.success("查询成功", PageResult.of(page));
    }

    @RequirePermission("statistics:profit")
    @GetMapping("/profit/trend")
    public Result<TrendDataVO> getProfitTrend(StatisticsQueryDTO queryDTO) {
        TrendDataVO trend = statisticsService.getProfitTrend(queryDTO);
        return Result.success("查询成功", trend);
    }

    @RequirePermission("statistics:profit")
    @GetMapping("/profit/warning")
    public Result<List<ProfitStatVO>> getProfitWarning(StatisticsQueryDTO queryDTO) {
        List<ProfitStatVO> list = statisticsService.getProfitWarning(queryDTO);
        return Result.success("查询成功", list);
    }

    @RequirePermission("statistics:repurchase")
    @GetMapping("/repurchase/overview")
    public Result<Map<String, Object>> getRepurchaseOverview(StatisticsQueryDTO queryDTO) {
        Map<String, Object> overview = statisticsService.getRepurchaseOverview(queryDTO);
        return Result.success("查询成功", overview);
    }

    @RequirePermission("statistics:repurchase")
    @GetMapping("/repurchase/distribution")
    public Result<Map<String, Object>> getRepurchaseDistribution(StatisticsQueryDTO queryDTO) {
        Map<String, Object> distribution = statisticsService.getRepurchaseDistribution(queryDTO);
        return Result.success("查询成功", distribution);
    }

    @RequirePermission("statistics:repurchase")
    @GetMapping("/repurchase/trend")
    public Result<TrendDataVO> getRepurchaseTrend(StatisticsQueryDTO queryDTO) {
        TrendDataVO trend = statisticsService.getRepurchaseTrend(queryDTO);
        return Result.success("查询成功", trend);
    }

    @RequirePermission("statistics:repurchase")
    @GetMapping("/repurchase/users")
    public Result<PageResult<RepurchaseStatVO>> getRepurchaseUsers(StatisticsQueryDTO queryDTO) {
        queryDTO.validate();
        IPage<RepurchaseStatVO> page = statisticsService.getRepurchaseUsers(queryDTO);
        return Result.success("查询成功", PageResult.of(page));
    }

    @RequirePermission("statistics:export")
    @GetMapping("/export")
    public void exportStatistics(StatisticsQueryDTO queryDTO, HttpServletResponse response) {
        try {
            byte[] data = statisticsService.exportStatistics(queryDTO);
            
            String fileName = "statistics_" + queryDTO.getType() + ".xlsx";
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.getOutputStream().write(data);
            response.getOutputStream().flush();
        } catch (Exception e) {
            log.error("导出统计数据失败", e);
        }
    }
}
