package com.gcsc.connection.statistics.controller;

import com.gcsc.connection.common.dto.ApiResponse;
import com.gcsc.connection.statistics.dto.ApiStatsResponse;
import com.gcsc.connection.statistics.dto.ServiceStatsResponse;
import com.gcsc.connection.statistics.dto.TenantStatsResponse;
import com.gcsc.connection.statistics.dto.UsageTrendResponse;
import com.gcsc.connection.statistics.dto.UserStatsResponse;
import com.gcsc.connection.statistics.service.StatisticsService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 통계 API - 서비스/사용자/API/테넌트별 통계 데이터 제공
 */
@RestController
@RequestMapping("/api/statistics")
@RequiredArgsConstructor
public class StatisticsController {

    private final StatisticsService statisticsService;

    /**
     * 서비스별 통계 조회
     */
    @GetMapping("/services")
    public ResponseEntity<ApiResponse<ServiceStatsResponse>> getServiceStats(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        LocalDateTime start = startDate.atStartOfDay();
        LocalDateTime end = endDate.plusDays(1).atStartOfDay();
        ServiceStatsResponse response = statisticsService.getServiceStats(start, end);
        return ResponseEntity.ok(ApiResponse.ok(response));
    }

    /**
     * 사용자별 통계 조회
     */
    @GetMapping("/users")
    public ResponseEntity<ApiResponse<UserStatsResponse>> getUserStats(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        LocalDateTime start = startDate.atStartOfDay();
        LocalDateTime end = endDate.plusDays(1).atStartOfDay();
        UserStatsResponse response = statisticsService.getUserStats(start, end);
        return ResponseEntity.ok(ApiResponse.ok(response));
    }

    /**
     * API 엔드포인트별 통계 조회
     */
    @GetMapping("/apis")
    public ResponseEntity<ApiResponse<ApiStatsResponse>> getApiStats(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        LocalDateTime start = startDate.atStartOfDay();
        LocalDateTime end = endDate.plusDays(1).atStartOfDay();
        ApiStatsResponse response = statisticsService.getApiStats(start, end);
        return ResponseEntity.ok(ApiResponse.ok(response));
    }

    /**
     * 테넌트별 통계 조회
     */
    @GetMapping("/tenants")
    public ResponseEntity<ApiResponse<TenantStatsResponse>> getTenantStats(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        LocalDateTime start = startDate.atStartOfDay();
        LocalDateTime end = endDate.plusDays(1).atStartOfDay();
        TenantStatsResponse response = statisticsService.getTenantStats(start, end);
        return ResponseEntity.ok(ApiResponse.ok(response));
    }

    /**
     * 사용량 추이 조회 (일별/주별/월별)
     */
    @GetMapping("/usage-trend")
    public ResponseEntity<ApiResponse<UsageTrendResponse>> getUsageTrend(
            @RequestParam(defaultValue = "daily") String period) {
        return ResponseEntity.ok(ApiResponse.ok(statisticsService.getUsageTrend(period)));
    }
}
