# netbox_tag_id/signals.py
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from extras.models import CustomField
from .models import TagidCustomField

@receiver(post_save, sender=CustomField)
def ensure_tagid_config(sender, instance: CustomField, created, **kwargs):
    if instance.type == "tagid":
        TagidCustomField.objects.get_or_create(tag_id=instance)
    else:
        TagidCustomField.objects.filter(tag_id=instance).delete()

@receiver(post_delete, sender=CustomField)
def delete_tagid_config(sender, instance: CustomField, **kwargs):
    TagidCustomField.objects.filter(tag_id=instance).delete()
