import { Module } from '@nestjs/common';
import Redis from 'ioredis';

import { getConfig } from '@hexahost/config';

import { BillingModule } from '../../billing/billing.module';
import { AuthModule } from '../../auth/auth.module';
import { ServersModule } from '../../servers/servers.module';

import { WhmcsIntegrationController } from './whmcs-integration.controller';
import { WhmcsDashboardService } from './whmcs-dashboard.service';
import { WhmcsEventsService } from './whmcs-events.service';
import { INTEGRATIONS_REDIS } from './whmcs-integration.constants';
import { WhmcsIntegrationGuard } from './whmcs-integration.guard';
import { WhmcsIntegrationService } from './whmcs-integration.service';
import { WhmcsMtlsService } from './whmcs-mtls.service';
import { WhmcsReconciliationService } from './whmcs-reconciliation.service';
import { WhmcsUsageService } from './whmcs-usage.service';

export { INTEGRATIONS_REDIS } from './whmcs-integration.constants';

@Module({
  imports: [ServersModule, BillingModule, AuthModule],
  controllers: [WhmcsIntegrationController],
  providers: [
    WhmcsIntegrationService,
    WhmcsReconciliationService,
    WhmcsEventsService,
    WhmcsUsageService,
    WhmcsDashboardService,
    WhmcsMtlsService,
    WhmcsIntegrationGuard,
    {
      provide: INTEGRATIONS_REDIS,
      useFactory: () => {
        const config = getConfig();
        return new Redis(config.REDIS_URL, {
          maxRetriesPerRequest: null,
        });
      },
    },
    {
      provide: Redis,
      useExisting: INTEGRATIONS_REDIS,
    },
  ],
})
export class WhmcsIntegrationModule {}
