package com.dora.controller;

import com.dora.annotation.RequireAdminOrStaff;
import com.dora.dto.ApiResponse;
import com.dora.dto.AdminRevenueDto;
import com.dora.service.AdminRevenueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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;

/**
 * 管理员收益设置控制器
 */
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "管理员收益设置", description = "管理员收益设置和统计相关接口")
@RequireAdminOrStaff
public class AdminRevenueController {

    private final AdminRevenueService adminRevenueService;

    @GetMapping("/settings/revenue")
    @Operation(summary = "获取收益设置", description = "获取系统收益相关配置")
    public ApiResponse<AdminRevenueDto.RevenueSettingsResponse> getRevenueSettings() {
        
        log.info("收到获取收益设置请求");
        return adminRevenueService.getRevenueSettings();
    }

    @PutMapping("/settings/revenue")
    @Operation(summary = "更新收益设置", description = "更新收益配置参数")
    public ApiResponse<String> updateRevenueSettings(
            @Valid @RequestBody AdminRevenueDto.RevenueSettingsUpdateRequest request) {
        
        log.info("收到更新收益设置请求");
        return adminRevenueService.updateRevenueSettings(request);
    }

    @GetMapping("/revenue/statistics")
    @Operation(summary = "获取收益统计", description = "获取收益统计数据")
    public ApiResponse<AdminRevenueDto.RevenueStatisticsResponse> getRevenueStatistics(
            @Parameter(description = "统计周期（daily/weekly/monthly）") @RequestParam(defaultValue = "daily") String period,
            @Parameter(description = "开始日期", example = "2024-01-01") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期", example = "2024-01-31") @RequestParam(required = false) String endDate,
            @Parameter(description = "内容类型筛选（course/workflow/all）") @RequestParam(defaultValue = "all") String contentType) {
        
        log.info("收到获取收益统计请求，周期：{}，内容类型：{}", period, contentType);
        
        AdminRevenueDto.RevenueStatisticsRequest request = new AdminRevenueDto.RevenueStatisticsRequest();
        request.setPeriod(period);
        request.setStartDate(startDate);
        request.setEndDate(endDate);
        request.setContentType(contentType);
        
        return adminRevenueService.getRevenueStatistics(request);
    }

    @GetMapping("/settings/commission")
    @Operation(summary = "获取分成设置", description = "获取分成比例配置")
    public ApiResponse<AdminRevenueDto.CommissionSettingsResponse> getCommissionSettings() {
        
        log.info("收到获取分成设置请求");
        return adminRevenueService.getCommissionSettings();
    }

    @PutMapping("/settings/commission")
    @Operation(summary = "更新分成设置", description = "更新分成比例配置")
    public ApiResponse<String> updateCommissionSettings(
            @Valid @RequestBody AdminRevenueDto.CommissionSettingsUpdateRequest request) {
        
        log.info("收到更新分成设置请求");
        return adminRevenueService.updateCommissionSettings(request);
    }

    @GetMapping("/settings/withdraw")
    @Operation(summary = "获取提现设置", description = "获取提现相关配置")
    public ApiResponse<AdminRevenueDto.WithdrawSettingsResponse> getWithdrawSettings() {
        
        log.info("收到获取提现设置请求");
        return adminRevenueService.getWithdrawSettings();
    }

    @PutMapping("/settings/withdraw")
    @Operation(summary = "更新提现设置", description = "更新提现相关配置")
    public ApiResponse<String> updateWithdrawSettings(
            @Valid @RequestBody AdminRevenueDto.WithdrawSettingsUpdateRequest request) {
        
        log.info("收到更新提现设置请求");
        return adminRevenueService.updateWithdrawSettings(request);
    }

    @DeleteMapping("/config/{configKey}")
    @Operation(summary = "删除收益配置", description = "删除指定的收益配置项")
    public ApiResponse<String> deleteRevenueConfig(
            @Parameter(description = "配置键") @PathVariable String configKey) {
        
        log.info("收到删除收益配置请求，配置键: {}", configKey);
        return adminRevenueService.deleteRevenueConfig(configKey);
    }

    @DeleteMapping("/promotion-level/{levelId}")
    @Operation(summary = "删除推广等级", description = "删除指定的推广等级配置")
    public ApiResponse<String> deletePromotionLevel(
            @Parameter(description = "等级ID") @PathVariable Long levelId) {
        
        log.info("收到删除推广等级请求，等级ID: {}", levelId);
        return adminRevenueService.deletePromotionLevel(levelId);
    }

    @GetMapping("/statistics/most-used-workflows")
    @Operation(summary = "获取被使用次数最多的工作流", description = "统计指定时间段内被使用次数最多的工作流")
    public ApiResponse<AdminRevenueDto.MostUsedWorkflowResponse> getMostUsedWorkflows(
            @Parameter(description = "开始日期", example = "2024-01-01") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期", example = "2024-01-31") @RequestParam(required = false) String endDate,
            @Parameter(description = "返回条数限制", example = "10") @RequestParam(defaultValue = "10") Integer limit) {
        
        log.info("收到获取最多使用工作流统计请求，时间范围: {} 到 {}, 限制条数: {}", startDate, endDate, limit);
        
        AdminRevenueDto.PopularContentRequest request = new AdminRevenueDto.PopularContentRequest();
        request.setStartDate(startDate);
        request.setEndDate(endDate);
        request.setLimit(limit);
        
        return adminRevenueService.getMostUsedWorkflows(request);
    }

    @GetMapping("/statistics/most-viewed-videos")
    @Operation(summary = "获取观看次数最高的视频", description = "统计指定时间段内观看次数最高的视频")
    public ApiResponse<AdminRevenueDto.MostViewedVideoResponse> getMostViewedVideos(
            @Parameter(description = "开始日期", example = "2024-01-01") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期", example = "2024-01-31") @RequestParam(required = false) String endDate,
            @Parameter(description = "返回条数限制", example = "10") @RequestParam(defaultValue = "10") Integer limit) {
        
        log.info("收到获取最多观看视频统计请求，时间范围: {} 到 {}, 限制条数: {}", startDate, endDate, limit);
        
        AdminRevenueDto.PopularContentRequest request = new AdminRevenueDto.PopularContentRequest();
        request.setStartDate(startDate);
        request.setEndDate(endDate);
        request.setLimit(limit);
        
        return adminRevenueService.getMostViewedVideos(request);
    }
}
