import * as plugins from '../plugins.ts';
import type { ObjectStorageContainer } from '../classes/objectstoragecontainer.ts';
import * as handlers from './handlers/index.ts';
import { files as bundledFiles } from '../../ts_bundled/bundle.ts';

export class OpsServer {
  public objectStorageRef: ObjectStorageContainer;
  public typedrouter = new plugins.typedrequest.TypedRouter();
  public server!: plugins.typedserver.utilityservers.UtilityWebsiteServer;

  // Handler instances
  public adminHandler!: handlers.AdminHandler;
  public statusHandler!: handlers.StatusHandler;
  public bucketsHandler!: handlers.BucketsHandler;
  public objectsHandler!: handlers.ObjectsHandler;
  public configHandler!: handlers.ConfigHandler;
  public credentialsHandler!: handlers.CredentialsHandler;
  public policiesHandler!: handlers.PoliciesHandler;

  constructor(objectStorageRef: ObjectStorageContainer) {
    this.objectStorageRef = objectStorageRef;
  }

  public async start(port = 3000) {
    this.server = new plugins.typedserver.utilityservers.UtilityWebsiteServer({
      domain: 'localhost',
      feedMetadata: undefined,
      bundledContent: bundledFiles,
    });

    // Chain typedrouters: server -> opsServer -> individual handlers
    this.server.typedrouter.addTypedRouter(this.typedrouter);

    // Set up all handlers
    await this.setupHandlers();

    await this.server.start(port);
    console.log(`OpsServer started on http://localhost:${port}`);
  }

  private async setupHandlers(): Promise<void> {
    // AdminHandler requires async initialization for JWT key generation
    this.adminHandler = new handlers.AdminHandler(this);
    await this.adminHandler.initialize();

    // All other handlers self-register in their constructors
    this.statusHandler = new handlers.StatusHandler(this);
    this.bucketsHandler = new handlers.BucketsHandler(this);
    this.objectsHandler = new handlers.ObjectsHandler(this);
    this.configHandler = new handlers.ConfigHandler(this);
    this.credentialsHandler = new handlers.CredentialsHandler(this);
    this.policiesHandler = new handlers.PoliciesHandler(this);

    console.log('OpsServer TypedRequest handlers initialized');
  }

  public async stop() {
    if (this.server) {
      await this.server.stop();
      console.log('OpsServer stopped');
    }
  }
}
