import DS from 'ember-data'
import lookupValidator from 'ember-changeset-validations'
import Changeset from 'ember-changeset'
import ModelValidations from 'ember-panda/validations/organization'
validator = lookupValidator(ModelValidations)
import config from 'ember-get-config'

export default DS.Model.extend
  name:       DS.attr('string')
  identifier: DS.attr('string')
  logo:       DS.attr()
  changeset: (->
    new Changeset @, validator, ModelValidations
  ).property()

  #TODO: Move as general implementation to ember-cli-dunlop
  api_url: Ember.computed 'id', ->
    return unless id = @get('id')
    modelName = @get('constructor.modelName')
    adapter = @store.adapterFor(modelName)
    adapter.buildURL modelName, id
  action_url: (action) ->
    return unless api_url = @get('api_url')
    "#{api_url}/#{action}"

  logo_url: Ember.computed 'logo', ->
    urls = {}
    return urls unless logo = @get('logo')
    for key, relative_url of logo
      urls[key] = "#{config.apiHost}#{relative_url}"
    urls

  #projects:                DS.hasMany('project', inverse: 'organizations')
  manager_ids:             DS.attr(defaultValue: -> [])
  isEditing:               DS.attr('boolean', defaultValue: false)

  users:                   DS.hasMany('user', serialize: 'ids', inverse: 'organizations')
  tracker_subjects:        DS.hasMany('panda/tracker-subject')
  #workflow_instance_types: DS.hasMany("workflow-instance-type")
  workflow_action_definitions: DS.hasMany('panda/workflow-action-definition', inverse: 'organization')

