module Admin
  class ConferencesController < Admin::BaseController
    before_action :authorize_permission
    before_action :set_conference, except: [ :index, :new, :create ]

    def index
      @conferences = Conference.all
    end

    def new
      @conference = Conference.new
    end

    def create
      @conference = Conference.new(conference_params)

      if @conference.save
        FetchConferenceDataJob.perform_later(@conference.slug)
        redirect_to import_progress_admin_conference_path(@conference), notice: "Conference was successfully created and data import has started."
      else
        render :new, status: :unprocessable_entity
      end
    end

    def edit
    end

    def update
      if @conference.update(conference_params)
        redirect_to admin_conferences_path, notice: "Conference was successfully updated."
      else
        render :edit, status: :unprocessable_entity
      end
    end

    def destroy
      @conference.destroy
      redirect_to admin_conferences_path, notice: "Conference was successfully deleted."
    end

    def import_progress
    end

    def import_status
      render json: {
        status: @conference.import_status,
        started_at: @conference.last_import_started_at,
        completed_at: @conference.last_import_completed_at,
        error_summary: @conference.import_error_summary,
        has_error: @conference.last_import_error.present?
      }
    end

    def import_error
      render layout: false if request.xhr?
    end

    def import_history
      @import_histories = @conference.import_histories.order(created_at: :desc).limit(20)
    end

    def retry_import
      FetchConferenceDataJob.perform_later(@conference.slug)
      redirect_to import_progress_admin_conference_path(@conference), notice: "Data import has been started."
    end

    def select_relevant_stages
      @stages = @conference.stages.order(:name)
    end

    def update_relevant_stages
      @conference.relevant_stage_ids = params[:conference][:relevant_stage_ids]

      if @conference.save
        redirect_to admin_conferences_path, notice: "Relevant stages updated successfully."
      else
        @stages = @conference.stages.order(:name)
        render :select_relevant_stages
      end
    end

    private

    def authorize_permission
      super("manage_conferences")
    end

    def set_conference
      @conference = Conference.find_by(slug: params[:slug])
    end

    def conference_params
      all_params = params.require(:conference).permit(:name, :slug, :starts_at, :ends_at, :url, :time_zone, :import_job_class).to_h

      data_hash = @conference&.data&.dup || {}

      if params[:data].present?
        params[:data].each do |key, value|
          data_hash[key] = value.presence
        end
      end

      if params[:custom_field_keys].present? && params[:custom_field_values].present?
        keys = params[:custom_field_keys]
        values = params[:custom_field_values]

        keys.each_with_index do |key, index|
          next if key.blank?
          data_hash[key] = values[index].presence
        end
      end

      all_params[:data] = data_hash

      all_params
    end
  end
end