import uuid

import pytest

from huesoporro.infra.repos import QuoteRepo, UserRepo
from huesoporro.models import TwitchAuth, User


async def test_create_user_raises_value_error_for_existing_user(
    user_repo: UserRepo, persisted_user: User
):
    with pytest.raises(
        ValueError, match=f"User {persisted_user.username} already exists"
    ):
        await user_repo.create(persisted_user)


async def test_get_user_by_username(user_repo: UserRepo, persisted_user: User):
    db_user = await user_repo.get_by_username(persisted_user.username)
    assert db_user == persisted_user


async def test_get_user_returns_none(user_repo: UserRepo):
    assert await user_repo.get_by_username("unknown_user") is None


async def test_get_user_by_id(user_repo: UserRepo, persisted_user: User):
    db_user = await user_repo.get_by_id(persisted_user.id)
    assert db_user == persisted_user


async def test_get_user_by_id_returns_none(user_repo: UserRepo):
    assert await user_repo.get_by_id(uuid.uuid4()) is None


async def test_update_users_tokens(
    user_repo: UserRepo, twitch_auth: TwitchAuth, persisted_user: User
):
    twitch_auth.access_token = uuid.uuid4().hex
    twitch_auth.refresh_token = uuid.uuid4().hex
    new_tokens = {"twitch": twitch_auth}
    persisted_user.external_auth = new_tokens  # type: ignore[assignment]
    assert await user_repo.update(persisted_user) == persisted_user


async def test_update_username(user_repo: UserRepo, persisted_user: User):
    persisted_user.username = "new_username"
    assert await user_repo.update(persisted_user) == persisted_user


async def test_update_non_existing_user_raises_value_error(
    user_repo: UserRepo, user: User
):
    with pytest.raises(ValueError, match=f"User {user.username} does not exist"):
        await user_repo.update(user)


async def test_delete_user(user_repo: UserRepo, persisted_user: User):
    assert await user_repo.delete(persisted_user) is None
    assert await user_repo.get_by_id(persisted_user.id) is None


async def test_create_chatbot_raises_value_error_for_existing_chatbot(
    chatbot_repo, chatbot_factory, persisted_chatbot
):
    with pytest.raises(
        ValueError, match=f"Chatbot {persisted_chatbot.user_id} already exists"
    ):
        await chatbot_repo.create(
            chatbot_factory.build(user_id=persisted_chatbot.user_id)
        )


async def test_update_chatbot(persisted_chatbot, faker, chatbot_repo):
    persisted_chatbot.automatic_generation_timer = faker.pyint()
    persisted_chatbot.automatic_quote_timer = faker.pyint()
    persisted_chatbot.mods = ["mod1", "mod2"]

    updated_chatbot = await chatbot_repo.update(persisted_chatbot)
    persisted_chatbot.last_updated_at = updated_chatbot.last_updated_at
    assert updated_chatbot == persisted_chatbot


async def test_update_chatbot_raises_value_error_on_non_existing_chatbot(
    chatbot_repo, chatbot
):
    with pytest.raises(ValueError, match=f"Chatbot {chatbot.user_id} does not exist"):
        await chatbot_repo.update(chatbot)


async def test_delete_chatbot_raises_value_error_on_non_existing_chatbot(
    chatbot_repo, chatbot
):
    with pytest.raises(ValueError, match=f"Chatbot {chatbot.id} does not exist"):
        await chatbot_repo.delete(chatbot)


async def test_delete_chatbot(chatbot_repo, persisted_chatbot):
    assert await chatbot_repo.delete(persisted_chatbot) is None
    assert await chatbot_repo.get_by_id(persisted_chatbot.id) is None


async def test_get_by_id(chatbot_repo, persisted_chatbot):
    chatbot = await chatbot_repo.get_by_id(persisted_chatbot.id)
    assert chatbot == persisted_chatbot


async def test_list(chatbot_repo, persisted_five_chatbots):
    chatbots = await chatbot_repo.list()
    assert len(chatbots) == 5  # noqa: PLR2004


async def test_list_offset_limit(chatbot_repo, persisted_five_chatbots):
    chatbots = await chatbot_repo.list(offset=1, limit=2)
    assert len(chatbots) == 2  # noqa: PLR2004


async def test_get_random_quote(quote_repo: QuoteRepo, persisted_quote):
    quote = await quote_repo.get_random(persisted_quote.channel_name)
    assert quote
    assert quote.author == persisted_quote.author
    assert quote.channel_name == persisted_quote.channel_name


async def test_create_quote_raises_value_error_for_existing_quote(
    quote_repo: QuoteRepo, persisted_quote
):
    with pytest.raises(
        ValueError, match=f"Quote {persisted_quote.quote} already exists"
    ):
        await quote_repo.create(persisted_quote)


async def test_create_quote(quote_repo: QuoteRepo, quote_factory):
    quote = quote_factory.build()
    created_quote = await quote_repo.create(quote)
    assert created_quote == quote


async def test_update_quote_raises_value_error_on_non_existing_quote(
    quote_repo: QuoteRepo, quote
):
    with pytest.raises(ValueError, match=f"Quote {quote.id} does not exist"):
        await quote_repo.update(quote)


async def test_update_quote(quote_repo: QuoteRepo, persisted_quote):
    persisted_quote.quote = "new quote"
    updated_quote = await quote_repo.update(persisted_quote)
    persisted_quote.last_updated_at = updated_quote.last_updated_at
    assert updated_quote == persisted_quote


async def test_delete_quote(quote_repo: QuoteRepo, persisted_quote):
    assert await quote_repo.delete(persisted_quote) is None
    assert await quote_repo.get_by_id(persisted_quote.id) is None


async def test_list_quotes(quote_repo, persisted_five_quotes):
    quotes = await quote_repo.list()
    assert len(quotes) == 5  # noqa: PLR2004


async def test_list_quotes_offset_limit(quote_repo, persisted_five_quotes):
    quotes = await quote_repo.list(offset=1, limit=2)
    assert len(quotes) == 2  # noqa: PLR2004
