import { Router } from 'express';
import { DataSource } from 'typeorm';
import { InventoryService } from './services';
import { InventoryController } from './controllers';
import { StockLevel, StockMovement } from './entities';

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

export class InventoryModule {
  public router: Router;
  public inventoryService: InventoryService;
  private dataSource: DataSource;
  private basePath: string;

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

  private initializeServices(): void {
    const stockLevelRepository = this.dataSource.getRepository(StockLevel);
    const movementRepository = this.dataSource.getRepository(StockMovement);

    this.inventoryService = new InventoryService(
      stockLevelRepository,
      movementRepository,
      this.dataSource
    );
  }

  private initializeRoutes(): void {
    const inventoryController = new InventoryController(this.inventoryService);
    this.router.use(`${this.basePath}/inventory`, inventoryController.router);
  }

  static getEntities(): Function[] {
    return [StockLevel, StockMovement];
  }
}
