import { Module } from '@nestjs/common'
import { MongooseModule } from '@nestjs/mongoose'
import { ScheduleModule } from '@nestjs/schedule'
import { ThrottlerModule } from '@nestjs/throttler'
import { AuthModule } from '../auth/auth.module.js'
import { IntegrationsModule } from '../integrations/integrations.module.js'
import { AvailabilitySchedule, AvailabilityScheduleSchema } from '../schemas/availability-schedule.schema.js'
import { Booking, BookingSchema } from '../schemas/booking.schema.js'
import { EventType, EventTypeSchema } from '../schemas/event-type.schema.js'
import { Host, HostSchema } from '../schemas/scheduling-host.schema.js'
import { SlotLock, SlotLockSchema } from '../schemas/slot-lock.schema.js'
import { Tenant, TenantSchema } from '../schemas/tenant.schema.js'
import { User, UserSchema } from '../schemas/user.schema.js'
import { TenantsModule } from '../tenants/tenants.module.js'
import { AvailabilityService } from './availability/availability.service.js'
import { BookingsService } from './bookings/bookings.service.js'
import { JmapMailer } from './email/jmap-mailer.service.js'
import { EventTypesService } from './event-types/event-types.service.js'
import { HostsService } from './hosts/hosts.service.js'
import { PublicSchedulingController } from './public/public-scheduling.controller.js'
import { PublicSchedulingService } from './public/public-scheduling.service.js'
import { BookingReminderWorker } from './reminders/booking-reminder.worker.js'
import { SchedulingAdminController } from './scheduling-admin.controller.js'
import { SlotService } from './slots/slot.service.js'
import { StalwartCalendarModule } from './stalwart-calendar/stalwart-calendar.module.js'

// dezky Scheduling — Calendly-style booking on top of Stalwart calendars. Public
// pages (booking.dezky.eu) hit the unauthenticated /api/v1/public routes; host
// config sits behind the workspace-portal OIDC. The Stalwart integration is
// isolated in StalwartCalendarModule so it can be extracted later.
@Module({
  imports: [
    MongooseModule.forFeature([
      { name: Host.name, schema: HostSchema },
      { name: AvailabilitySchedule.name, schema: AvailabilityScheduleSchema },
      { name: EventType.name, schema: EventTypeSchema },
      { name: Booking.name, schema: BookingSchema },
      { name: SlotLock.name, schema: SlotLockSchema },
      { name: Tenant.name, schema: TenantSchema },
      { name: User.name, schema: UserSchema },
    ]),
    // Drives the @Cron booking reminder worker.
    ScheduleModule.forRoot(),
    // Per-IP rate limiting for the public booking endpoints (default read limit;
    // write endpoints tighten it via @Throttle).
    ThrottlerModule.forRoot({ throttlers: [{ name: 'default', ttl: 60_000, limit: 60 }] }),
    AuthModule,
    TenantsModule,
    IntegrationsModule, // StalwartClient — host→account lookup during onboarding
    StalwartCalendarModule,
  ],
  controllers: [SchedulingAdminController, PublicSchedulingController],
  providers: [
    HostsService,
    AvailabilityService,
    EventTypesService,
    SlotService,
    BookingsService,
    PublicSchedulingService,
    JmapMailer,
    BookingReminderWorker,
  ],
})
export class SchedulingModule {}
