"""Mastodon tests configuration."""

from collections.abc import Generator
from unittest.mock import AsyncMock, patch

from mastodon.Mastodon import Account, InstanceV2, Status
import pytest

from homeassistant.components.mastodon.const import CONF_BASE_URL, DOMAIN
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_CLIENT_ID, CONF_CLIENT_SECRET

from tests.common import MockConfigEntry, load_fixture


@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
    """Override async_setup_entry."""
    with patch(
        "homeassistant.components.mastodon.async_setup_entry",
        return_value=True,
    ) as mock_setup_entry:
        yield mock_setup_entry


@pytest.fixture
def mock_mastodon_client() -> Generator[AsyncMock]:
    """Mock a Mastodon client."""
    with (
        patch(
            "homeassistant.components.mastodon.utils.Mastodon",
            autospec=True,
        ) as mock_client,
    ):
        client = mock_client.return_value
        client.instance_v1.return_value = InstanceV2.from_json(
            load_fixture("instance.json", DOMAIN)
        )
        client.instance_v2.return_value = InstanceV2.from_json(
            load_fixture("instance.json", DOMAIN)
        )
        client.account_verify_credentials.return_value = Account.from_json(
            load_fixture("account.json", DOMAIN)
        )
        client.account_lookup.return_value = Account.from_json(
            load_fixture("account.json", DOMAIN)
        )
        client.mastodon_api_version = 2
        client.status_post.return_value = Status.from_json(
            load_fixture("status_post.json", DOMAIN)
        )

        client.account_update_credentials.return_value = Account.from_json(
            load_fixture("account.json", DOMAIN)
        )

        yield client


@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
    """Mock a config entry."""
    return MockConfigEntry(
        domain=DOMAIN,
        title="@trwnh@mastodon.social",
        data={
            CONF_BASE_URL: "https://mastodon.social",
            CONF_CLIENT_ID: "client_id",
            CONF_CLIENT_SECRET: "client_secret",
            CONF_ACCESS_TOKEN: "access_token",
        },
        entry_id="01J35M4AH9HYRC2V0G6RNVNWJH",
        unique_id="trwnh_mastodon_social",
        version=1,
        minor_version=2,
    )
