import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { HydratedDocument, Types } from 'mongoose'

export type EventTypeDocument = HydratedDocument<EventType>

export type LocationType = 'jitsi' | 'phone' | 'in_person' | 'custom'

// A bookable appointment type for a host (e.g. "30-min consultation"). Carries
// the booking rules slot computation needs: duration, granularity, buffers,
// notice window, and horizon.
@Schema({ collection: 'scheduling_event_types', timestamps: true })
export class EventType {
  @Prop({ type: Types.ObjectId, ref: 'Tenant', required: true, index: true })
  tenantId!: Types.ObjectId

  @Prop({ type: Types.ObjectId, ref: 'Host', required: true, index: true })
  hostId!: Types.ObjectId

  @Prop({ required: true, lowercase: true, trim: true })
  slug!: string

  @Prop({ required: true, trim: true })
  title!: string

  @Prop({ trim: true })
  description?: string

  @Prop({ required: true, min: 1 })
  durationMinutes!: number

  // Granularity of offered start times (e.g. 15 => :00/:15/:30/:45).
  @Prop({ required: true, min: 1, default: 15 })
  slotIntervalMinutes!: number

  @Prop({ min: 0, default: 0 })
  bufferBeforeMinutes!: number

  @Prop({ min: 0, default: 0 })
  bufferAfterMinutes!: number

  // Earliest bookable time relative to now.
  @Prop({ min: 0, default: 0 })
  minimumNoticeMinutes!: number

  // Booking horizon — how far ahead slots are offered.
  @Prop({ min: 1, default: 60 })
  maximumDaysInFuture!: number

  @Prop({ type: Types.ObjectId, ref: 'AvailabilitySchedule', required: true })
  availabilityScheduleId!: Types.ObjectId

  @Prop({ enum: ['jitsi', 'phone', 'in_person', 'custom'], default: 'jitsi' })
  locationType!: LocationType

  // Free text for phone / in_person / custom (e.g. a phone number or address).
  @Prop({ trim: true })
  locationDetails?: string

  @Prop({ trim: true })
  color?: string

  @Prop({ default: true, index: true })
  isActive!: boolean
}

export const EventTypeSchema = SchemaFactory.createForClass(EventType)
EventTypeSchema.index({ tenantId: 1, hostId: 1, slug: 1 }, { unique: true })
