from uuid import UUID

from corehq.apps.cleanup.management.commands.populate_sql_model_from_couch_model import PopulateSQLCommand
from ...models import OwnerType


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

    @classmethod
    def couch_doc_type(self):
        return 'FixtureOwnership'

    @classmethod
    def sql_class(self):
        from corehq.apps.fixtures.models import LookupTableRowOwner
        return LookupTableRowOwner

    @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.
        """
        fields = ["domain", "owner_id"]
        diffs = [cls.diff_attr(name, couch, sql) for name in fields]
        diffs.append(cls.diff_value(
            "owner_type",
            OWNER_TYPE_MAP[couch["owner_type"]],
            sql.owner_type,
        ))
        diffs.append(cls.diff_value(
            "row_id",
            UUID(couch["data_item_id"]),
            sql.row_id,
        ))
        return diffs

    def update_or_create_sql_object(self, doc):
        model, created = self.sql_class().objects.update_or_create(
            couch_id=doc['_id'],
            defaults={
                "domain": doc["domain"],
                "row_id": UUID(doc["data_item_id"]),
                "owner_type": OWNER_TYPE_MAP[doc["owner_type"]],
                "owner_id": doc.get("owner_id"),
            })
        return model, created


OWNER_TYPE_MAP = {
    "user": OwnerType.User,
    "group": OwnerType.Group,
    "location": OwnerType.Location,
}

@@ -1308,30 +1308,37 @@
        O
-WNER_TYPE_MAP%5B
+wnerType.from_string(
 couch%5B%22o
@@ -1340,33 +1340,33 @@
 ch%5B%22owner_type%22%5D
-%5D
+)
 ,%0A            sq
@@ -1848,22 +1848,29 @@
 %22: O
-WNER_TYPE_MAP%5B
+wnerType.from_string(
 doc%5B
@@ -1882,17 +1882,17 @@
 r_type%22%5D
-%5D
+)
 ,%0A      
@@ -1949,16 +1949,16 @@
      %7D)%0A
+
         
@@ -1983,121 +1983,4 @@
 ted%0A
-%0A%0AOWNER_TYPE_MAP = %7B%0A    %22user%22: OwnerType.User,%0A    %22group%22: OwnerType.Group,%0A    %22location%22: OwnerType.Location,%0A%7D%0A

Use OwnerType.from_string instead of dict constant