import { Injectable, NotFoundException, ServiceUnavailableException } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Model, Types } from 'mongoose'
import { AvailabilitySchedule, AvailabilityScheduleDocument } from '../../schemas/availability-schedule.schema.js'
import { Booking, BookingDocument } from '../../schemas/booking.schema.js'
import { EventTypeDocument } from '../../schemas/event-type.schema.js'
import { HostDocument } from '../../schemas/scheduling-host.schema.js'
import { SlotLock, SlotLockDocument } from '../../schemas/slot-lock.schema.js'
import { CredentialProvisioner } from '../stalwart-calendar/credential-provisioner.service.js'
import { JmapCalendarGateway } from '../stalwart-calendar/jmap-calendar.gateway.js'
import { computeSlots, ComputedSlot } from './slot-computer.js'

@Injectable()
export class SlotService {
  constructor(
    @InjectModel(AvailabilitySchedule.name) private readonly scheduleModel: Model<AvailabilityScheduleDocument>,
    @InjectModel(Booking.name) private readonly bookingModel: Model<BookingDocument>,
    @InjectModel(SlotLock.name) private readonly lockModel: Model<SlotLockDocument>,
    private readonly provisioner: CredentialProvisioner,
    private readonly gateway: JmapCalendarGateway,
  ) {}

  // Free UTC slots for a host+event-type within [fromUtc, toUtc). Fails CLOSED on
  // calendar errors (§9): rather than show slots we can't verify against live
  // free/busy — risking a double-book — we surface 503 so the UI shows a retry.
  async availableSlots(
    host: HostDocument,
    eventType: EventTypeDocument,
    fromUtc: Date,
    toUtc: Date,
    now: Date = new Date(),
  ): Promise<ComputedSlot[]> {
    const schedule = await this.scheduleModel.findById(eventType.availabilityScheduleId).exec()
    if (!schedule) throw new NotFoundException('Event type has no availability schedule')

    const access = await this.provisioner.resolveAccess(host)

    let calendarBusy
    try {
      calendarBusy = await this.gateway.getBusyIntervals(access, fromUtc, toUtc)
    } catch {
      throw new ServiceUnavailableException('Calendar is temporarily unavailable — please retry.')
    }

    const [bookingBusy, lockBusy] = await Promise.all([
      this.confirmedBookingIntervals(host._id, fromUtc, toUtc),
      this.activeLockIntervals(host._id, fromUtc, toUtc, now),
    ])

    return computeSlots({
      durationMinutes: eventType.durationMinutes,
      slotIntervalMinutes: eventType.slotIntervalMinutes,
      bufferBeforeMinutes: eventType.bufferBeforeMinutes,
      bufferAfterMinutes: eventType.bufferAfterMinutes,
      minimumNoticeMinutes: eventType.minimumNoticeMinutes,
      maximumDaysInFuture: eventType.maximumDaysInFuture,
      scheduleTimezone: schedule.timezone,
      weeklyRules: schedule.weeklyRules,
      dateOverrides: schedule.dateOverrides,
      busy: [...calendarBusy, ...bookingBusy, ...lockBusy],
      now,
      fromUtc,
      toUtc,
    })
  }

  private async confirmedBookingIntervals(hostId: Types.ObjectId, from: Date, to: Date) {
    const bookings = await this.bookingModel
      .find({ hostId, status: { $in: ['confirmed', 'pending'] }, startUtc: { $lt: to }, endUtc: { $gt: from } })
      .select('startUtc endUtc')
      .exec()
    return bookings.map((b) => ({ startUtc: b.startUtc, endUtc: b.endUtc }))
  }

  private async activeLockIntervals(hostId: Types.ObjectId, from: Date, to: Date, now: Date) {
    const locks = await this.lockModel
      .find({
        hostId,
        startUtc: { $lt: to },
        endUtc: { $gt: from },
        $or: [{ expiresAt: null }, { expiresAt: { $gt: now } }],
      })
      .select('startUtc endUtc')
      .exec()
    return locks.map((l) => ({ startUtc: l.startUtc, endUtc: l.endUtc }))
  }
}
