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

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

  private registerHandlers(): void {
    // List objects
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_ListObjects>(
        'listObjects',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          const result = await this.opsServerRef.objectStorageRef.listObjects(
            dataArg.bucketName,
            dataArg.prefix,
            dataArg.delimiter,
            dataArg.maxKeys,
          );
          return { result };
        },
      ),
    );

    // Delete object
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_DeleteObject>(
        'deleteObject',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          await this.opsServerRef.objectStorageRef.deleteObject(dataArg.bucketName, dataArg.key);
          return { ok: true };
        },
      ),
    );

    // Get object
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetObject>(
        'getObject',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          return await this.opsServerRef.objectStorageRef.getObject(dataArg.bucketName, dataArg.key);
        },
      ),
    );

    // Put object
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_PutObject>(
        'putObject',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          await this.opsServerRef.objectStorageRef.putObject(
            dataArg.bucketName,
            dataArg.key,
            dataArg.base64Content,
            dataArg.contentType,
          );
          return { ok: true };
        },
      ),
    );

    // Delete prefix (folder)
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_DeletePrefix>(
        'deletePrefix',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          await this.opsServerRef.objectStorageRef.deletePrefix(dataArg.bucketName, dataArg.prefix);
          return { ok: true };
        },
      ),
    );

    // Get object URL
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetObjectUrl>(
        'getObjectUrl',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          const url = await this.opsServerRef.objectStorageRef.getObjectUrl(dataArg.bucketName, dataArg.key);
          return { url };
        },
      ),
    );

    // Move object
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_MoveObject>(
        'moveObject',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          return await this.opsServerRef.objectStorageRef.moveObject(
            dataArg.bucketName,
            dataArg.sourceKey,
            dataArg.destKey,
          );
        },
      ),
    );

    // Move prefix (folder)
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_MovePrefix>(
        'movePrefix',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          return await this.opsServerRef.objectStorageRef.movePrefix(
            dataArg.bucketName,
            dataArg.sourcePrefix,
            dataArg.destPrefix,
          );
        },
      ),
    );
  }
}
