import { Module } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
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 { WebhookDelivery, WebhookDeliverySchema } from '../schemas/webhook-delivery.schema.js'
import { WebhookSubscription, WebhookSubscriptionSchema } from '../schemas/webhook-subscription.schema.js'
import { TenantsModule } from '../tenants/tenants.module.js'
import { ABUSE_GUARD, abuseGuardFactory } from './abuse/abuse-guard.js'
import { AvailabilityService } from './availability/availability.service.js'
import { BookingsService } from './bookings/bookings.service.js'
import { CalendarRetryWorker } from './bookings/calendar-retry.worker.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 { OverviewService } from './overview/overview.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'
import { WebhookDeliveryWorker } from './webhooks/webhook-delivery.worker.js'
import { WebhooksController } from './webhooks/webhooks.controller.js'
import { WebhooksService } from './webhooks/webhooks.service.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 },
      { name: WebhookSubscription.name, schema: WebhookSubscriptionSchema },
      { name: WebhookDelivery.name, schema: WebhookDeliverySchema },
    ]),
    // 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: [WebhooksController, SchedulingAdminController, PublicSchedulingController],
  providers: [
    HostsService,
    AvailabilityService,
    EventTypesService,
    SlotService,
    BookingsService,
    OverviewService,
    PublicSchedulingService,
    JmapMailer,
    BookingReminderWorker,
    CalendarRetryWorker,
    WebhooksService,
    WebhookDeliveryWorker,
    // Pluggable captcha guard for the public booking surface (Turnstile when
    // TURNSTILE_SECRET is set, otherwise a no-op).
    { provide: ABUSE_GUARD, useFactory: abuseGuardFactory, inject: [ConfigService] },
  ],
})
export class SchedulingModule {}
