# Deprecated. Use v1/otp instead
# TODO: Remove this!
class OtpController < ApplicationController
  class UUIDMissing < StandardError
  end

  class NotAValidUser < StandardError
  end

  def index
    @otps = if params[:expired].present?
              Otp.where expired: params[:expired].eql?('true')
            else
              Otp.all
            end
  end

  def request_otp
    user = User.find_by msisdn: params[:msisdn]
    raise NotAValidUser unless user

    otp = Otp.generate
    render json: encrypt({uuid: otp.uuid, status: user ? 'success' : 'failed'}.to_json)
  end

  def confirm_otp
    raise UUIDMissing unless params[:uuid] and params[:otp]

    status = Otp.valid?(params[:uuid], params[:otp]) ? 'success' : 'failed'
    render json: encrypt({uuid: params[:uuid], status: status}.to_json)
  end

  rescue_from UUIDMissing do |ex|
    render json: encrypt({error: 'Missing param UUID/OTP'}.to_json), status: 404
  end

  rescue_from NotAValidUser do |ex|
    render json: encrypt({error: 'User not linked', status: 'failed'}.to_json), status: 404
  end
end
