module.exports = bookshelf.Model.extend({
  tableName: 'skills',

  users: function () {
    return this.belongsToMany(User, 'skills_users')
  }

}, {

  find: function (nameOrId, opts = {}) {
    if (!nameOrId) return Promise.resolve(null)

    const where = isNaN(Number(nameOrId))
      ? {name: nameOrId}
      : {id: nameOrId}

    return this.where(where).fetch(opts)
  }
})

module.exports = bookshelf.Model.extend({
  tableName: 'skills',

  users: function () {
    return this.belongsToMany(User, 'skills_users')
  }

}, {

  find: function (nameOrId, opts = {}) {
    if (!nameOrId) return Promise.resolve(null)

    if (isNaN(Number(nameOrId))) {
      return Skill.query(qb => qb.whereRaw('lower(name) = lower(?)', nameOrId))
      .fetch(opts)
    }
    return Tag.where({id: nameOrId}).fetch(opts)
  }
})

make Skill.find case insensitive