"""Tests for pybyd.config and pybyd.client basics."""

from __future__ import annotations

import pytest

from pybyd.config import BydConfig, DeviceProfile
from pybyd.exceptions import BydError


class TestDeviceProfile:
    def test_defaults(self) -> None:
        dp = DeviceProfile()
        assert dp.ostype == "and"
        assert dp.imei == "BANGCLE01234"
        assert dp.mobile_brand == "XIAOMI"

    def test_custom(self) -> None:
        dp = DeviceProfile(mobile_brand="Samsung", mobile_model="Galaxy S24")
        assert dp.mobile_brand == "Samsung"
        assert dp.mobile_model == "Galaxy S24"

    def test_frozen(self) -> None:
        dp = DeviceProfile()
        with pytest.raises(AttributeError):
            dp.ostype = "ios"  # type: ignore[misc]


class TestBydConfig:
    def test_required_fields(self) -> None:
        config = BydConfig(username="user", password="pass")
        assert config.username == "user"
        assert config.password == "pass"
        assert config.country_code == "NL"

    def test_defaults(self) -> None:
        config = BydConfig(username="u", password="p")
        assert config.base_url == "https://dilinkappoversea-eu.byd.auto"
        assert config.language == "en"
        assert config.app_version == "2.2.1"

    def test_frozen(self) -> None:
        config = BydConfig(username="u", password="p")
        with pytest.raises(AttributeError):
            config.username = "other"  # type: ignore[misc]

    def test_from_env(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setenv("BYD_USERNAME", "env_user")
        monkeypatch.setenv("BYD_PASSWORD", "env_pass")
        monkeypatch.setenv("BYD_COUNTRY_CODE", "DE")
        config = BydConfig.from_env()
        assert config.username == "env_user"
        assert config.password == "env_pass"
        assert config.country_code == "DE"

    def test_from_env_overrides(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setenv("BYD_USERNAME", "env_user")
        monkeypatch.setenv("BYD_PASSWORD", "env_pass")
        config = BydConfig.from_env(country_code="FR")
        assert config.country_code == "FR"

    def test_from_env_device_override(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setenv("BYD_USERNAME", "u")
        monkeypatch.setenv("BYD_PASSWORD", "p")
        monkeypatch.setenv("BYD_MOBILE_BRAND", "Samsung")
        config = BydConfig.from_env()
        assert config.device.mobile_brand == "Samsung"


class TestBydClientInit:
    @pytest.mark.asyncio
    async def test_requires_context_manager(self) -> None:
        from pybyd.client import BydClient

        config = BydConfig(username="u", password="p")
        client = BydClient(config)
        with pytest.raises(BydError, match="not initialized"):
            client._require_transport()

    @pytest.mark.asyncio
    async def test_requires_login(self) -> None:
        from pybyd.client import BydClient

        config = BydConfig(username="u", password="p")
        async with BydClient(config) as client:
            with pytest.raises(BydError, match="Not logged in"):
                await client.get_vehicles()
