# 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 ConverterController < ApplicationController
  def index
    @categories = Converter.get_available_categories
    @currencies = Converter.get_available_currencies
    @current_theme = session[:theme] || 'default'
  end
  
  def convert_currency
    amount = params[:amount].to_f
    from_currency = params[:from_currency]
    to_currency = params[:to_currency]
    
    if amount > 0 && from_currency && to_currency
      @result = Converter.convert_currency(amount, from_currency, to_currency)
      
      if @result
        # Save to calculation history
        CalculationHistory.create!(
          expression: "#{amount} #{from_currency} to #{to_currency}",
          result: @result,
          operation: 'currency_conversion'
        )
        
        respond_to do |format|
          format.html { redirect_to converter_path, notice: "Conversion successful!" }
          format.json { render json: { result: @result, from: from_currency, to: to_currency } }
        end
      else
        respond_to do |format|
          format.html { redirect_to converter_path, alert: "Invalid currency conversion" }
          format.json { render json: { error: "Invalid currency conversion" }, status: :unprocessable_entity }
        end
      end
    else
      respond_to do |format|
        format.html { redirect_to converter_path, alert: "Please enter valid amount and select currencies" }
        format.json { render json: { error: "Invalid parameters" }, status: :unprocessable_entity }
      end
    end
  end
  
  def convert_unit
    amount = params[:amount].to_f
    from_unit = params[:from_unit]
    to_unit = params[:to_unit]
    category = params[:category]
    
    if amount > 0 && from_unit && to_unit && category
      @result = Converter.convert_unit(amount, from_unit, to_unit, category)
      
      if @result
        # Save to calculation history
        CalculationHistory.create!(
          expression: "#{amount} #{from_unit} to #{to_unit} (#{category})",
          result: @result,
          operation: 'unit_conversion'
        )
        
        respond_to do |format|
          format.html { redirect_to converter_path, notice: "Conversion successful!" }
          format.json { render json: { result: @result, from: from_unit, to: to_unit, category: category } }
        end
      else
        respond_to do |format|
          format.html { redirect_to converter_path, alert: "Invalid unit conversion" }
          format.json { render json: { error: "Invalid unit conversion" }, status: :unprocessable_entity }
        end
      end
    else
      respond_to do |format|
        format.html { redirect_to converter_path, alert: "Please enter valid amount and select units" }
        format.json { render json: { error: "Invalid parameters" }, status: :unprocessable_entity }
      end
    end
  end
  
  def get_units
    category = params[:category]
    @units = Converter.get_available_units(category)
    
    respond_to do |format|
      format.json { render json: { units: @units } }
    end
  end
end
