# frozen_string_literal: true

module Configuration
  class DossiersEleveController < ApplicationController

    before_action :identification_agent
    before_action :if_agent_is_admin, except: %i[edit update activation]

    def changer_mef_destination
      dossiers = DossierEleve.where(etablissement: @agent_connecte.etablissement, mef_origine: params[:mef_origine])
      dossiers.update_all(mef_destination_id: params[:nouveau_mef_destination])

      dossiers.each do |dossier_eleve|
        mef_destination = dossier_eleve.mef_destination
        dossier_eleve.options_pedagogiques &= mef_destination.options_pedagogiques

        defini_options_origines dossier_eleve
      end

      redirect_to configuration_mef_index_path
    end

    def non_abandonnable?(dossier, option)
      !option.abandonnable?(dossier.mef_destination) &&
        !dossier.options_pedagogiques.include?(option) &&
        dossier.mef_destination.options_pedagogiques.include?(option)
    end

    private

    def defini_options_origines(dossier_eleve)
      options_origines = dossier_eleve.options_origines.keys.map { |o| OptionPedagogique.find(o) }
      options_origines.each do |option|
        dossier_eleve.options_pedagogiques << option if non_abandonnable?(dossier_eleve, option)
      end
    end

  end
end
