from django.db import models


def deactivate_services():
    services = Service.objects.all()
    services.update(is_active=False)

def get_active():
    try:
        service = Service.objects.get(is_active=True)
        return service
    except:
        return None

class Service(models.Model):
    name = models.CharField(
        verbose_name='Название услуги',
        max_length=255
    )
    
    price = models.PositiveIntegerField(
        verbose_name='Цена услуги',
    )

    group_link = models.URLField(
        verbose_name='Ссылка на группу'
    )


    is_active = models.BooleanField(
        verbose_name='Активный',
        default=False
    )


    class Meta:
        verbose_name = 'Услуга'
        verbose_name_plural = 'Услуги'
    

    def __str__(self) -> str:
        return f'{self.name} | {self.price}'

    def save(self, *args, **kwargs): 
        if self.is_active:
            deactivate_services()
            self.is_active = True
        
        super(Service, self).save(*args, **kwargs) 



    
    