from scrapi.processing.osf import crud
from scrapi.processing.osf import collision
from scrapi.processing.base import BaseProcessor


class OSFProcessor(BaseProcessor):
    NAME = 'osf'

    def process_normalized(self, raw_doc, normalized):
        found, _hash = collision.already_processed(raw_doc)

        if found:
            return

        normalized['meta'] = {
            'docHash': _hash
        }

        # if crud.is_event(normalized):
        #     crud.dump_metadata(normalized, {})
        #     return

        normalized['collisionCategory'] = crud.get_collision_cat(normalized['source'])

        report_norm = normalized
        resource_norm = crud.clean_report(normalized)

        report_hash = collision.generate_report_hash_list(report_norm)
        resource_hash = collision.generate_resource_hash_list(resource_norm)

        # report = collision.detect_collisions(report_hash)
        # resource = collision.detect_collisions(resource_hash, is_resource=True)

        report_norm['meta']['uids'] = report_hash
        resource_norm['meta']['uids'] = resource_hash
        resource_norm['meta']['isResource'] = True

        crud.dump_metadata(report_norm, {})
        crud.dump_metadata(resource_norm, {})

        # if not resource:
        #     resource = crud.create_resource(resource_norm)
        # else:
        #     crud.dump_metadata(resource_norm, {'nid': resource})
        #     crud.update_node(report, report_norm)

        # if not report:
        #     report = crud.create_report(report_norm, resource)
        # else:
        #     crud.dump_metadata(report_norm, {'nid': report, 'pid': resource})
        #     if not crud.is_claimed(resource):
        #         crud.update_node(resource, resource_norm)

@@ -409,119 +409,8 @@
  %7D%0A%0A
-        # if crud.is_event(normalized):%0A        #     crud.dump_metadata(normalized, %7B%7D)%0A        #     return%0A%0A
     
@@ -742,16 +742,16 @@
     
-# report
+resource
  = c
@@ -783,18 +783,39 @@
 s(re
-port_hash)
+source_hash, is_resource=True)%0A
 %0A   
@@ -819,17 +819,22 @@
         
-#
+if not
  resourc
@@ -838,70 +838,65 @@
 urce
- = collision.detect_collisions(resource_hash, is_resource=True
+:%0A            resource = crud.dump_metadata(resource_norm
 )%0A%0A 
@@ -958,155 +958,97 @@
   re
-source
+port
 _norm%5B'
-meta'%5D%5B'uids'%5D = resource_hash%0A        resource_norm%5B'meta'%5D%5B'isResource'%5D = True%0A%0A        crud.dump_metadata(report_norm, %7B%7D)%0A       
+attached'%5D = %7B%0A            'pmid': resource%5B'_id'%5D%0A        %7D%0A%0A        report =
  cru
@@ -1069,23 +1069,17 @@
 a(re
-source
+port
 _norm
-, %7B%7D
 )%0A%0A 
@@ -1076,34 +1076,32 @@
 t_norm)%0A%0A       
- #
  if not resource
@@ -1092,32 +1092,48 @@
  if not resource
+.get('attached')
 :%0A        #     
@@ -1121,26 +1121,24 @@
 d'):%0A       
- #
      resourc
@@ -1142,129 +1142,72 @@
 urce
- = crud.create_resource(resource_norm)%0A        # else:%0A        #     crud.dump_metadata(resource_norm, %7B'nid': resource%7D)
+%5B'attached'%5D = %7B%7D%0A%0A        if resource%5B'attached'%5D.get('cmids'):
 %0A   
@@ -1215,76 +1215,73 @@
     
-#
     
- crud.update_node(report, report_norm)%0A%0A        # if not report
+resource%5B'attached'%5D%5B'cmids'%5D += %5Breport%5B'_id'%5D%5D%0A        else
 :%0A  
@@ -1289,220 +1289,158 @@
     
- #
      re
-port = crud.create_report(report_norm, resource)%0A        # else:%0A        #     crud.dump_metadata(report_norm, %7B'nid': report, 'pid': resource%7D)%0A        #     if not crud.is_claimed(resource):%0A        # 
+source%5B'attached'%5D%5B'cmids'%5D = %5Breport%5B'_id'%5D%5D%0A%0A        resource%5B'meta'%5D%5B'uids'%5D = list(set(resource%5B'meta'%5D.get('uids', %5B%5D) + resource_hash))%0A%0A
     
@@ -1459,12 +1459,16 @@
 ate_
-node
+metadata
 (res
@@ -1472,16 +1472,23 @@
 resource
+%5B'_id'%5D
 , resour

Update OSF logic