class Admin::Google::ConfigsController < Admin::Controller
  def show
    render_success :ok, json: config_props
  end

  def update
    if google_config.update(config_params)
      render_success :ok, json: config_props
    else
      render_error :unprocessable_entity, errors: google_config
    end
  end

  def refresh_domains
    google_config.resfresh_domains
    render_success :ok, json: google_config.google_domains
  end

  private
    def google_config
      @google_config ||= current_school.find_or_build_google_config
    end

    def config_params
      params.permit(:email, :active, :token, :student_domain_id, :employee_domain_id)
    end

    def config_props
      {
        id: google_config.id,
        email: google_config.email,
        active: google_config.active,
        student_domain_id: google_config.student_domain_id,
        employee_domain_id: google_config.employee_domain_id,
        domains: google_config.google_domains
      }
    end
end
