import type * as servezoneInterfaces from '@serve.zone/interfaces';
import type { DcRouterApiClient } from './classes.dcrouterapiclient.js';

export class GatewayClientApi {
  constructor(private clientRef: DcRouterApiClient) {}

  public async getCapabilities(): Promise<servezoneInterfaces.data.IGatewayCapabilities> {
    const response = await this.clientRef.request<servezoneInterfaces.requests.gateway.IReq_GetGatewayCapabilities>(
      'getGatewayCapabilities',
      this.clientRef.buildRequestPayload() as any,
    );
    return response.capabilities;
  }

  public async getGatewayClientContext(): Promise<servezoneInterfaces.data.IGatewayClientContext> {
    const response = await this.clientRef.request<servezoneInterfaces.requests.gateway.IReq_GetGatewayClientContext>(
      'getGatewayClientContext',
      this.clientRef.buildRequestPayload() as any,
    );
    return response.context;
  }

  public async getDomains(): Promise<servezoneInterfaces.data.IGatewayDomain[]> {
    const response = await this.clientRef.request<servezoneInterfaces.requests.gateway.IReq_GetGatewayClientDomains>(
      'getGatewayClientDomains',
      this.clientRef.buildRequestPayload() as any,
    );
    return response.domains;
  }

  public async getDnsRecords(): Promise<servezoneInterfaces.data.IGatewayDnsRecord[]> {
    const response = await this.clientRef.request<servezoneInterfaces.requests.gateway.IReq_GetGatewayClientDnsRecords>(
      'getGatewayClientDnsRecords',
      this.clientRef.buildRequestPayload() as any,
    );
    return response.records;
  }

  public async syncRoute(options: {
    ownership: servezoneInterfaces.data.IGatewayClientOwnership;
    route: servezoneInterfaces.data.IGatewayRouteConfig;
    enabled?: boolean;
  }): Promise<servezoneInterfaces.data.IGatewayRouteSyncResult> {
    return this.clientRef.request<servezoneInterfaces.requests.gateway.IReq_SyncGatewayClientRoute>(
      'syncGatewayClientRoute',
      this.clientRef.buildRequestPayload({
        ownership: options.ownership,
        route: options.route,
        enabled: options.enabled,
      }) as any,
    );
  }

  public async deleteRoute(
    ownership: servezoneInterfaces.data.IGatewayClientOwnership,
  ): Promise<servezoneInterfaces.data.IGatewayRouteSyncResult> {
    return this.clientRef.request<servezoneInterfaces.requests.gateway.IReq_SyncGatewayClientRoute>(
      'syncGatewayClientRoute',
      this.clientRef.buildRequestPayload({
        ownership,
        delete: true,
      }) as any,
    );
  }
}
