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

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

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

  private registerHandlers(): void {
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetServerConfig>(
        'getServerConfig',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          const containerConfig = this.opsServerRef.objectStorageRef.config;
          const config: interfaces.data.IServerConfig = {
            objstPort: containerConfig.objstPort,
            uiPort: containerConfig.uiPort,
            region: containerConfig.region,
            storageDirectory: containerConfig.storageDirectory,
            authEnabled: true,
            corsEnabled: false,
            clusterEnabled: containerConfig.clusterEnabled,
            clusterNodeId: containerConfig.clusterNodeId,
            clusterQuicPort: containerConfig.clusterQuicPort,
            clusterSeedNodes: containerConfig.clusterSeedNodes,
            erasureDataShards: containerConfig.erasureDataShards,
            erasureParityShards: containerConfig.erasureParityShards,
            erasureChunkSizeBytes: containerConfig.erasureChunkSizeBytes,
            drivePaths: containerConfig.drivePaths,
            clusterHeartbeatIntervalMs: containerConfig.clusterHeartbeatIntervalMs,
            clusterHeartbeatTimeoutMs: containerConfig.clusterHeartbeatTimeoutMs,
          };
          return { config };
        },
      ),
    );
  }
}
