module IdCard
  class SetupController < ApplicationController
    before_action :set_employer_and_setup

    # View Methods

    def index
      if @setup.provider_section.present? && !@setup.provider_section.default
        employer_custom_options = @setup.provider_section
      else
        employer_custom_options = IdCard::ProviderSection.new(title: "#{@employer.name} Custom")
      end
      @provider_options = (IdCard::ProviderSection.where(default: true) + [employer_custom_options])
        .compact.uniq.map { |option| [option.display_title, option.id || "99"] }
      @rx_options = IdCard::RxSection.where.not(title: nil)
      @rx_default = IdCard::RxSection.find_by(title: "FairosRx")
      render :index
    end

    def update
      if params[:id_card_setup]["provider_section_id"].include?("new|")
        new_provider_section_params = IdCard::ProviderSection.permitted_params(params)
        new_provider_section = IdCard::ProviderSection.create(new_provider_section_params)
        params[:id_card_setup]["provider_section_id"] = new_provider_section.id
      end
      setup_params = IdCard::Setup.permitted_params(params)
      if @setup.update(setup_params)
        puts "sucess"
        redirect_to employer_path(@employer.slug), notice: 'ID Card Setup was successfully updated.'
      else
        puts "fail"
        if @setup.provider_section.present? && !@setup.provider_section.default
          employer_custom_options = @setup.provider_section
        else
          employer_custom_options = IdCard::ProviderSection.new(title: "#{@employer.name} Custom")
        end
        @provider_options = (IdCard::ProviderSection.where(default: true) + [employer_custom_options])
        .compact.uniq.map { |option| [option.display_title, option.id || "99"] }
        @rx_options = IdCard::RxSection.all
        @rx_default = IdCard::RxSection.find_by(title: "FairosRx")
        render :index, status: :unprocessable_entity
      end
    end

    def plans
      @plan_templates = IdCard::Plan.templates
      render :plans
    end

    def update_plans
      plans_params = IdCard::Plan.permitted_params(params)
      if @setup.update(plans_params)
        puts "sucess"
        redirect_to employer_path(@setup.employer.slug), notice: 'ID Card Plans successfully updated.'
      else
        puts "fail"
        @plan_templates = IdCard::Plan.templates
        render :plans, status: :unprocessable_entity
      end

    end

    def field_exceptions
      render :field_exceptions
    end

    def update_field_exceptions
      field_exceptions_params = IdCard::FieldException.permitted_params(params)
      if @setup.update(field_exceptions_params)
        puts "sucess"
        redirect_to employer_path(@setup.employer.slug), notice: 'ID Card Exceptions successfully updated.'
      else
        puts "fail"
        render :field_exceptions, status: :unprocessable_entity
      end

    end

    def update_active_status
      @setup.active = !@setup.active
      if @setup.save
        puts "sucess"
        redirect_to employer_path(@setup.employer.slug), notice: 'ID Card Active Status successfully updated.'
      else
        puts "fail"
        render :show, status: :unprocessable_entity
      end
    end

    def destroy
    end

    # API Methods

    private

    def set_employer_and_setup
      @employer = Employer.find_by(slug: params[:employer_id])
      if @employer.id_card_setup.present?
        @setup = @employer.id_card_setup
      else
        @setup = @employer.create_id_card_setup
      end
    end

  end
end
