"""
Unit tests for configurable default settings
"""

import unittest
from unittest.mock import patch
import os
import django

os.environ["DJANGO_SETTINGS_MODULE"] = "ivatar.settings"
django.setup()

from django.test import TestCase


class ConfigurableDefaultsTestCase(TestCase):
    """
    Test cases for configurable default settings for gravatarproxy, gravatarredirect, and forcedefault
    """

    def test_config_imports_successfully(self):
        """
        Test that the new configuration options can be imported successfully
        """
        try:
            from ivatar.settings import (
                DEFAULT_GRAVATARPROXY,
                DEFAULT_GRAVATARREDIRECT,
                FORCEDEFAULT,
            )

            # Test that they have the expected default values
            self.assertTrue(isinstance(DEFAULT_GRAVATARPROXY, bool))
            self.assertTrue(isinstance(DEFAULT_GRAVATARREDIRECT, bool))
            self.assertTrue(isinstance(FORCEDEFAULT, bool))
        except ImportError as e:
            self.fail(f"Failed to import configuration settings: {e}")

    def test_views_imports_config_successfully(self):
        """
        Test that views.py can import the new configuration options
        """
        try:
            from ivatar.views import (
                DEFAULT_GRAVATARPROXY,
                DEFAULT_GRAVATARREDIRECT,
                FORCEDEFAULT,
            )

            # Test that they have the expected default values
            self.assertTrue(isinstance(DEFAULT_GRAVATARPROXY, bool))
            self.assertTrue(isinstance(DEFAULT_GRAVATARREDIRECT, bool))
            self.assertTrue(isinstance(FORCEDEFAULT, bool))
        except ImportError as e:
            self.fail(f"Failed to import configuration settings in views: {e}")

    @patch("ivatar.settings.DEFAULT_GRAVATARPROXY", False)
    @patch("ivatar.settings.DEFAULT_GRAVATARREDIRECT", True)
    @patch("ivatar.settings.FORCEDEFAULT", True)
    def test_config_values_can_be_overridden(self):
        """
        Test that configuration values can be overridden (mocked for testing)
        """
        from ivatar import settings

        # These should reflect the patched values
        self.assertFalse(settings.DEFAULT_GRAVATARPROXY)
        self.assertTrue(settings.DEFAULT_GRAVATARREDIRECT)
        self.assertTrue(settings.FORCEDEFAULT)

    def test_default_values_are_correct(self):
        """
        Test that the default values match the issue requirements
        """
        from ivatar.settings import (
            DEFAULT_GRAVATARPROXY,
            DEFAULT_GRAVATARREDIRECT,
            FORCEDEFAULT,
        )

        # Based on the issue, these should be the defaults for public instances
        self.assertTrue(
            DEFAULT_GRAVATARPROXY
        )  # Default should be True for public instances
        self.assertFalse(DEFAULT_GRAVATARREDIRECT)  # Default should be False
        self.assertFalse(FORCEDEFAULT)  # Default should be False


if __name__ == "__main__":
    unittest.main()
