""" shopping cart management command"""
from django.core.management.base import NoArgsCommand
from ... import models
from django.contrib.auth.models import Group


class Command(NoArgsCommand):
    """ Add the store roles and permissions """

    help = "Add the store roles and permissions"

    def handle_noargs(self, **options):
        """ Create the store permissions """
        print("Start adding groups and permissions for store admin")

        (
            catalog_view_permission,
            cvp_created,
        ) = models.GlobalPermission.objects.get_or_create(
            codename="store_catalog_view", name="Can view catalog"
        )

        (
            catalog_edit_permission,
            cep_created,
        ) = models.GlobalPermission.objects.get_or_create(
            codename="store_catalog_edit", name="Can edit catalog"
        )

        (
            store_login_permission,
            slp_created,
        ) = models.GlobalPermission.objects.get_or_create(
            codename="store_admin_login", name="Can login to store admin"
        )

        (
            store_orders_permission,
            sop_created,
        ) = models.GlobalPermission.objects.get_or_create(
            codename="store_admin_orders", name="Can manage store orders"
        )

        store_admin_group, sag_created = Group.objects.get_or_create(name="store_admin")
        if sag_created:
            print("Added Store Admin Group")
            store_admin_group.permissions.add(store_login_permission)
            store_admin_group.save()

        store_catalog_edit_group, sceg_created = Group.objects.get_or_create(
            name="store_catalog_edit"
        )
        if sceg_created:
            print("Added Store catalog edit Group")
            store_catalog_edit_group.permissions.add(store_login_permission)
            store_catalog_edit_group.permissions.add(catalog_view_permission)
            store_catalog_edit_group.permissions.add(catalog_edit_permission)
            store_catalog_edit_group.save()

        store_catalog_view_group, scvg_created = Group.objects.get_or_create(
            name="store_catalog_view"
        )
        if scvg_created:
            print("Added Store catalog view Group")
            store_catalog_view_group.permissions.add(store_login_permission)
            store_catalog_view_group.permissions.add(catalog_view_permission)
            store_catalog_view_group.save()

        store_orders_group, sog_created = Group.objects.get_or_create(
            name="store_orders"
        )
        if sog_created:
            print("Added Store order Group")
            store_orders_group.permissions.add(store_login_permission)
            store_orders_group.permissions.add(store_orders_permission)
            store_orders_group.save()

        store_admin_full_group, safg_created = Group.objects.get_or_create(
            name="store_admin_full"
        )
        if safg_created:
            print("Added Store admin full Group")
            store_admin_full_group.permissions.add(store_login_permission)
            store_admin_full_group.permissions.add(catalog_view_permission)
            store_admin_full_group.permissions.add(catalog_edit_permission)
            store_admin_full_group.permissions.add(store_orders_permission)
            store_admin_full_group.save()

        print("Finished adding groups and permissions")
