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

import datetime

from django.utils.timezone import get_current_timezone, now
import pytest

from hotpocket_soa.dto.accounts import AuthKeyOut


@pytest.fixture
def auth_key_factory(request: pytest.FixtureRequest):
    default_account = request.getfixturevalue('account')

    def factory(account=None, **kwargs):
        from hotpocket_backend_testing.factories.accounts import AuthKeyFactory

        return AuthKeyFactory(
            account_uuid=(
                account.pk
                if account is not None
                else default_account.pk
            ),
            **kwargs,
        )

    return factory


@pytest.fixture
def auth_key(auth_key_factory):
    return auth_key_factory()


@pytest.fixture
def auth_key_out(auth_key):
    return AuthKeyOut.model_validate(auth_key, from_attributes=True)


@pytest.fixture
def deleted_auth_key(auth_key_factory):
    return auth_key_factory(deleted_at=now())


@pytest.fixture
def deleted_auth_key_out(deleted_auth_key):
    return AuthKeyOut.model_validate(deleted_auth_key, from_attributes=True)


@pytest.fixture
def expired_auth_key(auth_key_factory):
    result = auth_key_factory()
    result.created_at = datetime.datetime(
        1987, 10, 3, 8, 0, 0, tzinfo=get_current_timezone(),
    )
    result.save()

    return result


@pytest.fixture
def expired_auth_key_out(expired_auth_key):
    return AuthKeyOut.model_validate(expired_auth_key, from_attributes=True)


@pytest.fixture
def consumed_auth_key(auth_key_factory):
    return auth_key_factory(consumed_at=now())


@pytest.fixture
def consumed_auth_key_out(consumed_auth_key):
    return AuthKeyOut.model_validate(consumed_auth_key, from_attributes=True)


@pytest.fixture
def other_auth_key(auth_key_factory):
    return auth_key_factory()


@pytest.fixture
def other_auth_key_out(other_auth_key):
    return AuthKeyOut.model_validate(other_auth_key, from_attributes=True)


@pytest.fixture
def inactive_account_auth_key(auth_key_factory, inactive_account):
    return auth_key_factory(account=inactive_account)


@pytest.fixture
def inactive_account_auth_key_out(auth_key):
    return AuthKeyOut.model_validate(
        inactive_account_auth_key, from_attributes=True,
    )


@pytest.fixture
def other_account_auth_key(auth_key_factory, other_account):
    return auth_key_factory(account=other_account)


@pytest.fixture
def other_account_auth_key_out(other_account_auth_key):
    return AuthKeyOut.model_validate(
        other_account_auth_key, from_attributes=True,
    )
