# #region Test.AppTimezoneEdge [C:3] [TYPE Module] [SEMANTICS test,timezone,format,offset,edge]
# @BRIEF Edge-case coverage for core/timezone.py — format_timezone_offset fallback.
# @RELATION BINDS_TO -> [Timezone]
# @TEST_EDGE: empty_strftime -> Fallback to '+00:00' when strftime returns empty
# @TEST_EDGE: invalid_timezone -> validate_timezone rejects invalid tz names
# @TEST_EDGE: missing_env_var -> Default timezone when env var not set

from datetime import timezone, timedelta
from unittest.mock import patch
import pytest


class TestFormatTimezoneOffsetEdge:
    """Cover line 90 in format_timezone_offset — fallback when strftime returns empty."""

    def test_offset_returns_expected_format(self):
        from src.core.timezone import format_timezone_offset, invalidate_timezone_cache

        invalidate_timezone_cache()
        result = format_timezone_offset()
        # Should return something like "+03:00" or "+05:00"
        assert isinstance(result, str)
        assert ":" in result

    def test_offset_with_zero_offset(self):
        """Simulate UTC timezone offset returning "+00:00"."""
        import src.core.timezone as tz_mod

        # Mock now() to return a UTC time
        fake_dt = type("FakeDT", (), {
            "strftime": lambda self, fmt: "+0000",
        })()

        with patch.object(tz_mod, "now", return_value=fake_dt):
            result = tz_mod.format_timezone_offset()
            assert result == "+00:00"

    def test_offset_with_empty_strftime(self):
        """When strftime returns empty, fallback to '+00:00'."""
        import src.core.timezone as tz_mod

        fake_dt = type("FakeDT", (), {
            "strftime": lambda self, fmt: "",
        })()

        with patch.object(tz_mod, "now", return_value=fake_dt):
            result = tz_mod.format_timezone_offset()
            assert result == "+00:00"

    def test_now_returns_aware(self):
        from src.core.timezone import now

        dt = now()
        assert dt.tzinfo is not None

    def test_localize_with_none(self):
        from src.core.timezone import localize

        assert localize(None) is None

    def test_validate_timezone_with_invalid(self):
        from src.core.timezone import validate_timezone

        assert validate_timezone("Not/A/Timezone") is False

    def test_validate_timezone_with_none(self):
        from src.core.timezone import validate_timezone

        assert validate_timezone(None) is False  # type: ignore

    def test_invalidate_and_reget(self):
        import src.core.timezone as tz_mod

        tz_mod._APP_TZ_CACHE = None
        tz_mod.invalidate_timezone_cache()
        assert tz_mod._APP_TZ_CACHE is None
        tz1 = tz_mod.get_app_timezone()
        assert tz_mod._APP_TZ_CACHE is tz1
        tz_mod.invalidate_timezone_cache()
        assert tz_mod._APP_TZ_CACHE is None

    def test_get_default_tz_name_env(self):
        from src.core.timezone import _get_default_tz_name

        with patch("src.core.timezone.os.getenv", return_value="America/New_York"):
            assert _get_default_tz_name() == "America/New_York"

    def test_get_default_tz_name_default(self):
        from src.core.timezone import _get_default_tz_name

        with patch("src.core.timezone.os.getenv", return_value="Europe/Moscow"):
            assert _get_default_tz_name() == "Europe/Moscow"
# #endregion Test.AppTimezoneEdge
