import { Router } from 'express';
import { DataSource } from 'typeorm';
import { WarehousesService } from './services';
import { WarehousesController } from './controllers';
import { Warehouse, WarehouseLocation } from './entities';

export interface WarehousesModuleOptions {
  dataSource: DataSource;
  basePath?: string;
}

export class WarehousesModule {
  public router: Router;
  public warehousesService: WarehousesService;
  private dataSource: DataSource;
  private basePath: string;

  constructor(options: WarehousesModuleOptions) {
    this.dataSource = options.dataSource;
    this.basePath = options.basePath || '';
    this.router = Router();
    this.initializeServices();
    this.initializeRoutes();
  }

  private initializeServices(): void {
    const warehouseRepository = this.dataSource.getRepository(Warehouse);
    const locationRepository = this.dataSource.getRepository(WarehouseLocation);

    this.warehousesService = new WarehousesService(warehouseRepository, locationRepository);
  }

  private initializeRoutes(): void {
    const warehousesController = new WarehousesController(this.warehousesService);
    this.router.use(`${this.basePath}/warehouses`, warehousesController.router);
  }

  static getEntities(): Function[] {
    return [Warehouse, WarehouseLocation];
  }
}
