from django.db import models
from at_django_boilerplate.utils.mixins import HashidSlugMixin

from django.conf import  settings
from django.utils import  timezone

class TrackedChange(HashidSlugMixin):
    CHANGE_TYPE = (('add','Add'),
                   ('edit','Edit'),
                   ('delete','Delete'),
                   )
    existing = models.TextField(null=True,blank=True)
    change = models.TextField(null=True,blank=True)
    change_type = models.CharField(choices=CHANGE_TYPE,default='add',max_length=10)
    change_done_by = models.ForeignKey(settings.AUTH_USER_MODEL,
                                       null=True,
                                       blank=True,
                                       on_delete=models.DO_NOTHING,
                                       related_name='changes_by_me')
    for_utility = models.ForeignKey('meters.UtilityModel',null=True,blank=True,on_delete=models.DO_NOTHING,related_name='utility_changes')
    applied_on = models.DateTimeField(default=timezone.now)

    def __str__(self):
        if self.change_type=='add':
            return f'{self.change_done_by} added {self.change}'
        elif self.change_type=='edit':
            return f'{self.change_done_by} updated {self.existing} to {self.change}'

        elif self.change_type=='delete':
            return f'{self.change_done_by} deleted {self.change}'
