"""Test fixtures for Teslemetry component."""

from collections.abc import Generator
from copy import deepcopy
from typing import Any
from unittest.mock import AsyncMock, patch

import pytest
from teslemetry_stream.stream import recursive_match

from homeassistant.components.teslemetry.const import TOKEN_URL

from .const import (
    COMMAND_OK,
    ENERGY_HISTORY,
    LIVE_STATUS,
    METADATA,
    METADATA_LEGACY,
    PRODUCTS,
    SITE_INFO,
    VEHICLE_DATA,
    WAKE_UP_ONLINE,
)

from tests.test_util.aiohttp import AiohttpClientMocker


@pytest.fixture
def mock_setup_entry():
    """Mock Teslemetry async_setup_entry method."""
    with patch(
        "homeassistant.components.teslemetry.async_setup_entry", return_value=True
    ) as mock_async_setup_entry:
        yield mock_async_setup_entry


@pytest.fixture
def mock_token_response() -> dict[str, Any]:
    """Return a mock OAuth token response."""
    return {
        "refresh_token": "mock-refresh-token",
        "access_token": "mock-access-token",
        "type": "Bearer",
        "expires_in": 60,
    }


@pytest.fixture
def mock_token_post(
    aioclient_mock: AiohttpClientMocker, mock_token_response: dict[str, Any]
) -> None:
    """Mock the OAuth token endpoint."""
    aioclient_mock.post(TOKEN_URL, json=mock_token_response)


@pytest.fixture(autouse=True)
def mock_metadata():
    """Mock Tesla Fleet Api metadata method."""
    with patch(
        "tesla_fleet_api.teslemetry.Teslemetry.metadata", return_value=METADATA
    ) as mock_products:
        yield mock_products


@pytest.fixture(autouse=True)
def mock_products():
    """Mock Tesla Fleet Api products method."""
    with patch(
        "tesla_fleet_api.teslemetry.Teslemetry.products", return_value=PRODUCTS
    ) as mock_products:
        yield mock_products


@pytest.fixture(autouse=True)
def mock_vehicle_data() -> Generator[AsyncMock]:
    """Mock Tesla Fleet API Vehicle Specific vehicle_data method."""
    with patch(
        "tesla_fleet_api.teslemetry.Vehicle.vehicle_data",
        return_value=VEHICLE_DATA,
    ) as mock_vehicle_data:
        yield mock_vehicle_data


@pytest.fixture
def mock_legacy():
    """Mock Tesla Fleet Api products method."""
    with patch(
        "tesla_fleet_api.teslemetry.Teslemetry.metadata", return_value=METADATA_LEGACY
    ) as mock_products:
        yield mock_products


@pytest.fixture(autouse=True)
def mock_wake_up():
    """Mock Tesla Fleet API Vehicle Specific wake_up method."""
    with patch(
        "tesla_fleet_api.teslemetry.Vehicle.wake_up",
        return_value=WAKE_UP_ONLINE,
    ) as mock_wake_up:
        yield mock_wake_up


@pytest.fixture(autouse=True)
def mock_vehicle() -> Generator[AsyncMock]:
    """Mock Tesla Fleet API Vehicle Specific vehicle method."""
    with patch(
        "tesla_fleet_api.teslemetry.Vehicle.vehicle",
        return_value=WAKE_UP_ONLINE,
    ) as mock_vehicle:
        yield mock_vehicle


@pytest.fixture(autouse=True)
def mock_request():
    """Mock Tesla Fleet API Vehicle Specific class."""
    with patch(
        "tesla_fleet_api.teslemetry.Teslemetry._request",
        return_value=COMMAND_OK,
    ) as mock_request:
        yield mock_request


@pytest.fixture(autouse=True)
def mock_live_status():
    """Mock Teslemetry Energy Specific live_status method."""
    with patch(
        "tesla_fleet_api.tesla.energysite.EnergySite.live_status",
        side_effect=lambda: deepcopy(LIVE_STATUS),
    ) as mock_live_status:
        yield mock_live_status


@pytest.fixture(autouse=True)
def mock_site_info():
    """Mock Teslemetry Energy Specific site_info method."""
    with patch(
        "tesla_fleet_api.tesla.energysite.EnergySite.site_info",
        side_effect=lambda: deepcopy(SITE_INFO),
    ) as mock_live_status:
        yield mock_live_status


@pytest.fixture(autouse=True)
def mock_energy_history():
    """Mock Teslemetry Energy Specific site_info method."""
    with patch(
        "tesla_fleet_api.tesla.energysite.EnergySite.energy_history",
        return_value=ENERGY_HISTORY,
    ) as mock_live_status:
        yield mock_live_status


@pytest.fixture(autouse=True)
def mock_stream_listen():
    """Mock Teslemetry Stream listen method."""
    with patch(
        "teslemetry_stream.TeslemetryStream.listen",
    ) as mock_stream_listen:
        yield mock_stream_listen


@pytest.fixture(autouse=True)
def mock_add_listener():
    """Mock Teslemetry Stream add listener method."""
    with patch(
        "teslemetry_stream.TeslemetryStream.async_add_listener",
    ) as mock_add_listener:
        mock_add_listener.listeners = []

        def unsubscribe() -> None:
            return

        def side_effect(callback, filters):
            mock_add_listener.listeners.append((callback, filters))
            return unsubscribe

        def send(event) -> None:
            for listener, filters in mock_add_listener.listeners:
                if recursive_match(filters, event):
                    listener(event)

        mock_add_listener.send = send
        mock_add_listener.side_effect = side_effect
        yield mock_add_listener


@pytest.fixture(autouse=True)
def mock_stream_get_config():
    """Mock Teslemetry Stream listen method."""
    with patch(
        "teslemetry_stream.TeslemetryStreamVehicle.get_config",
    ) as mock_stream_get_config:
        yield mock_stream_get_config


@pytest.fixture(autouse=True)
def mock_stream_update_config():
    """Mock Teslemetry Stream listen method."""
    with patch(
        "teslemetry_stream.TeslemetryStreamVehicle.update_config",
    ) as mock_stream_update_config:
        yield mock_stream_update_config


@pytest.fixture(autouse=True)
def mock_stream_connected():
    """Mock Teslemetry Stream listen method."""
    with patch(
        "teslemetry_stream.TeslemetryStream.connected",
        return_value=True,
    ) as mock_stream_connected:
        yield mock_stream_connected
