class Admin::Admissions::Applications::ProfileFieldsController < Admin::Admissions::Controller
  include Admin::Admissions::ApplicationScoped

  def show
    render_success :ok, json: profile_fields.map { |f| profile_field_props(f) }
  end

  def update
    profile_fields.each { |f| f.assign_attributes(profile_field_params(f)) }
    if transactional_save(profile_fields)
      render_success :ok
    else
      render_error :unprocessible_entity, message: 'Something went wrong.'
    end
  end

  private
    def profile_fields
      @profile_fields ||= application.profile_fields.build_all(application)
    end

    def profile_field_params(profile_field)
      params.require(profile_field.field).permit(:display, :required)
    end

    def profile_field_props(profile_field)
      {
        field: profile_field.field,
        name: profile_field.field.titleize,
        display: profile_field.display,
        required: profile_field.required,
        locked: Admission::ProfileField::REQUIRED_FIELDS.include?(profile_field.field)
      }
    end
end
