"""Integration tests for ECMSecuritySync.

Requires a running ECM server at localhost:4000.
"""

import unittest

from ecmind_blue_client.ecm import ECM, ECMGroup, ECMSystemRole, ECMUser, ECMUserAttributes
from ecmind_blue_client.pool import SyncPoolClient
from tests._test_settings import ECM_SERVERS, ECM_USERNAME, ECM_PASSWORD


class TestEcmSecuritySync(unittest.TestCase):
    """Integration tests for ECMSecuritySync — requires localhost:4000."""

    @classmethod
    def setUpClass(cls):
        cls.client = SyncPoolClient(servers=ECM_SERVERS, username=ECM_USERNAME, password=ECM_PASSWORD)
        cls.ecm = ECM(cls.client)
        cls._roles = cls.ecm.security.roles()
        cls._users = cls.ecm.security.users()
        cls._user_groups = cls.ecm.security.user_groups("CB870C5C6E2A491EA853D7C36D61C06B")
        cls._user_groups_unknown = cls.ecm.security.user_groups("1ECC63AFD2B041679126ABC501AAE130")
        cls._groups = cls.ecm.security.groups()
        cls._group_standard = cls.ecm.security.group("STANDARD")
        cls._group_unknown = cls.ecm.security.group("NONEXISTENT_GROUP_XYZ")
        cls._group_members = cls.ecm.security.group_members("STANDARD")
        cls._group_members_unknown = cls.ecm.security.group_members("NONEXISTENT_GROUP_XYZ")
        cls._user_root = cls.ecm.security.user("ROOT")
        cls._user_unknown = cls.ecm.security.user("NONEXISTENT_USER_XYZ")

    @classmethod
    def tearDownClass(cls):
        del cls.client

    # ── roles ────────────────────────────────────────────────────────

    def test_roles_returns_list(self):
        self.assertIsInstance(self._roles, list)

    def test_roles_items_are_ecm_system_roles(self):
        for role in self._roles:
            self.assertIsInstance(role, ECMSystemRole)

    def test_roles_root_has_roles(self):
        self.assertGreater(len(self._roles), 0)

    def test_roles_root_has_admin_start(self):
        self.assertIn(ECMSystemRole.ADMIN_START, self._roles)

    # ── users ─────────────────────────────────────────────────────────

    def test_users_returns_list(self):
        self.assertIsInstance(self._users, list)

    def test_users_items_are_ecm_users(self):
        for user in self._users:
            self.assertIsInstance(user, ECMUser)

    def test_users_not_empty(self):
        self.assertGreater(len(self._users), 0)

    def test_users_have_guid(self):
        for user in self._users:
            self.assertGreater(len(user.guid), 0)

    # ── user_groups ───────────────────────────────────────────────────────────

    def test_user_groups_returns_list(self):
        self.assertIsInstance(self._user_groups, list)

    def test_user_groups_items_are_ecm_groups(self):
        for group in self._user_groups:
            self.assertIsInstance(group, ECMGroup)

    def test_user_groups_have_name(self):
        for group in self._user_groups:
            self.assertGreater(len(group.name), 0)

    def test_user_groups_no_groups_for_unknown_user(self):
        self.assertIsInstance(self._user_groups_unknown, list)

    # ── groups ────────────────────────────────────────────────────────────────

    def test_groups_returns_list(self):
        self.assertIsInstance(self._groups, list)

    def test_groups_not_empty(self):
        self.assertGreater(len(self._groups), 0)

    def test_groups_items_are_ecm_groups(self):
        for group in self._groups:
            self.assertIsInstance(group, ECMGroup)

    def test_groups_have_name(self):
        for group in self._groups:
            self.assertGreater(len(group.name), 0)

    # ── group ─────────────────────────────────────────────────────────────────

    def test_group_returns_instance(self):
        self.assertIsInstance(self._group_standard, ECMGroup)

    def test_group_name_matches(self):
        assert self._group_standard is not None
        self.assertEqual(self._group_standard.name, "STANDARD")

    def test_group_unknown_returns_none(self):
        self.assertIsNone(self._group_unknown)

    # ── group_members ─────────────────────────────────────────────────────────

    def test_group_members_returns_list(self):
        self.assertIsInstance(self._group_members, list)

    def test_group_members_not_empty(self):
        self.assertGreater(len(self._group_members), 0)

    def test_group_members_items_are_ecm_users(self):
        for user in self._group_members:
            self.assertIsInstance(user, ECMUser)

    def test_group_members_unknown_group_returns_empty(self):
        self.assertIsInstance(self._group_members_unknown, list)

    # ── get_user_attributes ───────────────────────────────────────────────────

    def test_get_user_attributes_returns_instance(self):
        self.assertIsInstance(self._user_root, ECMUserAttributes)

    def test_get_user_attributes_has_guid(self):
        assert self._user_root is not None
        self.assertGreater(len(self._user_root.guid), 0)

    def test_get_user_attributes_username_matches(self):
        assert self._user_root is not None
        self.assertEqual(self._user_root.username, "ROOT")

    def test_get_user_attributes_unknown_user_returns_none(self):
        self.assertIsNone(self._user_unknown)
