/**
 * Billing Usage Module
 *
 * Module registration for billing and usage tracking
 */

import { Router } from 'express';
import { DataSource } from 'typeorm';
import {
  SubscriptionPlansController,
  SubscriptionsController,
  UsageController,
  InvoicesController,
} from './controllers';

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

export class BillingUsageModule {
  public router: Router;
  private subscriptionPlansController: SubscriptionPlansController;
  private subscriptionsController: SubscriptionsController;
  private usageController: UsageController;
  private invoicesController: InvoicesController;

  constructor(options: BillingUsageModuleOptions) {
    const { dataSource, basePath = '/billing' } = options;

    this.router = Router();

    // Initialize controllers
    this.subscriptionPlansController = new SubscriptionPlansController(dataSource);
    this.subscriptionsController = new SubscriptionsController(dataSource);
    this.usageController = new UsageController(dataSource);
    this.invoicesController = new InvoicesController(dataSource);

    // Register routes
    this.router.use(`${basePath}/subscription-plans`, this.subscriptionPlansController.router);
    this.router.use(`${basePath}/subscriptions`, this.subscriptionsController.router);
    this.router.use(`${basePath}/usage`, this.usageController.router);
    this.router.use(`${basePath}/invoices`, this.invoicesController.router);
  }

  /**
   * Get all entities for this module (for TypeORM configuration)
   */
  static getEntities() {
    return [
      require('./entities/subscription-plan.entity').SubscriptionPlan,
      require('./entities/tenant-subscription.entity').TenantSubscription,
      require('./entities/usage-tracking.entity').UsageTracking,
      require('./entities/invoice.entity').Invoice,
      require('./entities/invoice-item.entity').InvoiceItem,
      require('./entities/plan-feature.entity').PlanFeature,
    ];
  }
}

export default BillingUsageModule;
