import { ArrayNotEmpty, IsArray, IsBoolean, IsIn, IsOptional, IsUrl, MaxLength } from 'class-validator'
import { WEBHOOK_EVENTS, type WebhookEvent } from '../../../schemas/webhook-subscription.schema.js'

// Only allow https receivers (or http for local dev hostnames). Booking payloads
// carry attendee PII, so plain http to arbitrary hosts is rejected.
const URL_OPTS = { protocols: ['https', 'http'], require_protocol: true, require_tld: false }

export class CreateWebhookDto {
  @IsUrl(URL_OPTS, { message: 'url must be a valid http(s) URL' })
  @MaxLength(2048)
  url!: string

  @IsArray()
  @ArrayNotEmpty()
  @IsIn(WEBHOOK_EVENTS, { each: true, message: `events must be one of: ${WEBHOOK_EVENTS.join(', ')}` })
  events!: WebhookEvent[]
}

export class UpdateWebhookDto {
  @IsOptional()
  @IsUrl(URL_OPTS, { message: 'url must be a valid http(s) URL' })
  @MaxLength(2048)
  url?: string

  @IsOptional()
  @IsArray()
  @ArrayNotEmpty()
  @IsIn(WEBHOOK_EVENTS, { each: true, message: `events must be one of: ${WEBHOOK_EVENTS.join(', ')}` })
  events?: WebhookEvent[]

  @IsOptional()
  @IsBoolean()
  active?: boolean
}
