import { analyticsClient } from './analyticsClient';
import type {
  ApiResponse,
  UserAnalyticsDashboardResponse,
  AnalyticsDashboardResponse,
  UserTimelineAnalyticsResponse,
  TimelineAnalyticsResponse,
  UserRoiAnalyticsResponse,
  RoiAnalyticsResponse,
  UserChannelAnalyticsResponse,
  ChannelAnalyticsResponse,
  AnalyticsQueryParams,
  TimelineQueryParams,
  ChannelQueryParams,
  RoiQueryParams,
} from '../model/types';

/**
 * Analytics API Service
 * 실시간 효과 측정 및 통합 대시보드 API
 *
 * Note: Proxy routes handle /api/v1 prefix, so paths start with /users or /events
 */
export const analyticsApi = {
  // ============= User Analytics (사용자 전체 이벤트 통합) =============

  /**
   * 사용자 전체 성과 대시보드 조회
   */
  getUserAnalytics: async (
    userId: string,
    params?: AnalyticsQueryParams
  ): Promise<UserAnalyticsDashboardResponse> => {
    const response = await analyticsClient.get<ApiResponse<UserAnalyticsDashboardResponse>>(
      `/users/${userId}`,
      { params }
    );
    return response.data.data;
  },

  /**
   * 사용자 전체 시간대별 참여 추이
   */
  getUserTimelineAnalytics: async (
    userId: string,
    params?: TimelineQueryParams
  ): Promise<UserTimelineAnalyticsResponse> => {
    const response = await analyticsClient.get<ApiResponse<UserTimelineAnalyticsResponse>>(
      `/users/${userId}/timeline`,
      { params }
    );
    return response.data.data;
  },

  /**
   * 사용자 전체 ROI 상세 분석
   */
  getUserRoiAnalytics: async (
    userId: string,
    params?: AnalyticsQueryParams & RoiQueryParams
  ): Promise<UserRoiAnalyticsResponse> => {
    const response = await analyticsClient.get<ApiResponse<UserRoiAnalyticsResponse>>(
      `/users/${userId}/roi`,
      { params }
    );
    return response.data.data;
  },

  /**
   * 사용자 전체 채널별 성과 분석
   */
  getUserChannelAnalytics: async (
    userId: string,
    params?: ChannelQueryParams
  ): Promise<UserChannelAnalyticsResponse> => {
    const response = await analyticsClient.get<ApiResponse<UserChannelAnalyticsResponse>>(
      `/users/${userId}/channels`,
      { params }
    );
    return response.data.data;
  },

  // ============= Event Analytics (특정 이벤트 분석) =============

  /**
   * 이벤트 성과 대시보드 조회
   */
  getEventAnalytics: async (
    eventId: string,
    params?: AnalyticsQueryParams
  ): Promise<AnalyticsDashboardResponse> => {
    const response = await analyticsClient.get<ApiResponse<AnalyticsDashboardResponse>>(
      `/events/${eventId}`,
      { params }
    );
    return response.data.data;
  },

  /**
   * 이벤트 시간대별 참여 추이
   */
  getEventTimelineAnalytics: async (
    eventId: string,
    params?: TimelineQueryParams
  ): Promise<TimelineAnalyticsResponse> => {
    const response = await analyticsClient.get<ApiResponse<TimelineAnalyticsResponse>>(
      `/events/${eventId}/timeline`,
      { params }
    );
    return response.data.data;
  },

  /**
   * 이벤트 ROI 상세 분석
   */
  getEventRoiAnalytics: async (
    eventId: string,
    params?: RoiQueryParams
  ): Promise<RoiAnalyticsResponse> => {
    const response = await analyticsClient.get<ApiResponse<RoiAnalyticsResponse>>(
      `/events/${eventId}/roi`,
      { params }
    );
    return response.data.data;
  },

  /**
   * 이벤트 채널별 성과 분석
   */
  getEventChannelAnalytics: async (
    eventId: string,
    params?: ChannelQueryParams
  ): Promise<ChannelAnalyticsResponse> => {
    const response = await analyticsClient.get<ApiResponse<ChannelAnalyticsResponse>>(
      `/events/${eventId}/channels`,
      { params }
    );
    return response.data.data;
  },
};

export default analyticsApi;
