# frozen_string_literal: true

module Nylas
  # Structure to represent a the Event Schema.
  # @see https://docs.nylas.com/reference#events
  class Event
    include Model
    self.resources_path = "/events"
    allows_operations(creatable: true, listable: true, filterable: true, showable: true, updatable: true,
                      destroyable: true)

    attribute :id, :string
    attribute :object, :string
    attribute :account_id, :string
    attribute :calendar_id, :string
    attribute :master_event_id, :string
    attribute :message_id, :string

    attribute :busy, :boolean
    attribute :description, :string
    attribute :location, :string
    attribute :owner, :string
    attribute :recurrence, :recurrence
    has_n_of_attribute :participants, :participant
    attribute :read_only, :boolean
    attribute :status, :string
    attribute :title, :string
    attribute :when, :when
    attribute :original_start_time, :unix_timestamp

    def busy?
      busy
    end

    def read_only?
      read_only
    end

    def rsvp(status, notify_participants:)
      rsvp = Rsvp.new(api: api, status: status, notify_participants: notify_participants,
                      event_id: id, account_id: account_id)
      rsvp.save
    end
  end
end

# frozen_string_literal: true

module Nylas
  # Structure to represent a the Event Schema.
  # @see https://docs.nylas.com/reference#events
  class Event
    include Model
    self.resources_path = "/events"
    allows_operations(creatable: true, listable: true, filterable: true, showable: true, updatable: true,
                      destroyable: true)

    attribute :id, :string
    attribute :object, :string
    attribute :account_id, :string
    attribute :calendar_id, :string
    attribute :master_event_id, :string
    attribute :message_id, :string
    attribute :ical_uid, :string

    attribute :busy, :boolean
    attribute :description, :string
    attribute :location, :string
    attribute :owner, :string
    attribute :recurrence, :recurrence
    has_n_of_attribute :participants, :participant
    attribute :read_only, :boolean
    attribute :status, :string
    attribute :title, :string
    attribute :when, :when
    attribute :original_start_time, :unix_timestamp

    def busy?
      busy
    end

    def read_only?
      read_only
    end

    def rsvp(status, notify_participants:)
      rsvp = Rsvp.new(api: api, status: status, notify_participants: notify_participants,
                      event_id: id, account_id: account_id)
      rsvp.save
    end
  end
end

Add support for iCal UID field
Add support for iCal UID field

While this SDK is supposed to not blow up on new field attributes, that is
not currently true for Event creation. Without this patch, the following
code (directly from our example code!) blows up with the stack trace below if
the API is returning an `ical_uid` field:

created_event = api.events.create(
  title: "A fun event!",
  location: "The Party Zone",
  calendar_id: calendar.id,
  # when: { date: "2020-01-10" })
  when: { start_time: Time.now + 60, end_time: Time.now + 120 })

---

bundler: failed to load command: ./reproduce_bug.rb (./reproduce_bug.rb)
Nylas::Registry::MissingKeyError: key ical_uid not in [:id, :object, :account_id, :calendar_id, :master_event_id, :message_id, :busy, :description, :location, :owner, :recurrence, :participants, :read_only, :status, :title, :when, :original_start_time]
  /home/spang/src/nylas/nylas-ruby/lib/nylas/registry.rb:30:in `rescue in []'
  /home/spang/src/nylas/nylas-ruby/lib/nylas/registry.rb:27:in `[]'
  /home/spang/src/nylas/nylas-ruby/lib/nylas/model/attributes.rb:48:in `cast'
  /home/spang/src/nylas/nylas-ruby/lib/nylas/model/attributes.rb:19:in `[]='
  /home/spang/src/nylas/nylas-ruby/lib/nylas/model/attributes.rb:25:in `block in merge'
  /home/spang/src/nylas/nylas-ruby/lib/nylas/model/attributes.rb:24:in `each'
  /home/spang/src/nylas/nylas-ruby/lib/nylas/model/attributes.rb:24:in `merge'
  /home/spang/src/nylas/nylas-ruby/lib/nylas/model.rb:35:in `save'
  /home/spang/src/nylas/nylas-ruby/lib/nylas/collection.rb:26:in `create'
  /home/spang/src/nylas/nylas-ruby/reproduce_clio_error.rb:40:in `<top (required)>'
