import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PassportModule } from '@nestjs/passport';
import { JwtModule } from '@nestjs/jwt';

// Domain entities
import { AiConversation } from './domain/entities/ai-conversation.entity';

// Domain repository tokens
import { CONVERSATION_REPOSITORY } from './domain/repositories/conversation.repository.interface';

// Infrastructure — persistence
import { ConversationRepository } from './infrastructure/persistence/conversation.repository';

// Infrastructure — external agents
import { AI_AGENT_CLIENT } from './domain/ports/ai-agent.client.interface';
import { AiAgentClient } from './infrastructure/external-agents/ai-agent.client';

// Application services
import { AiChatService } from './application/services/ai-chat.service';
import { AiCreditService } from './application/services/ai-credit.service';
import { AiPricingService } from './application/services/ai-pricing.service';
import { AiAnomalyService } from './application/services/ai-anomaly.service';
import { AdminAgentService } from './application/services/admin-agent.service';

// Infrastructure — auth
import { JwtStrategy } from './infrastructure/strategies/jwt.strategy';

// Interface — HTTP controllers
import { AiController } from './interface/http/controllers/ai.controller';
import { AdminAgentController } from './interface/http/controllers/admin-agent.controller';

@Module({
  imports: [
    TypeOrmModule.forFeature([AiConversation]),
    PassportModule.register({ defaultStrategy: 'jwt' }),
    JwtModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        secret: config.get<string>('JWT_ACCESS_SECRET', 'dev-access-secret'),
      }),
    }),
  ],
  controllers: [AiController, AdminAgentController],
  providers: [
    // Infrastructure bindings (interface → implementation)
    { provide: AI_AGENT_CLIENT, useClass: AiAgentClient },
    { provide: CONVERSATION_REPOSITORY, useClass: ConversationRepository },

    // Auth
    JwtStrategy,

    // Application services
    AiChatService,
    AiCreditService,
    AiPricingService,
    AiAnomalyService,
    AdminAgentService,
  ],
  exports: [AiChatService, AiCreditService, AiPricingService, AiAnomalyService],
})
export class AiModule {}
