package com.example.demo.controller;

import com.example.demo.service.DashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/dashboard")
@CrossOrigin(origins = "*")
public class DashboardApiController {

    @Autowired
    private DashboardService dashboardService;

    /**
     * 获取仪表盘概览数据
     */
    @GetMapping("/overview")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Map<String, Object>> getOverview() {
        try {
            Map<String, Object> overview = dashboardService.getDashboardOverview();
            return ResponseEntity.ok(overview);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 获取日活数据
     */
    @GetMapping("/daily-active-users")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Map<String, Object>> getDailyActiveUsers() {
        try {
            Map<String, Object> data = dashboardService.getDailyActiveUsers();
            return ResponseEntity.ok(data);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 获取收入趋势数据
     */
    @GetMapping("/revenue-trend")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Map<String, Object>> getRevenueTrend() {
        try {
            Map<String, Object> data = dashboardService.getRevenueTrend();
            return ResponseEntity.ok(data);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 获取订单状态分布
     */
    @GetMapping("/order-status-distribution")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Map<String, Object>> getOrderStatusDistribution() {
        try {
            Map<String, Object> data = dashboardService.getOrderStatusDistribution();
            return ResponseEntity.ok(data);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 获取支付方式分布
     */
    @GetMapping("/payment-method-distribution")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Map<String, Object>> getPaymentMethodDistribution() {
        try {
            Map<String, Object> data = dashboardService.getPaymentMethodDistribution();
            return ResponseEntity.ok(data);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 获取最近订单列表
     */
    @GetMapping("/recent-orders")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Map<String, Object>> getRecentOrders() {
        try {
            Map<String, Object> data = dashboardService.getRecentOrders();
            return ResponseEntity.ok(data);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 获取所有仪表盘数据
     */
    @GetMapping("/all")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Map<String, Object>> getAllDashboardData() {
        try {
            Map<String, Object> allData = Map.of(
                "overview", dashboardService.getDashboardOverview(),
                "dailyActiveUsers", dashboardService.getDailyActiveUsers(),
                "revenueTrend", dashboardService.getRevenueTrend(),
                "orderStatusDistribution", dashboardService.getOrderStatusDistribution(),
                "paymentMethodDistribution", dashboardService.getPaymentMethodDistribution(),
                "recentOrders", dashboardService.getRecentOrders()
            );
            return ResponseEntity.ok(allData);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
}
