class InstallController < BaseController
  skip_before_action :set_current_tenant_by_subdomain_or_name
  before_action :lookup_domain

	def index
	end

	def activate
    flag = "<%= ENV['AES_IV'] %>"
    key_base = "<%= ENV['secret_key_base'] %>"

		restaurant = params[:restaurant_name]
		license_key = params[:license_key]
		# admin_user = params[:admin_user]
		# admin_password = params[:admin_password]
    db_host = params[:db_host]
    db_schema = params[:db_schema]
    db_user = params[:db_user]
    db_password = params[:db_password]
    phrase = license_key

    @license = License.new(ENV["SX_PROVISION_URL"], request.host)
    response = @license.license_activate(restaurant, license_key, db_host, db_schema, db_user, db_password)
    if response[:status]
      redirect_to root_path, notice: response["message"]
    else
      redirect_to activate_path, notice: response["message"]
    end
	end

private

	def lookup_domain
    if ENV["SERVER_MODE"] == "cloud"
      not_found
    elsif ENV["SERVER_MODE"] == "application" || request.subdomains.last && request.subdomains.last != "www"
      if current_license.exists? #(request.host)
        if current_license.expired?
          redirect_to review_license_path
        else
          redirect_to root_path
        end
      end
    else
      not_found
    end
  end

end
