require_dependency "multitenacy/application_controller"
module Multitenacy
  class Account::AccountsController < ApplicationController
    before_filter :authenticate_user!
    before_filter :authorize_owner, only: [:edit, :update, :plan]
    before_filter :load_plan, only: [:plan, :subscribe, :confirm_plan]

    def update
      plan_id = account_params.delete(:plan_id)
      if current_account.update_attributes(account_params)
        flash[:success] = 'Account updated successfully.'
        if plan_id != current_account.plan_id
          redirect_to plan_account_url(plan_id: plan_id)
        else
          redirect_to root_path
        end
      else
        flash[:error] = 'Account could not be updated.'
        render :edit
      end
    end

    def plan
    end

    def subscribe
      @result = Braintree::TransparentRedirect.confirm(request.query_string)
      if @result.success?
        subscription_result = Braintree::Subscription.create(payment_method_token: @result.customer.credit_cards[0].token, plan_id: @plan.braintree_id)
        if subscription_result.success?
          current_account.update_column(:plan_id, params[:plan_id])
          current_account.update_column(:braintree_subscription_id, subscription_result.subscription.id)
          flash[:success] = "You have switched to '#{@plan.name}' plan."
          redirect_to root_url
        else
          flash[:error] = "Invalid credit card details. Please try again."
          render :plan
        end
      else
        flash[:error] = "Invalid credit card details. Please try again."
        render :plan
      end
    end

    def confirm_plan
      subscription_id = current_account.braintree_subscription_id
      if subscription_id.present?
        subscription_result = Braintree::Subscription.update(subscription_id, plan_id: @plan.braintree_id)
        if subscription_result.present? && subscription_result.success?
          current_account.update_column(:plan_id, @plan.id)
          flash[:success] = "You have switched to the '#{@plan.name}' plan."
          redirect_to root_path
        else
          flash[:error] = "Something went wrong. Please try again."
          render :plan
        end
      else
        flash[:error] = "Something went wrong. Please try again."
        render :plan
      end
    end

    private
    def account_params
      params.require(:account).permit(:name, :plan_id)
    end

    def load_plan
      @plan = Multitenacy::Plan.find_by_id(params[:plan_id])
    end
  end
end
