import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ScheduleModule } from '@nestjs/schedule';
import { APP_INTERCEPTOR } from '@nestjs/core';

import { SecurityIpLog } from './security-ip-log.entity';
import { SecurityAuditLog } from './security-audit-log.entity';
import { SecurityIpLogService } from './security-ip-log.service';
import { SecurityAuditLogService } from './security-audit-log.service';
import { SecurityIpLogController } from './security-ip-log.controller';
import { SecurityIpLogInterceptor } from './security-ip-log.interceptor';
import { AdminGuard } from './guards/admin.guard';

/**
 * SecurityIpLogModule - Law enforcement and fraud prevention IP logging
 *
 * Legal Basis:
 * - GDPR Article 6(1)(c): Legal obligation (law enforcement cooperation)
 * - GDPR Article 6(1)(f): Legitimate interest (fraud prevention)
 *
 * This module is SEPARATE from consent-free analytics:
 * - Analytics: ephemeral session IDs, no device storage, privacy-first
 * - Security logs: raw IP storage with audit trail for legal compliance
 *
 * Features:
 * - Global request interceptor logs all HTTP requests with raw IPs
 * - Admin-only endpoints for querying IP logs
 * - Mandatory audit logging for all queries
 * - 90-day automatic retention with daily purge cron
 *
 * Access Restrictions:
 * - Controller endpoints require admin authentication
 * - Every query is logged to security_audit_log
 * - Rate limiting recommended at nginx level
 */
@Module({
  imports: [
    ScheduleModule.forRoot(),
    TypeOrmModule.forFeature([SecurityIpLog, SecurityAuditLog]),
  ],
  controllers: [SecurityIpLogController],
  providers: [
    SecurityIpLogService,
    SecurityAuditLogService,
    AdminGuard,
    // Register interceptor globally for this module
    // Note: For truly global registration, add to AppModule instead
    {
      provide: APP_INTERCEPTOR,
      useClass: SecurityIpLogInterceptor,
    },
  ],
  exports: [SecurityIpLogService, SecurityAuditLogService],
})
export class SecurityIpLogModule {}
