'use strict'

const { execute, queryOne, query } = require('../database/postgres')

class ClubPartner {
  static findByClub(clubId) {
    return query(
      `SELECT * FROM club_partners WHERE club_id=$1 AND active=TRUE ORDER BY name`,
      [clubId]
    )
  }

  static findById(id) {
    return queryOne('SELECT * FROM club_partners WHERE id=$1', [id])
  }

  static create({ club_id, tenant_id, name, specialty, phone, address, discount_percent, notes }) {
    return execute(
      `INSERT INTO club_partners
         (club_id, tenant_id, name, specialty, phone, address, discount_percent, notes)
       VALUES ($1,$2,$3,$4,$5,$6,$7,$8) RETURNING id`,
      [club_id, tenant_id, name, specialty || null, phone || null,
       address || null, discount_percent || 0, notes || null]
    )
  }

  static update(id, { name, specialty, phone, address, discount_percent, notes, active }) {
    return execute(
      `UPDATE club_partners
       SET name=$1, specialty=$2, phone=$3, address=$4,
           discount_percent=$5, notes=$6, active=$7
       WHERE id=$8`,
      [name, specialty || null, phone || null, address || null,
       discount_percent ?? 0, notes || null, active !== false, id]
    )
  }

  static delete(id) {
    return execute('UPDATE club_partners SET active=FALSE WHERE id=$1', [id])
  }
}

module.exports = ClubPartner
