import * as plugins from '../../plugins.ts';
import type { OpsServer } from '../classes.opsserver.ts';
import * as interfaces from '../../../ts_interfaces/index.ts';
import { requireAdminIdentity } from '../helpers/guards.ts';
import type { TPlatformServiceType } from '../../types.ts';

export class NetworkHandler {
  public typedrouter = new plugins.typedrequest.TypedRouter();

  constructor(private opsServerRef: OpsServer) {
    this.opsServerRef.typedrouter.addTypedRouter(this.typedrouter);
    this.registerHandlers();
  }

  private getPlatformServicePort(type: TPlatformServiceType): number {
    const ports: Record<TPlatformServiceType, number> = {
      mongodb: 27017,
      minio: 9000,
      redis: 6379,
      postgresql: 5432,
      rabbitmq: 5672,
      smartproxy: 80,
      clickhouse: 8123,
      mariadb: 3306,
    };
    return ports[type] || 0;
  }

  private registerHandlers(): void {
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetNetworkTargets>(
        'getNetworkTargets',
        async (dataArg) => {
          await requireAdminIdentity(this.opsServerRef.adminHandler, dataArg);
          const targets: interfaces.data.INetworkTarget[] = [];

          // Services
          const services = this.opsServerRef.oneboxRef.services.listServices();
          for (const svc of services) {
            targets.push({
              type: 'service',
              name: svc.name,
              domain: svc.domain || null,
              targetHost: (svc as any).containerIP || svc.containerID || 'unknown',
              targetPort: svc.port || 80,
              status: svc.status,
            });
          }

          // Registry
          const registryStatus = this.opsServerRef.oneboxRef.registry.getStatus();
          if (registryStatus.running) {
            targets.push({
              type: 'registry',
              name: 'onebox-registry',
              domain: null,
              targetHost: 'localhost',
              targetPort: registryStatus.port,
              status: 'running',
            });
          }

          // Platform services
          const platformServices = this.opsServerRef.oneboxRef.platformServices.getAllPlatformServices();
          for (const ps of platformServices) {
            const provider = this.opsServerRef.oneboxRef.platformServices.getProvider(ps.type);
            targets.push({
              type: 'platform',
              name: provider?.displayName || ps.type,
              domain: null,
              targetHost: 'localhost',
              targetPort: this.getPlatformServicePort(ps.type),
              status: ps.status,
            });
          }

          return { targets };
        },
      ),
    );

    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetNetworkStats>(
        'getNetworkStats',
        async (dataArg) => {
          await requireAdminIdentity(this.opsServerRef.adminHandler, dataArg);
          const proxyStatus = this.opsServerRef.oneboxRef.reverseProxy.getStatus() as any;
          const logReceiverStats = this.opsServerRef.oneboxRef.proxyLogReceiver.getStats();

          return {
            stats: {
              proxy: {
                running: proxyStatus.running ?? proxyStatus.http?.running ?? false,
                httpPort: proxyStatus.httpPort ?? proxyStatus.http?.port ?? 80,
                httpsPort: proxyStatus.httpsPort ?? proxyStatus.https?.port ?? 443,
                routes: proxyStatus.routes ?? 0,
                certificates: proxyStatus.certificates ?? proxyStatus.https?.certificates ?? 0,
              },
              logReceiver: {
                running: logReceiverStats.running,
                port: logReceiverStats.port,
                clients: logReceiverStats.clients,
                connections: logReceiverStats.connections,
                sampleRate: logReceiverStats.sampleRate,
                recentLogsCount: logReceiverStats.recentLogsCount,
              },
            },
          };
        },
      ),
    );

    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetTrafficStats>(
        'getTrafficStats',
        async (dataArg) => {
          await requireAdminIdentity(this.opsServerRef.adminHandler, dataArg);
          const trafficStats = this.opsServerRef.oneboxRef.proxyLogReceiver.getTrafficStats(60);
          return { stats: trafficStats };
        },
      ),
    );
  }
}
