# frozen_string_literal: true

class User < ApplicationRecord
  after_commit(on: :create) do
    OverallScore.create!(user: self)
    Tournament.find_each do |tourney|
      Team.find_or_create_by(user: self, tournament: tourney)
    end
  end
  has_one :overall_score, dependent: :destroy
  has_many :teams, dependent: :destroy
  has_many :user_scoreboards, dependent: :destroy
  has_many :team_players, through: :teams
  has_many :team_scorecards, through: :teams
  has_many :players, through: :team_players
  has_many :comments, dependent: :delete_all
  scope :paid, -> { where(has_paid: true) }
  scope :not_paid, -> { where(has_paid: false) }
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :lockable
end
