import { Router } from 'express';
import { DataSource } from 'typeorm';
import { SalesService } from './services';
import { QuotationsController, SalesOrdersController } from './controllers';
import { Quotation, SalesOrder } from './entities';

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

export class SalesModule {
  public router: Router;
  public salesService: SalesService;
  private dataSource: DataSource;
  private basePath: string;

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

  private initializeServices(): void {
    const quotationRepository = this.dataSource.getRepository(Quotation);
    const orderRepository = this.dataSource.getRepository(SalesOrder);
    this.salesService = new SalesService(quotationRepository, orderRepository);
  }

  private initializeRoutes(): void {
    const quotationsController = new QuotationsController(this.salesService);
    const ordersController = new SalesOrdersController(this.salesService);
    this.router.use(`${this.basePath}/quotations`, quotationsController.router);
    this.router.use(`${this.basePath}/sales-orders`, ordersController.router);
  }

  static getEntities(): Function[] {
    return [Quotation, SalesOrder];
  }
}
