import { Router } from 'express';
import { DataSource } from 'typeorm';
import { PurchasesService } from './services';
import { PurchasesController } from './controllers';
import { PurchaseOrder } from './entities';

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

export class PurchasesModule {
  public router: Router;
  public purchasesService: PurchasesService;
  private dataSource: DataSource;
  private basePath: string;

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

  private initializeServices(): void {
    const orderRepository = this.dataSource.getRepository(PurchaseOrder);
    this.purchasesService = new PurchasesService(orderRepository);
  }

  private initializeRoutes(): void {
    const purchasesController = new PurchasesController(this.purchasesService);
    this.router.use(`${this.basePath}/purchase-orders`, purchasesController.router);
  }

  static getEntities(): Function[] {
    return [PurchaseOrder];
  }
}
