import uuid

from django.db import models


class SyncModel(models.Model):
    """Abstract base model for all djino-synced models.

    Adds revision tracking, soft deletion, and sync metadata.
    """

    _djino_id = models.UUIDField(
        default=uuid.uuid4,
        unique=True,
        editable=False,
        db_index=True,
    )
    _djino_rev = models.PositiveBigIntegerField(
        default=0,
        db_index=True,
    )
    _djino_deleted = models.BooleanField(
        default=False,
        db_index=True,
    )
    _djino_updated_at = models.DateTimeField(
        auto_now=True,
        db_index=True,
    )

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        self._djino_rev += 1
        super().save(*args, **kwargs)

    def soft_delete(self):
        self._djino_deleted = True
        self.save()
