"""
Tests for Themis admin configuration.
"""
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.test import TestCase

from themis.admin import UserAPIKeyAdmin, UserProfileAdmin
from themis.models import UserAPIKey, UserProfile

User = get_user_model()


class AdminRegistrationTest(TestCase):
    """Tests that models are registered with Django admin."""

    def test_user_profile_registered(self):
        """UserProfile is registered with the admin site."""
        self.assertIn(UserProfile, admin.site._registry)

    def test_user_api_key_registered(self):
        """UserAPIKey is registered with the admin site."""
        self.assertIn(UserAPIKey, admin.site._registry)

    def test_user_profile_admin_class(self):
        """UserProfile uses the correct admin class."""
        self.assertIsInstance(admin.site._registry[UserProfile], UserProfileAdmin)

    def test_user_api_key_admin_class(self):
        """UserAPIKey uses the correct admin class."""
        self.assertIsInstance(admin.site._registry[UserAPIKey], UserAPIKeyAdmin)


class UserProfileAdminTest(TestCase):
    """Tests for UserProfileAdmin configuration."""

    def setUp(self):
        self.admin_class = UserProfileAdmin

    def test_list_display(self):
        """list_display includes expected fields."""
        expected = [
            "user",
            "home_timezone",
            "theme_mode",
            "theme_name",
            "notifications_enabled",
            "is_traveling",
            "created_at",
        ]
        self.assertEqual(self.admin_class.list_display, expected)

    def test_list_filter(self):
        """list_filter includes expected fields."""
        self.assertIn("theme_mode", self.admin_class.list_filter)
        self.assertIn("date_format", self.admin_class.list_filter)

    def test_search_fields(self):
        """search_fields includes user-related fields."""
        self.assertIn("user__username", self.admin_class.search_fields)
        self.assertIn("user__email", self.admin_class.search_fields)

    def test_readonly_fields(self):
        """Read-only fields include computed properties and timestamps."""
        self.assertIn("created_at", self.admin_class.readonly_fields)
        self.assertIn("updated_at", self.admin_class.readonly_fields)
        self.assertIn("effective_timezone", self.admin_class.readonly_fields)
        self.assertIn("is_traveling", self.admin_class.readonly_fields)

    def test_raw_id_fields(self):
        """user is a raw_id field for performance."""
        self.assertIn("user", self.admin_class.raw_id_fields)

    def test_fieldsets_defined(self):
        """Admin has fieldsets configured."""
        self.assertIsNotNone(self.admin_class.fieldsets)
        self.assertGreater(len(self.admin_class.fieldsets), 0)


class UserAPIKeyAdminTest(TestCase):
    """Tests for UserAPIKeyAdmin configuration."""

    def setUp(self):
        self.admin_class = UserAPIKeyAdmin

    def test_list_display(self):
        """list_display includes expected fields."""
        expected = [
            "service_name",
            "key_type",
            "user",
            "is_active",
            "last_used_at",
            "created_at",
        ]
        self.assertEqual(self.admin_class.list_display, expected)

    def test_list_filter(self):
        """list_filter includes expected fields."""
        self.assertIn("key_type", self.admin_class.list_filter)
        self.assertIn("is_active", self.admin_class.list_filter)

    def test_search_fields(self):
        """search_fields includes service and user fields."""
        self.assertIn("service_name", self.admin_class.search_fields)
        self.assertIn("user__username", self.admin_class.search_fields)

    def test_readonly_fields(self):
        """Read-only fields include id, masked_value, and timestamps."""
        self.assertIn("id", self.admin_class.readonly_fields)
        self.assertIn("masked_value", self.admin_class.readonly_fields)
        self.assertIn("created_at", self.admin_class.readonly_fields)
        self.assertIn("updated_at", self.admin_class.readonly_fields)

    def test_raw_id_fields(self):
        """user is a raw_id field for performance."""
        self.assertIn("user", self.admin_class.raw_id_fields)

    def test_fieldsets_defined(self):
        """Admin has fieldsets configured."""
        self.assertIsNotNone(self.admin_class.fieldsets)
        self.assertGreater(len(self.admin_class.fieldsets), 0)

    def test_encrypted_value_not_in_fieldsets(self):
        """encrypted_value is not directly exposed in fieldsets."""
        all_fields = []
        for name, options in self.admin_class.fieldsets:
            all_fields.extend(options.get("fields", []))
        self.assertNotIn("encrypted_value", all_fields)
