attr = DS.attr
App.List = DS.Model.extend
  orders: DS.hasMany('order')
  needs_help: attr('boolean')
  needs_payment: attr('boolean')
  user_requests_closing: attr('boolean')
  state: attr('string')
  price: attr('number')
  closed_at: DS.attr('date')
  extended_version: attr('boolean')
  supplier_orders_in_process_count: attr('number')
  supplier_orders_placed_count: attr('number')
  cached_supplier_name: attr('string')

  supplier: DS.belongsTo('supplier')
  table: DS.belongsTo('table')
  join_requests: DS.hasMany('join_request')
  users: DS.hasMany('user')
  supplier_name: (-> @get('cached_supplier_name') or @get('supplier.name')).property('cached_supplier_name', 'supplier.name')

  is_extended_version: ->
    @get('extended_version')
  relevant_orders: (-> @get('orders').filter((o)->o.get('state') isnt 'cancelled')).property('orders.@each.state')
  sorted_orders: (-> @get('relevant_orders').sortBy('created_at').reverseObjects()).property('relevant_orders.@each.isLoaded')
  total: (->
    @get('relevant_orders').getEach('total').reduce(((sum, total) -> sum + total), 0)
  ).property('relevant_orders.@each.total')
  active: (-> @get('state') is 'active' ).property('state')
