import * as interfaces from '../ts_interfaces/index.js';
import type { DcRouterApiClient } from './classes.dcrouterapiclient.js';

type TTimeRange = '1h' | '6h' | '24h' | '7d' | '30d';

export class StatsManager {
  private clientRef: DcRouterApiClient;

  constructor(clientRef: DcRouterApiClient) {
    this.clientRef = clientRef;
  }

  public async getServer(options?: {
    timeRange?: TTimeRange;
    includeHistory?: boolean;
  }): Promise<interfaces.requests.IReq_GetServerStatistics['response']> {
    return this.clientRef.request<interfaces.requests.IReq_GetServerStatistics>(
      'getServerStatistics',
      this.clientRef.buildRequestPayload(options || {}) as any,
    );
  }

  public async getEmail(options?: {
    timeRange?: TTimeRange;
    domain?: string;
    includeDetails?: boolean;
  }): Promise<interfaces.requests.IReq_GetEmailStatistics['response']> {
    return this.clientRef.request<interfaces.requests.IReq_GetEmailStatistics>(
      'getEmailStatistics',
      this.clientRef.buildRequestPayload(options || {}) as any,
    );
  }

  public async getDns(options?: {
    timeRange?: TTimeRange;
    domain?: string;
    includeQueryTypes?: boolean;
  }): Promise<interfaces.requests.IReq_GetDnsStatistics['response']> {
    return this.clientRef.request<interfaces.requests.IReq_GetDnsStatistics>(
      'getDnsStatistics',
      this.clientRef.buildRequestPayload(options || {}) as any,
    );
  }

  public async getRateLimits(options?: {
    domain?: string;
    ip?: string;
    includeBlocked?: boolean;
  }): Promise<interfaces.requests.IReq_GetRateLimitStatus['response']> {
    return this.clientRef.request<interfaces.requests.IReq_GetRateLimitStatus>(
      'getRateLimitStatus',
      this.clientRef.buildRequestPayload(options || {}) as any,
    );
  }

  public async getSecurity(options?: {
    timeRange?: TTimeRange;
    includeDetails?: boolean;
  }): Promise<interfaces.requests.IReq_GetSecurityMetrics['response']> {
    return this.clientRef.request<interfaces.requests.IReq_GetSecurityMetrics>(
      'getSecurityMetrics',
      this.clientRef.buildRequestPayload(options || {}) as any,
    );
  }

  public async getConnections(options?: {
    protocol?: 'smtp' | 'smtps' | 'http' | 'https';
    state?: string;
  }): Promise<interfaces.requests.IReq_GetActiveConnections['response']> {
    return this.clientRef.request<interfaces.requests.IReq_GetActiveConnections>(
      'getActiveConnections',
      this.clientRef.buildRequestPayload(options || {}) as any,
    );
  }

  public async getQueues(options?: {
    queueName?: string;
  }): Promise<interfaces.requests.IReq_GetQueueStatus['response']> {
    return this.clientRef.request<interfaces.requests.IReq_GetQueueStatus>(
      'getQueueStatus',
      this.clientRef.buildRequestPayload(options || {}) as any,
    );
  }

  public async getHealth(detailed?: boolean): Promise<interfaces.requests.IReq_GetHealthStatus['response']> {
    return this.clientRef.request<interfaces.requests.IReq_GetHealthStatus>(
      'getHealthStatus',
      this.clientRef.buildRequestPayload({ detailed }) as any,
    );
  }

  public async getNetwork(): Promise<interfaces.requests.IReq_GetNetworkStats['response']> {
    return this.clientRef.request<interfaces.requests.IReq_GetNetworkStats>(
      'getNetworkStats',
      this.clientRef.buildRequestPayload() as any,
    );
  }

  public async getCombined(sections?: {
    server?: boolean;
    email?: boolean;
    dns?: boolean;
    security?: boolean;
    network?: boolean;
  }): Promise<interfaces.requests.IReq_GetCombinedMetrics['response']> {
    return this.clientRef.request<interfaces.requests.IReq_GetCombinedMetrics>(
      'getCombinedMetrics',
      this.clientRef.buildRequestPayload({ sections }) as any,
    );
  }
}
