package com.erp.customer.controller;

import com.erp.customer.dto.CustomerStatisticsDTO;
import com.erp.customer.service.CustomerStatisticsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 客户统计控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/customer-statistics")
@RequiredArgsConstructor
public class CustomerStatisticsController {
    
    private final CustomerStatisticsService statisticsService;
    
    /**
     * 获取客户统计数据
     */
    @GetMapping
    public ResponseEntity<CustomerStatisticsDTO> getCustomerStatistics(@RequestParam Long tenantId) {
        log.info("获取客户统计数据: tenantId={}", tenantId);
        CustomerStatisticsDTO stats = statisticsService.getCustomerStatistics(tenantId);
        return ResponseEntity.ok(stats);
    }
    
    /**
     * 获取客户等级分布
     */
    @GetMapping("/level-distribution")
    public ResponseEntity<CustomerStatisticsDTO> getLevelDistribution(@RequestParam Long tenantId) {
        log.info("获取客户等级分布: tenantId={}", tenantId);
        CustomerStatisticsDTO stats = statisticsService.getLevelDistribution(tenantId);
        return ResponseEntity.ok(stats);
    }
    
    /**
     * 获取客户状态分布
     */
    @GetMapping("/status-distribution")
    public ResponseEntity<CustomerStatisticsDTO> getStatusDistribution(@RequestParam Long tenantId) {
        log.info("获取客户状态分布: tenantId={}", tenantId);
        CustomerStatisticsDTO stats = statisticsService.getStatusDistribution(tenantId);
        return ResponseEntity.ok(stats);
    }
    
    /**
     * 获取销售负责客户分布
     */
    @GetMapping("/sales-distribution")
    public ResponseEntity<CustomerStatisticsDTO> getSalesDistribution(@RequestParam Long tenantId) {
        log.info("获取销售负责客户分布: tenantId={}", tenantId);
        CustomerStatisticsDTO stats = statisticsService.getSalesDistribution(tenantId);
        return ResponseEntity.ok(stats);
    }
    
    /**
     * 获取客户增长趋势
     */
    @GetMapping("/growth-trend")
    public ResponseEntity<CustomerStatisticsDTO> getGrowthTrend(@RequestParam Long tenantId) {
        log.info("获取客户增长趋势: tenantId={}", tenantId);
        CustomerStatisticsDTO stats = statisticsService.getGrowthTrend(tenantId);
        return ResponseEntity.ok(stats);
    }
    
    /**
     * 获取超时未联系客户统计
     */
    @GetMapping("/overdue-followup")
    public ResponseEntity<CustomerStatisticsDTO> getOverdueFollowUpStatistics(
            @RequestParam Long tenantId,
            @RequestParam(defaultValue = "7") Integer overdueDays) {
        log.info("获取超时未联系统计: tenantId={}, overdueDays={}", tenantId, overdueDays);
        CustomerStatisticsDTO stats = statisticsService.getOverdueFollowUpStatistics(tenantId, overdueDays);
        return ResponseEntity.ok(stats);
    }
    
    /**
     * 获取今日待跟进统计
     */
    @GetMapping("/today-followup")
    public ResponseEntity<CustomerStatisticsDTO> getTodayFollowUpStatistics(@RequestParam Long tenantId) {
        log.info("获取今日待跟进统计: tenantId={}", tenantId);
        CustomerStatisticsDTO stats = statisticsService.getTodayFollowUpStatistics(tenantId);
        return ResponseEntity.ok(stats);
    }
}
