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

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

  private get actionLog() {
    return this.opsServerRef.gitopsAppRef.actionLog;
  }

  private registerHandlers(): void {
    // Get pipelines
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetPipelines>(
        'getPipelines',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          const provider = this.opsServerRef.gitopsAppRef.connectionManager.getProvider(
            dataArg.connectionId,
          );
          const pipelines = await provider.getPipelines(dataArg.projectId, {
            page: dataArg.page,
          });
          return { pipelines };
        },
      ),
    );

    // Get pipeline jobs
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetPipelineJobs>(
        'getPipelineJobs',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          const provider = this.opsServerRef.gitopsAppRef.connectionManager.getProvider(
            dataArg.connectionId,
          );
          const jobs = await provider.getPipelineJobs(dataArg.projectId, dataArg.pipelineId);
          return { jobs };
        },
      ),
    );

    // Retry pipeline
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_RetryPipeline>(
        'retryPipeline',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          const provider = this.opsServerRef.gitopsAppRef.connectionManager.getProvider(
            dataArg.connectionId,
          );
          await provider.retryPipeline(dataArg.projectId, dataArg.pipelineId);
          this.actionLog.append({
            actionType: 'update',
            entityType: 'pipeline',
            entityId: dataArg.pipelineId,
            entityName: `Pipeline #${dataArg.pipelineId}`,
            details: `Retried pipeline #${dataArg.pipelineId} in project ${dataArg.projectId}`,
            username: dataArg.identity.username,
          });
          return { ok: true };
        },
      ),
    );

    // Cancel pipeline
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_CancelPipeline>(
        'cancelPipeline',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          const provider = this.opsServerRef.gitopsAppRef.connectionManager.getProvider(
            dataArg.connectionId,
          );
          await provider.cancelPipeline(dataArg.projectId, dataArg.pipelineId);
          this.actionLog.append({
            actionType: 'delete',
            entityType: 'pipeline',
            entityId: dataArg.pipelineId,
            entityName: `Pipeline #${dataArg.pipelineId}`,
            details: `Cancelled pipeline #${dataArg.pipelineId} in project ${dataArg.projectId}`,
            username: dataArg.identity.username,
          });
          return { ok: true };
        },
      ),
    );
  }
}
