class User::GoogleSsoController < ApplicationController
  before_action :validate_access

  def show
    render_success :ok, json: { google_sso: current_user.google_id? }
  end

  def update
    if current_user.update(google_id: params[:google_id])
      render_success :ok, json: { google_sso: current_user.google_id? }
    else
      render_error :unprocessable_entity, errors: current_user
    end
  end

  private
    def school_config
      @school_config ||= current_school.find_or_build_school_config
    end

    def validate_access
      if role == :student
        school_config.google_sso_student? || school_config.google_sso_both?
      elsif role == :employee
        school_config.google_sso_employee? || school_config.google_sso_both?
      else
        render_error :unauthorized, errors: 'You do not have access to this resource'
      end
    end
end
