class Api::V1::DispatchSerializer < ActiveModel::Serializer
  attributes :id, :name, :scheduled_date, :completion_date, :location_name, :priority, :status, :dispatched, :client_name, 
             :location_address, :wo_type, :extended_status_code, :area_name, :wo_assigned, :can_edit, :can_dispatch, 
             :can_modify_wo
  has_many :tradesmens, serializer: Api::V1::TradesmenSerializer

  def scheduled_date
    object.scheduled_date
  end

  def location_name
    object.location.name
  end

  def status
    object.extended_status
  end

  def dispatched
    object.dispatched?
  end

  def client_name
    object.company.try(:name)
  end

  def location_address
    object.location.full_address
  end

  def area_name
    object.area.try(:name)
  end

  def wo_type
    object.work_order_type.try(:titleize)
  end

  def extended_status_code
    object.extended_status_code
  end
 
  def wo_assigned
    object.technician_trips_count > 0
  end

  def can_edit
    object.vendor.is_equipment_sp?
  end

  def can_modify_wo
    Company.current&.vendor? and object.vendor.is_equipment_sp?
  end

  def can_dispatch
    Company.current&.vendor? and object.vendor.is_equipment_sp?
  end

end
