import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from '@modules/auth/entities/user.entity';
import { Profile } from '@modules/auth/entities/profile.entity';
import { Role } from '@modules/auth/entities/role.entity';
import { UserRole } from '@modules/auth/entities/user-role.entity';
import { Tenant } from '@modules/auth/entities/tenant.entity';
import { Membership } from '@modules/auth/entities/membership.entity';
import { AuthAttempt } from '@modules/auth/entities/auth-attempt.entity';
import { UserSuspension } from '@modules/auth/entities/user-suspension.entity';
import { Module as EducationalModule } from '@modules/educational/entities/module.entity';
import { Exercise } from '@modules/educational/entities/exercise.entity';
import { ContentApproval } from '@modules/educational/entities/content-approval.entity';
import { ContentTemplate } from '@modules/content/entities/content-template.entity';
import { MediaFile } from '@modules/content/entities/media-file.entity';
import { Classroom } from '@modules/social/entities/classroom.entity';
import { TeacherClassroom } from '@modules/social/entities/teacher-classroom.entity';
import { AssignmentClassroom } from '@modules/social/entities/assignment-classroom.entity';
import { Assignment } from '@modules/assignments/entities/assignment.entity';
import { AssignmentStudent } from '@modules/assignments/entities/assignment-student.entity';
import { AssignmentSubmission } from '@modules/assignments/entities/assignment-submission.entity';
import { SystemSetting, FeatureFlag, NotificationSettings, BulkOperation, SystemAlert, AdminReport } from './entities'; // ✨ NUEVO - P1/P2 (System Configuration + Bulk Ops + Alerts + Reports)
import { AdminUsersController } from './controllers/admin-users.controller';
import { AdminOrganizationsController } from './controllers/admin-organizations.controller';
import { AdminContentController } from './controllers/admin-content.controller';
import { AdminSystemController } from './controllers/admin-system.controller';
import { AdminDashboardController } from './controllers/admin-dashboard.controller';
import { AdminRolesController } from './controllers/admin-roles.controller';
import { AdminReportsController } from './controllers/admin-reports.controller';
import { AdminLogsController } from './controllers/admin-logs.controller';
import { ClassroomAssignmentsController } from './controllers/classroom-assignments.controller';
import { ClassroomTeachersRestController } from './controllers/classroom-teachers-rest.controller';
import { AdminGamificationConfigController } from './controllers/admin-gamification-config.controller';
import { AdminBulkOperationsController } from './controllers/admin-bulk-operations.controller';
import { AdminAlertsController } from './controllers/admin-alerts.controller';
import { AdminAnalyticsController } from './controllers/admin-analytics.controller';
import { AdminProgressController } from './controllers/admin-progress.controller';
import { AdminMonitoringController } from './controllers/admin-monitoring.controller';
import { AdminInterventionsController } from './controllers/admin-interventions.controller';
import { AdminAssignmentsController } from './controllers/admin-assignments.controller';
import { FeatureFlagsController } from './controllers/feature-flags.controller';
import { AdminUsersService } from './services/admin-users.service';
import { AdminOrganizationsService } from './services/admin-organizations.service';
import { AdminContentService } from './services/admin-content.service';
import { AdminSystemService } from './services/admin-system.service';
import { AdminDashboardService } from './services/admin-dashboard.service';
import { AdminRolesService } from './services/admin-roles.service';
import { AdminReportsService } from './services/admin-reports.service';
import { ClassroomAssignmentsService } from './services/classroom-assignments.service';
import { GamificationConfigService } from './services/gamification-config.service';
import { BulkOperationsService } from './services/bulk-operations.service';
import { AdminAlertsService } from './services/admin-alerts.service';
import { AdminAnalyticsService } from './services/admin-analytics.service';
import { AdminProgressService } from './services/admin-progress.service';
import { AdminMonitoringService } from './services/admin-monitoring.service';
import { AdminInterventionsService } from './services/admin-interventions.service';
import { AdminAssignmentsService } from './services/admin-assignments.service';
import { FeatureFlagsService } from './services/feature-flags.service';
import { AdminGuard } from './guards/admin.guard';
import { TasksModule } from '../tasks/tasks.module';

