# -*- coding: utf-8 -*-
from __future__ import annotations

import typing
import uuid

from hotpocket_backend.apps.accounts.models import AccessToken


class AccessTokensTestingService:
    def assert_created(self,
                       *,
                       key: str,
                       account_uuid: uuid.UUID,
                       origin: str,
                       meta: dict,
                       ):
        access_token = AccessToken.objects.get(key=key)
        assert access_token.account_uuid == account_uuid
        assert access_token.origin == origin
        assert access_token.meta == meta

        assert access_token.created_at is not None
        assert access_token.updated_at is not None

    def assert_deleted(self, *, pk: uuid.UUID, reference: typing.Any = None):
        access_token = AccessToken.objects.get(pk=pk)
        assert access_token.deleted_at is not None

        if reference is not None:
            assert access_token.updated_at > reference.updated_at

    def assert_not_deleted(self, *, pk: uuid.UUID, reference: typing.Any = None):
        access_token = AccessToken.objects.get(pk=pk)
        assert access_token.deleted_at is None

        if reference is not None:
            assert access_token.updated_at == reference.updated_at

    def assert_meta_updated(self, *, pk: uuid.UUID, meta_update: dict, reference: typing.Any = None):
        access_token = AccessToken.objects.get(pk=pk)

        if len(meta_update) > 0 and reference is not None:
            expected_meta = {
                **reference.meta,
                **meta_update,
            }
            assert access_token.meta == expected_meta

        if reference is not None:
            assert access_token.updated_at > reference.updated_at
