/**
 * @description 系统总览相关API
 */

import { api } from '@/apis/index';
import type { ResultDomain } from '@/types';

export interface SystemStatisticsDTO {
  totalUsers: number;
  totalUsersChange: string;
  totalResources: number;
  totalResourcesChange: string;
  totalPv: number;
  totalPvChange: string;
  totalUv: number;
  totalUvChange: string;
}

export interface ActiveUsersChartDTO {
  labels: string[];
  pvValues: number[];
  uvValues: number[];
}

export interface ResourceCategoryItemDTO {
  name: string;
  value: number;
}

export interface ResourceCategoryStatsDTO {
  items: ResourceCategoryItemDTO[];
}

export interface TodayVisitsDTO {
  uv: number;
  pv: number;
  avgVisitDuration: string;
  bounceRate: string;
}

export const systemOverviewApi = {
  async getStatistics(): Promise<ResultDomain<SystemStatisticsDTO>> {
    const response = await api.get<SystemStatisticsDTO>('/system/overview/statistics');
    return response.data;
  },

  async getActiveUsersChart(start: string, end: string): Promise<ResultDomain<ActiveUsersChartDTO>> {
    const response = await api.get<ActiveUsersChartDTO>('/system/overview/active-users', { start, end });
    return response.data;
  },

  async getResourceCategoryStats(): Promise<ResultDomain<ResourceCategoryStatsDTO>> {
    const response = await api.get<ResourceCategoryStatsDTO>('/system/overview/resource-category-stats');
    return response.data;
  },

  async getTodayVisits(): Promise<ResultDomain<TodayVisitsDTO>> {
    const response = await api.get<TodayVisitsDTO>('/system/overview/today-visits');
    return response.data;
  },

  /**
   * 记录一次页面访问（PV），前端在路由切换时调用
   */
  async trackVisit(): Promise<ResultDomain<string>> {
    const response = await api.get<string>('/system/overview/track-visit', undefined, {
      showLoading: false,
      showError: false
    });
    return response.data;
  }
};