@Module({
  imports: [
    TypeOrmModule.forFeature([User, Profile, Role, UserRole, Tenant, Membership, AuthAttempt, UserSuspension, SystemSetting, FeatureFlag, NotificationSettings, BulkOperation, AdminReport], 'auth'),
    TypeOrmModule.forFeature([EducationalModule, Exercise, ContentApproval, Assignment, AssignmentStudent, AssignmentSubmission], 'educational'),
    TypeOrmModule.forFeature([ContentTemplate, MediaFile], 'content'),
    TypeOrmModule.forFeature([Classroom, TeacherClassroom, AssignmentClassroom], 'social'),
    TypeOrmModule.forFeature([SystemAlert], 'audit'),
    TypeOrmModule.forFeature([], 'progress'), // For AdminInterventionsService (student_intervention_alerts)
    TasksModule, // Import TasksModule to access MissionsCronService
  ],
  controllers: [
    AdminUsersController,
    AdminOrganizationsController,
    AdminContentController,
    AdminSystemController,
    AdminDashboardController,
    AdminRolesController,
    AdminReportsController,
    AdminLogsController,
    ClassroomAssignmentsController,
    ClassroomTeachersRestController, // NEW: REST endpoints for US-AE-007
    AdminGamificationConfigController,
    AdminBulkOperationsController,
    AdminAlertsController, // NEW: System alerts management
    AdminAnalyticsController, // NEW: Analytics endpoints (Plan 2)
    AdminProgressController, // NEW: Progress tracking endpoints (Plan 3)
    AdminMonitoringController, // NEW: Monitoring endpoints (Plan 4)
    AdminInterventionsController, // NEW: Student intervention alerts (BE-001)
    AdminAssignmentsController, // NEW: Assignments management endpoints (US-AE-009)
    FeatureFlagsController, // NEW: Feature flags management (BE-ADMIN-001-003)
  ],
  providers: [
    AdminUsersService,
    AdminOrganizationsService,
    AdminContentService,
    AdminSystemService,
    AdminDashboardService,
    AdminRolesService,
    AdminReportsService,
    ClassroomAssignmentsService,
    GamificationConfigService,
    BulkOperationsService,
    AdminAlertsService, // NEW: System alerts service
    AdminAnalyticsService, // NEW: Analytics service (Plan 2)
    AdminProgressService, // NEW: Progress tracking service (Plan 3)
    AdminMonitoringService, // NEW: Monitoring service (Plan 4)
    AdminInterventionsService, // NEW: Student intervention alerts service (BE-001)
    AdminAssignmentsService, // NEW: Assignments management service (US-AE-009)
    FeatureFlagsService, // NEW: Feature flags service (BE-ADMIN-001-003)
    AdminGuard,
  ],
  exports: [
    AdminUsersService,
    AdminOrganizationsService,
    AdminContentService,
    AdminSystemService,
    AdminDashboardService,
    AdminRolesService,
    AdminReportsService,
    ClassroomAssignmentsService,
    GamificationConfigService,
    BulkOperationsService,
    AdminAlertsService, // NEW: Export alerts service for use in other modules
    AdminAnalyticsService, // NEW: Export analytics service for use in other modules
    AdminProgressService, // NEW: Export progress service for use in other modules
    AdminMonitoringService, // NEW: Export monitoring service for use in other modules
    AdminInterventionsService, // NEW: Export interventions service for use in other modules (BE-001)
    AdminAssignmentsService, // NEW: Export assignments service for use in other modules (US-AE-009)
    FeatureFlagsService, // NEW: Export feature flags service for use in other modules (BE-ADMIN-001-003)
  ],
})
export class AdminModule {}
