import { Router } from 'express';
import { DataSource } from 'typeorm';
import { BiometricsService } from './services/biometrics.service.js';
import { BiometricsController } from './biometrics.controller.js';
import { createBiometricsRoutes } from './biometrics.routes.js';
import {
  Device,
  BiometricCredential,
  DeviceSession,
  DeviceActivityLog,
} from './entities/index.js';

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

/**
 * BiometricsModule - Modulo de solo lectura para datos biometricos
 *
 * Este modulo proporciona endpoints GET para consultar:
 * - Dispositivos registrados
 * - Credenciales biometricas
 * - Estado de enrollment
 * - Sesiones activas
 * - Log de actividad
 *
 * La funcionalidad de escritura se maneja desde el modulo `auth`.
 */
export class BiometricsModule {
  public router: Router;
  public biometricsService: BiometricsService;
  private dataSource: DataSource;
  private basePath: string;

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

  private initializeServices(): void {
    const deviceRepository = this.dataSource.getRepository(Device);
    const credentialRepository = this.dataSource.getRepository(BiometricCredential);
    const sessionRepository = this.dataSource.getRepository(DeviceSession);
    const activityLogRepository = this.dataSource.getRepository(DeviceActivityLog);

    this.biometricsService = new BiometricsService(
      deviceRepository,
      credentialRepository,
      sessionRepository,
      activityLogRepository
    );
  }

  private initializeRoutes(): void {
    const biometricsController = new BiometricsController(this.biometricsService);
    const routes = createBiometricsRoutes(biometricsController);
    this.router.use(this.basePath, routes);
  }

  /**
   * Retorna las entidades del modulo para registrar en TypeORM
   */
  static getEntities(): Function[] {
    return [Device, BiometricCredential, DeviceSession, DeviceActivityLog];
  }
}
