from django.apps import AppConfig
from django.db.models.signals import post_migrate


def create_photo_permissions(sender, **kwargs):
    """Create custom permissions for domain-specific photo models"""
    from django.contrib.auth.models import Permission
    from django.contrib.contenttypes.models import ContentType
    from parks.models import ParkPhoto
    from rides.models import RidePhoto

    # Create permissions for ParkPhoto
    park_photo_content_type = ContentType.objects.get_for_model(ParkPhoto)
    Permission.objects.get_or_create(
        codename="add_parkphoto",
        name="Can add park photo",
        content_type=park_photo_content_type,
    )
    Permission.objects.get_or_create(
        codename="change_parkphoto",
        name="Can change park photo",
        content_type=park_photo_content_type,
    )
    Permission.objects.get_or_create(
        codename="delete_parkphoto",
        name="Can delete park photo",
        content_type=park_photo_content_type,
    )

    # Create permissions for RidePhoto
    ride_photo_content_type = ContentType.objects.get_for_model(RidePhoto)
    Permission.objects.get_or_create(
        codename="add_ridephoto",
        name="Can add ride photo",
        content_type=ride_photo_content_type,
    )
    Permission.objects.get_or_create(
        codename="change_ridephoto",
        name="Can change ride photo",
        content_type=ride_photo_content_type,
    )
    Permission.objects.get_or_create(
        codename="delete_ridephoto",
        name="Can delete ride photo",
        content_type=ride_photo_content_type,
    )


class MediaConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "media"

    def ready(self):
        post_migrate.connect(create_photo_permissions, sender=self)
