class Admin::Legacy::Family::Families::MedicalsController < Admin::Controller
  def show
    render_success :ok, json: props
  end

  def update
    if medical.update(medical_params)
      render_success :ok, json: props
    else
      render_error :unprocessable_entity, errors: medical
    end
  end

  private
    def family
      @family ||= current_school.families.find_by(id: params[:family_id])
    end

    def medical
      @medical ||= family.find_or_build_family_medical
    end

    def medical_params
      params.permit(
        :physician,
        :physician_phone,
        :physician_address,
        :physician_address_ext,
        :physician_city,
        :physician_state,
        :physician_zip,
        :physician_country_id,
        :dentist,
        :dentist_phone,
        :dentist_address,
        :dentist_address_ext,
        :dentist_city,
        :dentist_state,
        :dentist_zip,
        :dentist_country_id,
        :insurance_company,
        :insurance_plan,
        :insurance_group,
        :hospital,
        :comments
      )
    end

    def props
      {
        id: medical.id,
        physician: medical.physician,
        physician_phone: medical.physician_phone,
        physician_address: medical.physician_address,
        physician_address_ext: medical.physician_address_ext,
        physician_city: medical.physician_city,
        physician_state: medical.physician_state,
        physician_zip: medical.physician_zip,
        physician_country_id: medical.physician_country_id,
        physician_country_name: medical.physician_country&.name,
        dentist: medical.dentist,
        dentist_phone: medical.dentist_phone,
        dentist_address: medical.dentist_address,
        dentist_address_ext: medical.dentist_address_ext,
        dentist_city: medical.dentist_city,
        dentist_state: medical.dentist_state,
        dentist_zip: medical.dentist_zip,
        dentist_country_id: medical.dentist_country_id,
        dentist_country_name: medical.dentist_country&.name,
        insurance_company: medical.insurance_company,
        insurance_plan: medical.insurance_plan,
        insurance_group: medical.insurance_group,
        hospital: medical.hospital,
        comments: medical.comments
      }
    end
end
