import { api, requestClient } from '#/api/request';

export type DashboardRange = '7d' | '30d' | 'custom' | 'today';

export interface DashboardOverviewQuery {
  endDate?: string;
  range?: DashboardRange;
  startDate?: string;
}

export interface DashboardUserRankQuery extends DashboardOverviewQuery {
  assistantId?: string;
}

export interface DashboardSummary {
  activeUserTotal: number;
  activeAssistantTotal: number;
  botTotal: number;
  chatActiveUserTotal: number;
  chatMessageTotal: number;
  chatSessionTotal: number;
  knowledgeBaseTotal: number;
  userTotal: number;
  workflowTotal: number;
}

export interface DashboardChatStatus {
  available: boolean;
  message: string;
}

export interface DashboardTrendItem {
  activeUserTotal: number;
  chatMessageTotal: number;
  chatSessionTotal: number;
  key: string;
  label: string;
}

export interface DashboardAssistantTrendPoint {
  key: string;
  label: string;
  sessionTotal: number;
}

export interface DashboardAssistantTrendSeries {
  assistantId?: number | string;
  label: string;
  points: DashboardAssistantTrendPoint[];
  totalSessionCount: number;
}

export interface DashboardDistributionItem {
  activeUserTotal: number;
  assistantId?: number | string;
  avgMessagePerSession?: number;
  avgSessionPerUser?: number;
  botTotal: number;
  key: string;
  knowledgeBaseTotal: number;
  label: string;
  messageTotal?: number;
  sessionTotal?: number;
  userTotal: number;
  value: number;
  workflowTotal: number;
}

export interface DashboardUserRankItem {
  label: string;
  loginName?: string;
  messageTotal: number;
  nickname?: string;
  sessionTotal: number;
  userId?: number | string;
}

export interface DashboardOverviewResponse {
  assistantTrends: DashboardAssistantTrendSeries[];
  chatStatus: DashboardChatStatus;
  distribution: DashboardDistributionItem[];
  query: DashboardOverviewQuery;
  summary: DashboardSummary;
  trends: DashboardTrendItem[];
  updatedAt: string;
}

export async function getDashboardOverview(params: DashboardOverviewQuery) {
  return requestClient.get<DashboardOverviewResponse>(
    '/api/v1/dashboard/overview',
    {
      params,
    },
  );
}

export async function getDashboardUserRanks(params: DashboardUserRankQuery) {
  return requestClient.get<DashboardUserRankItem[]>(
    '/api/v1/dashboard/user-ranks',
    {
      params,
    },
  );
}

export async function exportDashboardUserRanks(params: DashboardUserRankQuery) {
  return api.download(buildDashboardUrl('/api/v1/dashboard/user-ranks/export', params));
}

function buildDashboardUrl(url: string, params?: Record<string, any>) {
  const base =
    typeof window === 'undefined' ? 'http://localhost' : window.location.origin;
  const target = new URL(url, base);
  Object.entries(params || {}).forEach(([key, value]) => {
    if (value === undefined || value === null || value === '') {
      return;
    }
    target.searchParams.set(key, String(value));
  });
  return `${target.pathname}${target.search}`;
}
