# 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 CalculatorController < ApplicationController
  before_action :set_memory
  
  def index
    @display = "0"
    @previous = nil
    @operation = nil
    @history = CalculationHistory.order(created_at: :desc).limit(10)
  end
  
  def calculate
    operation = params[:operation]
    first_number = params[:first_number].to_f
    second_number = params[:second_number].to_f
    
    result = case operation
             when 'add'
               first_number + second_number
             when 'subtract'
               first_number - second_number
             when 'multiply'
               first_number * second_number
             when 'divide'
               second_number != 0 ? first_number / second_number : "Error"
             when 'power'
               first_number ** second_number
             when 'sqrt'
               Math.sqrt(first_number.abs)
             when 'sin'
               Math.sin(first_number * Math::PI / 180)
             when 'cos'
               Math.cos(first_number * Math::PI / 180)
             when 'tan'
               Math.tan(first_number * Math::PI / 180)
             when 'log'
               Math.log10(first_number)
             when 'ln'
               Math.log(first_number)
             when 'factorial'
               factorial(first_number.to_i)
             else
               "Invalid operation"
             end
    
    # Save to history
    CalculationHistory.create!(
      expression: "#{first_number} #{get_operation_symbol(operation)} #{second_number if operation != 'sqrt' && operation != 'sin' && operation != 'cos' && operation != 'tan' && operation != 'log' && operation != 'ln' && operation != 'factorial'}",
      result: result,
      operation: operation
    )
    
    respond_to do |format|
      format.html { redirect_to root_path, notice: "Calculation complete!" }
      format.json { render json: { result: result } }
    end
  end
  
  def memory
    action = params[:memory_action]
    value = params[:value].to_f
    
    case action
    when 'clear'
      session[:memory] = 0
    when 'add'
      session[:memory] = (session[:memory] || 0) + value
    when 'subtract'
      session[:memory] = (session[:memory] || 0) - value
    when 'recall'
      @memory_value = session[:memory] || 0
    end
    
    respond_to do |format|
      format.html { redirect_to root_path }
      format.json { render json: { memory: session[:memory] || 0 } }
    end
  end
  
  def history
    @history = CalculationHistory.order(created_at: :desc).limit(50)
    
    respond_to do |format|
      format.html
      format.json { render json: @history }
    end
  end
  
  private
  
  def set_memory
    @memory = session[:memory] || 0
  end
  
  def get_operation_symbol(operation)
    case operation
    when 'add' then '+'
    when 'subtract' then '-'
    when 'multiply' then '×'
    when 'divide' then '÷'
    when 'power' then '^'
    when 'sqrt' then '√'
    when 'sin' then 'sin'
    when 'cos' then 'cos'
    when 'tan' then 'tan'
    when 'log' then 'log'
    when 'ln' then 'ln'
    when 'factorial' then '!'
    else operation
    end
  end
  
  def factorial(n)
    return 1 if n <= 1
    (1..n).reduce(:*)
  end
end
