# frozen_string_literal: true
class Zendesk2::Category
  include Zendesk2::Model

  # @return [Integer] Automatically assigned during creation
  identity :id, type: :integer

  # @return [Time] The time the category was created
  attribute :created_at, type: :time
  # @return [String] The description of the category
  attribute :description, type: :string
  # @return [String] The name of the category
  attribute :name, type: :string
  # @return [Integer] The position of this category relative to other categories
  attribute :position, type: :integer
  # @return [Time] The time of the last update of the category
  attribute :updated_at, type: :time
  # @return [String] The API url of this category
  attribute :url, type: :string

  def destroy!
    requires :identity

    cistern.destroy_category('category' => { 'id' => identity })
  end

  def save!
    data = if new_record?
             requires :name

             cistern.create_category(params).body['category']
           else
             requires :identity

             cistern.update_category(params).body['category']
           end

    merge_attributes(data)
  end

  protected

  def params
    {
      'category' => Cistern::Hash.slice(
        Zendesk2.stringify_keys(attributes),
        *Zendesk2::CreateCategory.accepted_attributes
      ),
    }
  end
end
