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

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

  private registerHandlers(): void {
    // List buckets
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_ListBuckets>(
        'listBuckets',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          const buckets = await this.opsServerRef.objectStorageRef.listBuckets();
          return { buckets };
        },
      ),
    );

    // Create bucket
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_CreateBucket>(
        'createBucket',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          await this.opsServerRef.objectStorageRef.createBucket(dataArg.bucketName);
          return { ok: true };
        },
      ),
    );

    // Delete bucket
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_DeleteBucket>(
        'deleteBucket',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          await this.opsServerRef.objectStorageRef.deleteBucket(dataArg.bucketName);
          await this.opsServerRef.objectStorageRef.policyManager.onBucketDeleted(dataArg.bucketName);
          return { ok: true };
        },
      ),
    );

    // Get bucket policy
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetBucketPolicy>(
        'getBucketPolicy',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          const policy = await this.opsServerRef.objectStorageRef.getBucketPolicy(dataArg.bucketName);
          return { policy };
        },
      ),
    );

    // Put bucket policy
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_PutBucketPolicy>(
        'putBucketPolicy',
        async (dataArg) => {
          await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
          // Validate JSON
          try {
            JSON.parse(dataArg.policy);
          } catch {
            throw new Error('Invalid JSON policy document');
          }
          await this.opsServerRef.objectStorageRef.putBucketPolicy(dataArg.bucketName, dataArg.policy);
          return { ok: true };
        },
      ),
    );

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