/**
 * Payment Terminals Module
 *
 * Module registration for payment terminals and transactions
 * Includes: MercadoPago, Clip, Stripe Terminal
 */

import { Router } from 'express';
import { DataSource } from 'typeorm';
import {
  TerminalsController,
  TransactionsController,
  MercadoPagoController,
  MercadoPagoWebhookController,
  ClipController,
  ClipWebhookController,
} from './controllers';

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

export class PaymentTerminalsModule {
  public router: Router;
  public webhookRouter: Router;

  private terminalsController: TerminalsController;
  private transactionsController: TransactionsController;
  private mercadoPagoController: MercadoPagoController;
  private mercadoPagoWebhookController: MercadoPagoWebhookController;
  private clipController: ClipController;
  private clipWebhookController: ClipWebhookController;

  constructor(options: PaymentTerminalsModuleOptions) {
    const { dataSource, basePath = '' } = options;

    this.router = Router();
    this.webhookRouter = Router();

    // Initialize controllers
    this.terminalsController = new TerminalsController(dataSource);
    this.transactionsController = new TransactionsController(dataSource);
    this.mercadoPagoController = new MercadoPagoController(dataSource);
    this.mercadoPagoWebhookController = new MercadoPagoWebhookController(dataSource);
    this.clipController = new ClipController(dataSource);
    this.clipWebhookController = new ClipWebhookController(dataSource);

    // Register authenticated routes
    this.router.use(`${basePath}/payment-terminals`, this.terminalsController.router);
    this.router.use(`${basePath}/payment-transactions`, this.transactionsController.router);
    this.router.use(`${basePath}/mercadopago`, this.mercadoPagoController.router);
    this.router.use(`${basePath}/clip`, this.clipController.router);

    // Register public webhook routes (no auth required)
    this.webhookRouter.use('/mercadopago', this.mercadoPagoWebhookController.router);
    this.webhookRouter.use('/clip', this.clipWebhookController.router);
  }

  /**
   * Get all entities for this module (for TypeORM configuration)
   */
  static getEntities() {
    return [
      // Existing entities
      require('../branches/entities/branch-payment-terminal.entity').BranchPaymentTerminal,
      require('../mobile/entities/payment-transaction.entity').PaymentTransaction,
      // New entities for MercadoPago/Clip
      require('./entities/tenant-terminal-config.entity').TenantTerminalConfig,
      require('./entities/terminal-payment.entity').TerminalPayment,
      require('./entities/terminal-webhook-event.entity').TerminalWebhookEvent,
    ];
  }
}

export default PaymentTerminalsModule;
