class TechnologyPayment < ApplicationRecord
  include Base::TechnologyPayment

  belongs_to :school, foreign_key: :SchoolID
  belongs_to :school_year, foreign_key: :SchoolYearID
  belongs_to :family, foreign_key: :FamilyID
  belongs_to :student, foreign_key: :StudentID

  # Read-only model - no validations for creation/updates
  # This model is used only for displaying existing technology payment data

  scope :by_school, ->(school_id) { where(SchoolID: school_id) }
  scope :by_school_year, ->(school_year_id) { where(SchoolYearID: school_year_id) }
  scope :by_family, ->(family_id) { where(FamilyID: family_id) }
  scope :by_student, ->(student_id) { where(StudentID: student_id) }
  scope :recent, -> { order(CreatedAt: :desc) }

  def self.default_amount
    50.00
  end

  def formatted_amount
    ActionController::Base.helpers.number_to_currency(amount || self.class.default_amount)
  end

  def formatted_date
    return "Outstanding" if created_at.nil?
    created_at.strftime("%m/%d/%y")
  end

  def student_name
    return "N/A" unless student
    "#{student.last_name}, #{student.first_name}".strip
  end

  def family_name
    return "N/A" unless family
    family.name
  end
end
