import ical, { ICalCalendarMethod } from 'ical-generator'

// Build a PUBLISH .ics for the attendee's "add to calendar". METHOD:PUBLISH (not
// REQUEST) because dezky is not acting as the iMIP organizer — we send a branded
// confirmation with an attachable event, not a scheduling invitation. The UID
// matches the booking's calendarEventUid so a re-send updates rather than dupes.
export interface BookingIcsInput {
  uid: string
  start: Date
  end: Date
  summary: string
  description?: string
  location?: string
  organizerName: string
  organizerEmail: string
  attendeeName: string
  attendeeEmail: string
}

export function buildBookingIcs(p: BookingIcsInput): string {
  const cal = ical({
    prodId: { company: 'dezky', product: 'scheduling', language: 'EN' },
    method: ICalCalendarMethod.PUBLISH,
  })
  cal.createEvent({
    id: p.uid,
    start: p.start,
    end: p.end,
    summary: p.summary,
    description: p.description,
    location: p.location,
    organizer: { name: p.organizerName, email: p.organizerEmail },
    attendees: [{ name: p.attendeeName, email: p.attendeeEmail, rsvp: false }],
  })
  return cal.toString()
}
