from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType


class Command(BaseCommand):
    """
    Creates initial skeleton for the webapp.
    This command MUST do nothing if already run.
    """

    def create_infoscreen_moderator(self):
        self.stdout.write("Creating infoscreen moderator group")
        infoscreen_group, created = Group.objects.get_or_create(
            name="infoscreen moderators"
        )
        if not created:
            self.stdout.write(
                'The group "infoscreen moderators" already existed '
                "and was not therefore created"
            )

        cts = ContentType.objects.filter(app_label="infoscreen")
        permissions = Permission.objects.filter(content_type__in=cts)
        infoscreen_group.permissions.add(*permissions)

    def create_member_register_viewer(self):
        self.stdout.write("Creating member register viewer group")
        viewers_group, created = Group.objects.get_or_create(
            name="member register viewers"
        )
        if not created:
            self.stdout.write(
                'The group "member register viewers" already existed '
                "and was not therefore created"
            )

        cts = ContentType.objects.filter(app_label="members")
        members_permissions = Permission.objects.filter(
            content_type__in=cts, codename__contains="read"
        )

        viewers_group.permissions.add(*members_permissions)

    def create_member_register_administrator(self):
        self.stdout.write("Creating member register administrator group")
        admins_group, created = Group.objects.get_or_create(
            name="member register administrators"
        )
        if not created:
            self.stdout.write(
                'The group "member register administrators" already existed '
                "and was not therefore created"
            )

        cts = ContentType.objects.filter(app_label="members")
        permissions = Permission.objects.filter(content_type__in=cts)
        admins_group.permissions.add(*permissions)

    def create_official(self):
        self.stdout.write("Creating official group")
        officials_group, created = Group.objects.get_or_create(name="officials")
        if not created:
            self.stdout.write(
                'The group "officials" already existed ' "and was not therefore created"
            )

        cts = ContentType.objects.filter(app_label="webapp")
        feed_permissions = Permission.objects.filter(
            content_type__in=cts, codename__contains="feed"
        )
        event_permissions = Permission.objects.filter(
            content_type__in=cts, codename__contains="event"
        )
        registration_permissions = Permission.objects.filter(
            content_type__in=cts, codename__contains="registration"
        )

        officials_group.permissions.add(*feed_permissions)
        officials_group.permissions.add(*event_permissions)
        officials_group.permissions.add(*registration_permissions)

    def create_webapp_administrator(self):
        self.stdout.write("Creating webapp administrator group")
        admins_group, created = Group.objects.get_or_create(
            name="webapp administrators"
        )
        if not created:
            self.stdout.write(
                'The group "webapp administrators" already existed '
                "and was not therefore created"
            )

        cts = ContentType.objects.filter(app_label="webapp")
        permissions = Permission.objects.filter(content_type__in=cts)
        admins_group.permissions.add(*permissions)

    def handle(self, *args, **options):
        self.create_infoscreen_moderator()
        self.create_member_register_viewer()
        self.create_member_register_administrator()
        self.create_official()
        self.create_webapp_administrator()
        self.stdout.write("Initialization successful")
