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 ConnectionsHandler {
  public typedrouter = new plugins.typedrequest.TypedRouter();

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

  private registerHandlers(): void {
    // Get all connections
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetConnections>(
        'getConnections',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          const connections = this.opsServerRef.gitopsAppRef.connectionManager.getConnections();
          return { connections };
        },
      ),
    );

    // Create connection
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_CreateConnection>(
        'createConnection',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          const connection = await this.opsServerRef.gitopsAppRef.connectionManager.createConnection(
            dataArg.name,
            dataArg.providerType,
            dataArg.baseUrl,
            dataArg.token,
          );
          return { connection };
        },
      ),
    );

    // Update connection
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_UpdateConnection>(
        'updateConnection',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          const connection = await this.opsServerRef.gitopsAppRef.connectionManager.updateConnection(
            dataArg.connectionId,
            {
              name: dataArg.name,
              baseUrl: dataArg.baseUrl,
              token: dataArg.token,
            },
          );
          return { connection };
        },
      ),
    );

    // Test connection
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_TestConnection>(
        'testConnection',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          const result = await this.opsServerRef.gitopsAppRef.connectionManager.testConnection(
            dataArg.connectionId,
          );
          return result;
        },
      ),
    );

    // Delete connection
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_DeleteConnection>(
        'deleteConnection',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          await this.opsServerRef.gitopsAppRef.connectionManager.deleteConnection(
            dataArg.connectionId,
          );
          return { ok: true };
        },
      ),
    );
  }
}
