# frozen_string_literal: true

# == Schema Information
#
# Table name: teams
#
#  id           :bigint           not null, primary key
#  description  :text(16777215)
#  finance_code :string(191)
#  name         :string(191)
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#

class Team < ApplicationRecord
  FINANCE_CODES = %w[BRL CATA1 CATA2 FELL POLICY STRATEGY POWER CS_PROG].freeze
  has_many :users
  has_many :research_sessions, through: :users
  has_many :rewards
  has_one :budget
  has_many :debits, through: :budget
  has_many :credits, through: :budget
  validates :finance_code, inclusion: { in: FINANCE_CODES }
  default_scope { includes(:rewards) } # this could get dicey.

  after_create :make_budget
  def self.finance_code_hash
    FINANCE_CODES.each_with_object({}) { |el, acc| acc[el] = el; }
  end

  def rewards_total(since = Time.zone.today.beginning_of_year - 1.day)
    raise ArgumentError if since.class != Date

    total = rewards.where('created_at > ?', since).sum(:amount_cents)
    Money.new(total, 'USD')
  end

  def reward_by_finance_code(finance_code)
    total = rewards.where(finance_code: finance_code).sum(:amount_cents)
    Money.new(total, 'USD')
  end

  def available_budget
    budget.amount
  end

  delegate :transactions, to: :budget

  private

  def make_budget
    Budget.create(team_id: id)
  end
end
