import request from './request';

export type DeviceType = 'CONTROLLER' | 'CONTROLLED' | null;

export interface DeviceInfo {
  deviceId: string;
  deviceType: DeviceType;
  online: boolean;
  connectedAt: number | null;
}

export interface DashboardData {
  serverStartTime: number;
  currentTime: number;
  uptimeMs: number;
  totalDevices: number;
  onlineDevices: number;
  offlineDevices: number;
  onlineControllers: number;
  onlineControlled: number;
  pendingConnections: number;
  metrics: {
    totalMessages: number;
    totalOffers: number;
    totalAnswers: number;
    totalRejected: number;
    totalRegistered: number;
    peakSessions: number;
  };
}

export interface PendingConnection {
  fromDeviceId: string;
  toDeviceId: string;
  createdAt: number;
  elapsedMs: number;
}

export interface ServerConfig {
  dedupWindowMs: number;
  requestTimeoutMs: number;
  websocketEndpoint: string;
  serverPort: number;
}

export function getDashboard(): Promise<DashboardData> {
  return request.get('/api/admin/dashboard') as unknown as Promise<DashboardData>;
}

export function getDevices(type?: string): Promise<DeviceInfo[]> {
  return request.get('/api/admin/devices', { params: type ? { type } : {} }) as unknown as Promise<
    DeviceInfo[]
  >;
}

export function getConnections(): Promise<PendingConnection[]> {
  return request.get('/api/admin/connections') as unknown as Promise<PendingConnection[]>;
}

export function getConfig(): Promise<ServerConfig> {
  return request.get('/api/admin/config') as unknown as Promise<ServerConfig>;
}

export function getHealth(): Promise<{ status: string }> {
  return request.get('/api/admin/health') as unknown as Promise<{ status: string }>;
}

/** 校验令牌有效性（用于登录时验证）。 */
export function verifyToken(token: string): Promise<unknown> {
  return request.get('/api/admin/health', { headers: { 'X-Admin-Token': token } });
}
