'use strict'

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

class BenefitClub {
  static findAll(tenantId) {
    return query(
      `SELECT bc.*, COUNT(cp.id) AS plan_count
       FROM benefit_clubs bc
       LEFT JOIN club_plans cp ON cp.club_id = bc.id AND cp.active = TRUE
       WHERE bc.tenant_id = $1 AND bc.active = TRUE
       GROUP BY bc.id ORDER BY bc.id`,
      [tenantId]
    )
  }

  static findBySlug(slug, tenantId) {
    return queryOne(
      'SELECT * FROM benefit_clubs WHERE slug=$1 AND tenant_id=$2 AND active=TRUE',
      [slug, tenantId]
    )
  }

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

  // Planos de um clube
  static getPlans(clubId) {
    return query(
      'SELECT * FROM club_plans WHERE club_id=$1 AND active=TRUE ORDER BY price_monthly',
      [clubId]
    )
  }

  static getPlanById(planId) {
    return queryOne('SELECT * FROM club_plans WHERE id=$1', [planId])
  }
}

module.exports = BenefitClub
