import pytest
from django.db import connection

from djino.models import SyncModel


@pytest.mark.django_db
class TestSyncModel:
    def test_djino_fields_exist(self):
        from tests.testapp.models import Note

        field_names = {f.name for f in Note._meta.get_fields()}
        assert "_djino_id" in field_names
        assert "_djino_rev" in field_names
        assert "_djino_deleted" in field_names
        assert "_djino_updated_at" in field_names

    def test_rev_auto_increments_on_save(self):
        from django.contrib.auth.models import User

        from tests.testapp.models import Note

        user = User.objects.create_user("testuser")
        note = Note(title="Test", status="draft", author=user)
        assert note._djino_rev == 0
        note.save()
        assert note._djino_rev == 1
        note.title = "Updated"
        note.save()
        assert note._djino_rev == 2

    def test_soft_delete(self):
        from django.contrib.auth.models import User

        from tests.testapp.models import Note

        user = User.objects.create_user("testuser2")
        note = Note.objects.create(title="Test", status="draft", author=user)
        assert note._djino_deleted is False
        note.soft_delete()
        note.refresh_from_db()
        assert note._djino_deleted is True

    def test_djino_id_is_uuid(self):
        import uuid

        from django.contrib.auth.models import User

        from tests.testapp.models import Note

        user = User.objects.create_user("testuser3")
        note = Note.objects.create(title="Test", status="draft", author=user)
        assert isinstance(note._djino_id, uuid.UUID)

    def test_is_abstract(self):
        assert SyncModel._meta.abstract is True

    def test_concrete_model_is_not_abstract(self):
        from tests.testapp.models import Note

        assert not Note._meta.abstract
        assert Note._meta.db_table == "testapp_note"
