package com.dora.controller;

import com.dora.dto.AdminPaymentUserDto;
import com.dora.dto.ApiResponse;
import com.dora.service.AdminPaymentUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 管理端支付用户统计控制器
 */
@Slf4j
@RestController
@RequestMapping("/admin/payment-users")
@RequiredArgsConstructor
@Tag(name = "管理端支付用户统计", description = "管理端支付用户统计相关接口")
public class AdminPaymentUserController {

    private static final String DATE_FORMAT = "yyyy-MM-dd";
    private final AdminPaymentUserService adminPaymentUserService;

    @GetMapping("/statistics")
    @Operation(summary = "获取支付用户统计数据", description = "获取支付用户相关的统计数据，包括概览、用户列表、金额分布和每日统计")
    @ApiResponses(value = {
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "获取成功"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "请求参数错误"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "权限不足"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public ApiResponse<AdminPaymentUserDto.PaymentUserStatisticsResponse> getPaymentUserStatistics(
            @Parameter(description = "开始日期（格式：yyyy-MM-dd）", example = "2024-01-01") 
            @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期（格式：yyyy-MM-dd）", example = "2024-01-31") 
            @RequestParam(required = false) String endDate) {

        log.info("收到支付用户统计数据查询请求，开始日期: {}, 结束日期: {}", startDate, endDate);

        AdminPaymentUserDto.PaymentUserStatisticsRequest request = new AdminPaymentUserDto.PaymentUserStatisticsRequest();
        request.setStartDate(startDate);
        request.setEndDate(endDate);

        return adminPaymentUserService.getPaymentUserStatistics(request);
    }

    @GetMapping("/list")
    @Operation(summary = "获取支付用户详情列表", description = "分页获取支付用户详情列表，支持多种筛选和排序条件")
    @ApiResponses(value = {
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "获取成功"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "请求参数错误"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "权限不足"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public ApiResponse<AdminPaymentUserDto.PaymentUserListResponse> getPaymentUserList(
            @Valid AdminPaymentUserDto.PaymentUserListRequest request) {

        log.info("收到支付用户列表查询请求: {}", request);
        return adminPaymentUserService.getPaymentUserList(request);
    }

    // ==================== 便捷接口 ====================

    @GetMapping("/statistics/today")
    @Operation(summary = "获取今日支付用户统计", description = "获取今日支付用户统计数据")
    public ApiResponse<AdminPaymentUserDto.PaymentUserStatisticsResponse> getTodayPaymentUserStatistics() {
        
        log.info("收到今日支付用户统计查询请求");
        
        java.time.LocalDate today = java.time.LocalDate.now();
        String todayStr = today.format(java.time.format.DateTimeFormatter.ofPattern(DATE_FORMAT));
        
        AdminPaymentUserDto.PaymentUserStatisticsRequest request = new AdminPaymentUserDto.PaymentUserStatisticsRequest();
        request.setStartDate(todayStr);
        request.setEndDate(todayStr);
        
        return adminPaymentUserService.getPaymentUserStatistics(request);
    }

    @GetMapping("/statistics/week")
    @Operation(summary = "获取本周支付用户统计", description = "获取本周支付用户统计数据")
    public ApiResponse<AdminPaymentUserDto.PaymentUserStatisticsResponse> getWeekPaymentUserStatistics() {
        
        log.info("收到本周支付用户统计查询请求");
        
        java.time.LocalDate today = java.time.LocalDate.now();
        java.time.LocalDate weekStart = today.with(java.time.DayOfWeek.MONDAY);
        
        String startDateStr = weekStart.format(java.time.format.DateTimeFormatter.ofPattern(DATE_FORMAT));
        String endDateStr = today.format(java.time.format.DateTimeFormatter.ofPattern(DATE_FORMAT));
        
        AdminPaymentUserDto.PaymentUserStatisticsRequest request = new AdminPaymentUserDto.PaymentUserStatisticsRequest();
        request.setStartDate(startDateStr);
        request.setEndDate(endDateStr);
        
        return adminPaymentUserService.getPaymentUserStatistics(request);
    }

    @GetMapping("/statistics/month")
    @Operation(summary = "获取本月支付用户统计", description = "获取本月支付用户统计数据")
    public ApiResponse<AdminPaymentUserDto.PaymentUserStatisticsResponse> getMonthPaymentUserStatistics() {
        
        log.info("收到本月支付用户统计查询请求");
        
        java.time.LocalDate today = java.time.LocalDate.now();
        java.time.LocalDate monthStart = today.withDayOfMonth(1);
        
        String startDateStr = monthStart.format(java.time.format.DateTimeFormatter.ofPattern(DATE_FORMAT));
        String endDateStr = today.format(java.time.format.DateTimeFormatter.ofPattern(DATE_FORMAT));
        
        AdminPaymentUserDto.PaymentUserStatisticsRequest request = new AdminPaymentUserDto.PaymentUserStatisticsRequest();
        request.setStartDate(startDateStr);
        request.setEndDate(endDateStr);
        
        return adminPaymentUserService.getPaymentUserStatistics(request);
    }

    @GetMapping("/list/repeat-users")
    @Operation(summary = "获取复购用户列表", description = "获取有多次支付记录的复购用户列表")
    public ApiResponse<AdminPaymentUserDto.PaymentUserListResponse> getRepeatPaymentUsers(
            @Parameter(description = "开始日期（格式：yyyy-MM-dd）", example = "2024-01-01") 
            @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期（格式：yyyy-MM-dd）", example = "2024-01-31") 
            @RequestParam(required = false) String endDate,
            @Parameter(description = "页码", example = "1") 
            @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") 
            @RequestParam(defaultValue = "10") Integer size) {
        
        log.info("收到复购用户列表查询请求，开始日期: {}, 结束日期: {}", startDate, endDate);
        
        AdminPaymentUserDto.PaymentUserListRequest request = new AdminPaymentUserDto.PaymentUserListRequest();
        request.setStartDate(startDate);
        request.setEndDate(endDate);
        request.setOnlyRepeatUsers(true);
        request.setSortField("orderCount");
        request.setSortOrder("DESC");
        request.setPage(page);
        request.setSize(size);
        
        return adminPaymentUserService.getPaymentUserList(request);
    }

    @GetMapping("/list/top-spenders")
    @Operation(summary = "获取高消费用户列表", description = "获取按支付金额排序的高消费用户列表")
    public ApiResponse<AdminPaymentUserDto.PaymentUserListResponse> getTopSpendingUsers(
            @Parameter(description = "开始日期（格式：yyyy-MM-dd）", example = "2024-01-01") 
            @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期（格式：yyyy-MM-dd）", example = "2024-01-31") 
            @RequestParam(required = false) String endDate,
            @Parameter(description = "页码", example = "1") 
            @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") 
            @RequestParam(defaultValue = "10") Integer size) {
        
        log.info("收到高消费用户列表查询请求，开始日期: {}, 结束日期: {}", startDate, endDate);
        
        AdminPaymentUserDto.PaymentUserListRequest request = new AdminPaymentUserDto.PaymentUserListRequest();
        request.setStartDate(startDate);
        request.setEndDate(endDate);
        request.setSortField("totalAmount");
        request.setSortOrder("DESC");
        request.setPage(page);
        request.setSize(size);
        
        return adminPaymentUserService.getPaymentUserList(request);
    }
}
