import { Module, OnModuleInit } from '@nestjs/common';
import { StudentsModule } from '../students/students.module';
import { ClassesModule } from '../classes/classes.module';
import { AttendanceModule } from '../attendance/attendance.module';
import { RoomsModule } from '../rooms/rooms.module';
import { BillsModule } from '../bills/bills.module';
import { DashboardModule } from '../dashboard/dashboard.module';
import { AgentToolRegistry } from './agent-tool.registry';
import { AgentToolExecutor } from './agent-tool.executor';
import { SearchStudentsTool } from './tools/search-students.tool';
import { GetStudentBasicTool } from './tools/get-student-basic.tool';
import { AgentBusinessScopeFactory } from './agent-business-scope.factory';
import { SearchClassesTool } from './tools/search-classes.tool';
import { GetAttendanceSummaryTool } from './tools/get-attendance-summary.tool';
import { SearchRoomsTool } from './tools/search-rooms.tool';
import { GetRoomOccupancySummaryTool } from './tools/get-room-occupancy-summary.tool';
import { SearchBillsTool } from './tools/search-bills.tool';
import { GetDashboardStatsTool } from './tools/get-dashboard-stats.tool';
import { CreateStudentTool } from './tools/create-student.tool';
import { UpdateStudentsTool } from './tools/update-students.tool';
import { SearchExamsTool } from './tools/search-exams.tool';
import { SearchSchedulesTool } from './tools/search-schedules.tool';
import { SearchDepositsTool } from './tools/search-deposits.tool';
import { SearchExpensesTool } from './tools/search-expenses.tool';
import { SearchClassroomsTool } from './tools/search-classrooms.tool';
import { SearchClassroomRentalsTool } from './tools/search-classroom-rentals.tool';
import { GetSyncStatusTool } from './tools/get-sync-status.tool';
import { ExamsModule } from '../exams/exams.module';
import { SchedulesModule } from '../schedules/schedules.module';
import { DepositsModule } from '../deposits/deposits.module';
import { ExpensesModule } from '../expenses/expenses.module';
import { ClassroomsModule } from '../classrooms/classrooms.module';
import { ClassroomRentalsModule } from '../classroom-rentals/classroom-rentals.module';
import { SyncModule } from '../sync/sync.module';
import { BusinessContextService } from '../agent-context/business-context.service';
import { PendingTasksService } from '../agent-context/pending-tasks.service';
import { GetBusinessContextTool, GetEntitySchemaTool } from '../agent-context/get-business-context.tool';
import { GetPendingTasksTool } from '../agent-context/get-pending-tasks.tool';

/**
 * Agent Tools feature module.
 *
 * Provides a provider-neutral tool executor for LLM agent frameworks.
 * SDK consumers interact ONLY with {@link AgentToolExecutor}.
 *
 * `AgentToolRegistry` is an internal provider — it is NOT exported from
 * this module. All tool listing and execution goes through the executor,
 * which enforces double-check authorization, audit logging, and context
 * trust validation.
 *
 * Imports `StudentsModule` for student data access and relies on the
 * globally available `AuthorizationModule` and `OperationLogsModule`.
 */
@Module({
  imports: [
    StudentsModule,
    ClassesModule,
    AttendanceModule,
    RoomsModule,
    BillsModule,
    DashboardModule,
    ExamsModule,
    SchedulesModule,
    DepositsModule,
    ExpensesModule,
    ClassroomsModule,
    ClassroomRentalsModule,
    SyncModule,
  ],
  providers: [
    AgentToolRegistry,
    AgentToolExecutor,
    BusinessContextService,
    PendingTasksService,
    GetBusinessContextTool,
    GetEntitySchemaTool,
    GetPendingTasksTool,
    SearchStudentsTool,
    GetStudentBasicTool,
    AgentBusinessScopeFactory,
    SearchClassesTool,
    GetAttendanceSummaryTool,
    SearchRoomsTool,
    GetRoomOccupancySummaryTool,
    SearchBillsTool,
    GetDashboardStatsTool,
    CreateStudentTool,
    UpdateStudentsTool,
    SearchExamsTool,
    SearchSchedulesTool,
    SearchDepositsTool,
    SearchExpensesTool,
    SearchClassroomsTool,
    SearchClassroomRentalsTool,
    GetSyncStatusTool,
  ],
  exports: [AgentToolExecutor],
})
export class AgentToolsModule implements OnModuleInit {
  constructor(
    private readonly registry: AgentToolRegistry,
    private readonly searchTool: SearchStudentsTool,
    private readonly getTool: GetStudentBasicTool,
    private readonly searchClassesTool: SearchClassesTool,
    private readonly attendanceSummaryTool: GetAttendanceSummaryTool,
    private readonly searchRoomsTool: SearchRoomsTool,
    private readonly roomOccupancyTool: GetRoomOccupancySummaryTool,
    private readonly searchBillsTool: SearchBillsTool,
    private readonly dashboardStatsTool: GetDashboardStatsTool,
    private readonly createStudentTool: CreateStudentTool,
    private readonly updateStudentsTool: UpdateStudentsTool,
    private readonly searchExamsTool: SearchExamsTool,
    private readonly searchSchedulesTool: SearchSchedulesTool,
    private readonly searchDepositsTool: SearchDepositsTool,
    private readonly searchExpensesTool: SearchExpensesTool,
    private readonly searchClassroomsTool: SearchClassroomsTool,
    private readonly searchClassroomRentalsTool: SearchClassroomRentalsTool,
    private readonly getSyncStatusTool: GetSyncStatusTool,
    private readonly businessContextTool: GetBusinessContextTool,
    private readonly entitySchemaTool: GetEntitySchemaTool,
    private readonly pendingTasksTool: GetPendingTasksTool,
  ) {}

  onModuleInit(): void {
    this.registry.register(this.businessContextTool);
    this.registry.register(this.entitySchemaTool);
    this.registry.register(this.pendingTasksTool);
    this.registry.register(this.searchTool);
    this.registry.register(this.getTool);
    this.registry.register(this.searchClassesTool);
    this.registry.register(this.attendanceSummaryTool);
    this.registry.register(this.searchRoomsTool);
    this.registry.register(this.roomOccupancyTool);
    this.registry.register(this.searchBillsTool);
    this.registry.register(this.dashboardStatsTool);
    this.registry.register(this.createStudentTool);
    this.registry.register(this.updateStudentsTool);
    this.registry.register(this.searchExamsTool);
    this.registry.register(this.searchSchedulesTool);
    this.registry.register(this.searchDepositsTool);
    this.registry.register(this.searchExpensesTool);
    this.registry.register(this.searchClassroomsTool);
    this.registry.register(this.searchClassroomRentalsTool);
    this.registry.register(this.getSyncStatusTool);
  }
}
