# Ruby Calculator - Interactive calculator built with Ruby on Rails
# Author: RSK World (Molla Samser, Founder | Rima Khatun, Designer & Tester)
# Contact: help@rskworld.in | +91 93305 39277
# Website: https://rskworld.in
# Year: 2026
# Description: Build a fully functional calculator using Ruby on Rails with modern UI

class FinancialCalculator
  def self.calculate_loan_payment(principal, annual_rate, years)
    monthly_rate = annual_rate / 100 / 12
    months = years * 12
    
    if monthly_rate == 0
      monthly_payment = principal / months
      total_payment = monthly_payment * months
      total_interest = 0
    else
      monthly_payment = principal * (monthly_rate * (1 + monthly_rate)**months) / ((1 + monthly_rate)**months - 1)
      total_payment = monthly_payment * months
      total_interest = total_payment - principal
    end
    
    {
      monthly_payment: monthly_payment.round(2),
      total_payment: total_payment.round(2),
      total_interest: total_interest.round(2),
      principal: principal,
      annual_rate: annual_rate,
      years: years
    }
  end
  
  def self.generate_amortization_schedule(principal, annual_rate, years)
    monthly_rate = annual_rate / 100 / 12
    months = years * 12
    monthly_payment = calculate_loan_payment(principal, annual_rate, years)[:monthly_payment]
    
    schedule = []
    balance = principal
    
    (1..months).each do |month|
      interest_payment = balance * monthly_rate
      principal_payment = monthly_payment - interest_payment
      balance -= principal_payment
      
      schedule << {
        month: month,
        payment: monthly_payment.round(2),
        principal_payment: principal_payment.round(2),
        interest_payment: interest_payment.round(2),
        balance: [balance.round(2), 0].max
      }
    end
    
    schedule
  end
  
  def self.calculate_compound_interest(principal, annual_rate, years, compound_frequency = 12)
    r = annual_rate / 100
    n = compound_frequency
    t = years
    
    amount = principal * (1 + r/n)**(n*t)
    interest = amount - principal
    
    {
      principal: principal,
      final_amount: amount.round(2),
      total_interest: interest.round(2),
      annual_rate: annual_rate,
      years: years,
      compound_frequency: compound_frequency
    }
  end
  
  def self.calculate_investment_return(initial_investment, final_value, years)
    total_return = final_value - initial_investment
    total_return_percentage = (total_return / initial_investment) * 100
    annual_return = (final_value / initial_investment)**(1/years.to_f) - 1
    annual_return_percentage = annual_return * 100
    
    {
      initial_investment: initial_investment,
      final_value: final_value,
      total_return: total_return.round(2),
      total_return_percentage: total_return_percentage.round(2),
      annual_return: annual_return.round(6),
      annual_return_percentage: annual_return_percentage.round(2),
      years: years
    }
  end
  
  def self.calculate_retirement_savings(current_age, retirement_age, current_savings, monthly_contribution, annual_return)
    years_to_retirement = retirement_age - current_age
    monthly_rate = annual_return / 100 / 12
    months = years_to_retirement * 12
    
    # Future value of current savings
    future_current_savings = current_savings * (1 + monthly_rate)**months
    
    # Future value of monthly contributions
    if monthly_rate == 0
      future_contributions = monthly_contribution * months
    else
      future_contributions = monthly_contribution * ((1 + monthly_rate)**months - 1) / monthly_rate
    end
    
    total_retirement_savings = future_current_savings + future_contributions
    
    {
      current_age: current_age,
      retirement_age: retirement_age,
      years_to_retirement: years_to_retirement,
      current_savings: current_savings,
      monthly_contribution: monthly_contribution,
      annual_return: annual_return,
      future_current_savings: future_current_savings.round(2),
      future_contributions: future_contributions.round(2),
      total_retirement_savings: total_retirement_savings.round(2)
    }
  end
  
  def self.calculate_mortgage_affordability(annual_income, down_payment_percentage, annual_rate, years, other_debts = 0)
    # 28% rule for housing expenses
    max_housing_payment = (annual_income * 0.28 - other_debts) / 12
    
    # 36% rule for total debt
    max_total_payment = (annual_income * 0.36) / 12
    max_mortgage_payment = max_total_payment - other_debts
    
    # Use the more conservative estimate
    monthly_payment = [max_housing_payment, max_mortgage_payment].min
    
    monthly_rate = annual_rate / 100 / 12
    months = years * 12
    
    if monthly_rate == 0
      max_loan = monthly_payment * months
    else
      max_loan = monthly_payment * ((1 + monthly_rate)**months - 1) / (monthly_rate * (1 + monthly_rate)**months)
    end
    
    down_payment = max_loan * (down_payment_percentage / (100 - down_payment_percentage))
    max_home_price = max_loan + down_payment
    
    {
      annual_income: annual_income,
      max_monthly_payment: monthly_payment.round(2),
      max_loan_amount: max_loan.round(2),
      required_down_payment: down_payment.round(2),
      max_home_price: max_home_price.round(2),
      down_payment_percentage: down_payment_percentage
    }
  end
  
  def self.calculate_credit_card_payment(balance, annual_rate, monthly_payment)
    monthly_rate = annual_rate / 100 / 12
    
    if monthly_rate == 0
      months_to_pay_off = (balance / monthly_payment).ceil
      total_paid = monthly_payment * months_to_pay_off
      total_interest = 0
    else
      if monthly_payment <= balance * monthly_rate
        return {
          error: 'Monthly payment too low. You will never pay off the balance.',
          minimum_payment: (balance * monthly_rate * 1.01).round(2)
        }
      end
      
      months_to_pay_off = (-Math.log(1 - (balance * monthly_rate) / monthly_payment)) / Math.log(1 + monthly_rate)
      total_paid = monthly_payment * months_to_pay_off.ceil
      total_interest = total_paid - balance
    end
    
    {
      balance: balance,
      annual_rate: annual_rate,
      monthly_payment: monthly_payment,
      months_to_pay_off: months_to_pay_off.ceil,
      total_paid: total_paid.round(2),
      total_interest: total_interest.round(2)
    }
  end
  
  def self.calculate_tip(bill_amount, tip_percentage, number_of_people = 1)
    tip_amount = bill_amount * (tip_percentage / 100)
    total_amount = bill_amount + tip_amount
    amount_per_person = total_amount / number_of_people
    
    {
      bill_amount: bill_amount,
      tip_percentage: tip_percentage,
      tip_amount: tip_amount.round(2),
      total_amount: total_amount.round(2),
      number_of_people: number_of_people,
      amount_per_person: amount_per_person.round(2)
    }
  end
  
  def self.calculate_discount(original_price, discount_percentage)
    discount_amount = original_price * (discount_percentage / 100)
    final_price = original_price - discount_amount
    savings = discount_amount
    
    {
      original_price: original_price,
      discount_percentage: discount_percentage,
      discount_amount: discount_amount.round(2),
      final_price: final_price.round(2),
      savings: savings.round(2)
    }
  end
  
  def self.calculate_sales_tax(price, tax_percentage)
    tax_amount = price * (tax_percentage / 100)
    total_price = price + tax_amount
    
    {
      price: price,
      tax_percentage: tax_percentage,
      tax_amount: tax_amount.round(2),
      total_price: total_price.round(2)
    }
  end
end
