# 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

module CalculatorHelper
  def get_operation_color(operation)
    colors = {
      'addition' => 'success',
      'subtraction' => 'danger',
      'multiplication' => 'warning',
      'division' => 'info',
      'scientific' => 'primary',
      'currency' => 'success',
      'unit' => 'secondary',
      'memory' => 'dark',
      'graphing' => 'info',
      'equation' => 'primary',
      'programming' => 'warning',
      'financial' => 'success',
      'health' => 'danger',
      'statistics' => 'info',
      'matrix' => 'secondary'
    }
    colors[operation] || 'primary'
  end
  
  def format_number(number)
    if number.is_a?(Float)
      if number.finite?
        if number == number.to_i
          number.to_i.to_s
        else
          sprintf('%.4f', number).gsub(/\.?0+$/, '')
        end
      else
        number.to_s
      end
    else
      number.to_s
    end
  end
  
  def time_ago_in_words_short(time)
    diff = Time.current - time
    
    if diff < 60
      "#{diff.to_i}s ago"
    elsif diff < 3600
      "#{(diff / 60).to_i}m ago"
    elsif diff < 86400
      "#{(diff / 3600).to_i}h ago"
    else
      "#{(diff / 86400).to_i}d ago"
    end
  end
  
  def calculation_icon(operation)
    icons = {
      'addition' => 'fa-plus',
      'subtraction' => 'fa-minus',
      'multiplication' => 'fa-times',
      'division' => 'fa-divide',
      'scientific' => 'fa-square-root-alt',
      'currency' => 'fa-dollar-sign',
      'unit' => 'fa-exchange-alt',
      'memory' => 'fa-memory',
      'graphing' => 'fa-chart-line',
      'equation' => 'fa-equals',
      'programming' => 'fa-code',
      'financial' => 'fa-calculator',
      'health' => 'fa-heartbeat',
      'statistics' => 'fa-chart-bar',
      'matrix' => 'fa-table'
    }
    icons[operation] || 'fa-calculator'
  end
  
  def theme_color_class(theme_name)
    theme_classes = {
      'default' => 'bg-gradient-purple',
      'dark' => 'bg-gradient-dark',
      'ocean' => 'bg-gradient-ocean',
      'sunset' => 'bg-gradient-sunset',
      'forest' => 'bg-gradient-forest',
      'candy' => 'bg-gradient-candy'
    }
    theme_classes[theme_name] || 'bg-gradient-purple'
  end
  
  def favorite_category_icon(category)
    icons = {
      'mathematics' => 'fa-calculator',
      'physics' => 'fa-atom',
      'finance' => 'fa-dollar-sign',
      'engineering' => 'fa-cogs',
      'statistics' => 'fa-chart-bar',
      'conversion' => 'fa-exchange-alt',
      'health' => 'fa-heartbeat',
      'other' => 'fa-star'
    }
    icons[category] || 'fa-star'
  end
  
  def safe_eval(expression)
    # Safe mathematical expression evaluation
    # Only allow mathematical functions and operators
    allowed_chars = /^[0-9+\-*/().^sinco tan log sqrtabs pi e]+$/
    
    if expression.match?(allowed_chars)
      begin
        # Replace mathematical functions with Ruby equivalents
        processed = expression.downcase
          .gsub(/sin/, 'Math.sin')
          .gsub(/cos/, 'Math.cos')
          .gsub(/tan/, 'Math.tan')
          .gsub(/log/, 'Math.log10')
          .gsub(/sqrt/, 'Math.sqrt')
          .gsub(/abs/, 'Math.abs')
          .gsub(/pi/, 'Math::PI')
          .gsub(/e/, 'Math::E')
          .gsub(/\^/, '**')
        
        eval(processed)
      rescue => e
        "Error: #{e.message}"
      end
    else
      "Error: Invalid characters in expression"
    end
  end
  
  def calculation_statistics(history)
    return {} if history.empty?
    
    {
      total: history.count,
      by_operation: history.group(:operation).count,
      by_day: history.group_by_day(:created_at).count,
      average_result: history.average(:result)&.round(4),
      most_used_operation: history.group(:operation).count.max_by(&:last)&.first,
      recent_count: history.where('created_at > ?', 1.day.ago).count
    }
  end
  
  def export_calculations_csv(calculations)
    CSV.generate do |csv|
      csv << ['Expression', 'Result', 'Operation', 'Created At']
      calculations.each do |calc|
        csv << [calc.expression, calc.result, calc.operation, calc.created_at.strftime('%Y-%m-%d %H:%M:%S')]
      end
    end
  end
  
  def export_calculations_json(calculations)
    calculations.map do |calc|
      {
        expression: calc.expression,
        result: calc.result,
        operation: calc.operation,
        created_at: calc.created_at.iso8601
      }
    end.to_json
  end
end
