from corehq.apps.cleanup.management.commands.populate_sql_model_from_couch_model import (
    PopulateSQLCommand,
)
from corehq.motech.models import ConnectionSettings
from corehq.motech.repeaters.dbaccessors import (
    get_all_repeater_docs,
    get_repeater_count_for_domains,
    get_repeaters_by_domain,
)


class Command(PopulateSQLCommand):
    @classmethod
    def couch_db_slug(cls):
        return "receiverwrapper"

    @classmethod
    def couch_doc_type(cls):
        return 'CaseRepeater'

    @classmethod
    def sql_class(cls):
        from corehq.motech.repeaters.models import SQLCaseRepeater
        return SQLCaseRepeater

    @classmethod
    def commit_adding_migration(cls):
        return "TODO: add once the PR adding this file is merged"

    @classmethod
    def diff_couch_and_sql(cls, couch, sql):
        """
        This should compare each attribute of the given couch document and sql object.
        Return a list of human-reaedable strings describing their differences, or None if the
        two are equivalent. The list may contain `None` or empty strings which will be filtered
        out before display.

        Note: `diff_value`, `diff_attr` and `diff_lists` methods of `PopulateSQLCommand` are useful
        helpers.
        """
        return None

    def update_or_create_sql_object(self, doc):
        model, created = self.sql_class().objects.update_or_create(
            couch_id=doc['_id'],
            defaults={
                "version": doc.get("version"),
                "repeater_id": doc.get('repeater_id'),
                "domain": doc.get("domain"),
                "connection_settings": doc.get("connection_settings_id"),
                "auth_type": doc.get("auth_type"),
                "notify_addresses_str": doc.get("notify_addresses_str"),
                "format": doc.get("format"),
                "paused": doc.get("paused"),
                "started_at": force_to_datetime(doc.get("started_at")),
                "last_success_at": force_to_datetime(doc.get("last_success_at")),
                "failure_streak": doc.get("failure_streak"),

            })
        # add code to migrate CaseRepeater.white_listed_case_types
        # add code to migrate CaseRepeater.black_listed_users
        return model, created

@@ -1422,27 +1422,34 @@
     
-couch
+repeater
 _id=doc
-%5B
+.get(
 '_id'
-%5D
+)
 ,%0A  
@@ -1520,63 +1520,8 @@
 %22),%0A
-                %22repeater_id%22: doc.get('repeater_id'),%0A
     
@@ -1604,18 +1604,9 @@
 s%22: 
-doc.get(%22c
+C
 onne
@@ -1614,145 +1614,65 @@
 tion
-_s
+S
 ettings
-_id%22),%0A                %22auth_type%22: doc.get(%22auth_type%22),%0A                %22notify_addresses_str%22: doc.get(%22notify_addresses_str%22
+.objects.get(id=doc.get(%22connection_settings_id%22)
 ),%0A 
@@ -1732,16 +1732,19 @@
        %22
+is_
 paused%22:
@@ -1784,350 +1784,152 @@
    %22
-started_at%22: force_to_datetime(doc.get(%22started_at%22)),%0A                %22last_success_at%22: force_to_datetime(doc.get(%22last_success_at%22)),%0A                %22failure_streak%22: doc.get(%22failure_streak%22),%0A%0A            %7D)%0A        # add code to migrate CaseRepeater.white_listed_case_types%0A        # add code to migrate CaseRepeater.black_listed_users
+white_listed_case_types%22: doc.get(%22white_listed_case_types%22),%0A                %22black_listed_users%22: doc.get(%22black_listed_users%22)%0A            %7D)
 %0A   

update implementation of update_or_create_sql_object