class IdCardBenefitsTemplatesController < ApplicationController
  skip_before_action :verify_authenticity_token

  def new_id_card_template
    @id_card_templates = IdCardTemplate.where.not(title: "BLANK")
    @id_card_template_benefits = IdCardTemplate.find_by(title: "BLANK").id_card_template_benefits.sort_by(&:sequence)
  end

  def create_id_card_template
    @id_card_template = IdCardTemplate.create(title: params[:title])

    params[:benefits].each do |key, value|
      IdCardTemplateBenefit.create(
        sequence: key,
        benefit_desc: value["desc"],
        benefit: value["value"],
        id_card_template: @id_card_template
      )

    end

    respond_to do |format|
      if @id_card_template.save && @id_card_template.id_card_template_benefits.length == 14
        format.html { redirect_to '/dev_tools/new_id_card_setup', notice: "Template was successfully created." }
        # format.json { render :show, status: :created, location: @employer }
      else
        format.html { render :new_id_card_template, status: :unprocessable_entity }
        # format.json { render json: @employer.errors, status: :unprocessable_entity }
      end
    end
  end

  def new_id_card_setup
    @id_card_templates = IdCardTemplate.where.not(title: "BLANK")
    @id_card_template_benefits = IdCardTemplate.find_by(title: "BLANK").id_card_template_benefits
  end

  def get_template_benefits
    @id_card_benefits = IdCardBenefitsTemplate.find(params[:id]).id_card_benefits
    render json: @id_card_benefits.as_json
  end

  def create_id_card_setup
    employer_general = params['general']
    hl_plan_code = Vhcs::HlPlanCode.new(
      group_number: employer_general['group_number'],
      medical_number: employer_general['group_number'],
      dental_number: '',
      plan_key: employer_general['pl_plan_key'],
      effect_date: employer_general['effect_date']
    )

    # Replace fairos_info with template like for benefits
    fairos_info = Vhcs::HLRXCrosRef.where(pl_plan_key: 52).first
    hlrx_cros_ref = Vhcs::HLRXCrosRef.new(
      group_no: employer_general['group_number'],
      rx_group_id: employer_general['group_number'],
      help_desk: fairos_info.help_desk,
      customer_service: fairos_info.customer_service,
      web_url: fairos_info.web_url,
      pl_plan_key: employer_general['pl_plan_key']
    )

    number_of_plans = params[:number_of_plans].to_i

     number_of_plans.each do |i|
      value['benefits'].each do |ben_key, ben_value|
        Vhcs::HLEgglestonCardBenefit.create(
          plan_id: value['plan_id'],
          benefit_desc: ben_value["desc"],
          benefit: ben_value["value"],
          sequence: ben_key,
          plan_key: employer_general['pl_plan_key']
        )
      end
    end

    respond_to do |format|
      if hl_plan_code.save && hlrx_cros_ref.save
        format.html { redirect_to '/dev_tools/new_id_card_setup', notice: "Card setup was successfully created." }
        # format.json { render :show, status: :created, location: @employer }
      else
        format.html { render :new_id_card_setup, status: :unprocessable_entity }
        # format.json { render json: @employer.errors, status: :unprocessable_entity }
      end
    end
  end

end
