# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""
Tests for CommCoach feature registration module.
"""

import pytest
from ..mainCommcoach import (
    FEATURE_CODE, FEATURE_LABEL, FEATURE_ICON,
    UI_OBJECTS, DATA_OBJECTS, RESOURCE_OBJECTS, TEMPLATE_ROLES,
    getFeatureDefinition, getUiObjects, getResourceObjects, getDataObjects, getTemplateRoles,
)


class TestFeatureMetadata:
    def test_featureCode(self):
        assert FEATURE_CODE == "commcoach"

    def test_featureLabel(self):
        assert "de" in FEATURE_LABEL
        assert "en" in FEATURE_LABEL
        assert "Coach" in FEATURE_LABEL["de"]

    def test_featureIcon(self):
        assert FEATURE_ICON.startswith("mdi-")


class TestFeatureDefinition:
    def test_structure(self):
        defn = getFeatureDefinition()
        assert defn["code"] == "commcoach"
        assert "label" in defn
        assert "icon" in defn
        assert defn["autoCreateInstance"] is True


class TestRbacObjects:
    def test_uiObjectsExist(self):
        objs = getUiObjects()
        assert len(objs) >= 4
        keys = [o["objectKey"] for o in objs]
        assert "ui.feature.commcoach.dashboard" in keys
        assert "ui.feature.commcoach.coaching" in keys
        assert "ui.feature.commcoach.dossier" in keys
        assert "ui.feature.commcoach.settings" in keys

    def test_uiObjectsHaveLabels(self):
        for obj in getUiObjects():
            assert "label" in obj
            assert "de" in obj["label"]

    def test_dataObjectsExist(self):
        objs = getDataObjects()
        assert len(objs) >= 7
        keys = [o["objectKey"] for o in objs]
        assert "data.feature.commcoach.CoachingContext" in keys
        assert "data.feature.commcoach.CoachingSession" in keys
        assert "data.feature.commcoach.CoachingMessage" in keys
        assert "data.feature.commcoach.CoachingTask" in keys
        assert "data.feature.commcoach.CoachingScore" in keys
        assert "data.feature.commcoach.*" in keys

    def test_resourceObjectsExist(self):
        objs = getResourceObjects()
        assert len(objs) >= 5
        keys = [o["objectKey"] for o in objs]
        assert "resource.feature.commcoach.context.create" in keys
        assert "resource.feature.commcoach.session.start" in keys
        assert "resource.feature.commcoach.task.manage" in keys

    def test_resourceObjectsHaveMeta(self):
        for obj in getResourceObjects():
            assert "meta" in obj
            assert "endpoint" in obj["meta"]
            assert "method" in obj["meta"]


class TestTemplateRoles:
    def test_rolesExist(self):
        roles = getTemplateRoles()
        assert len(roles) >= 1
        labels = [r["roleLabel"] for r in roles]
        assert "commcoach-user" in labels

    def test_userRoleHasStrictOwnership(self):
        """Verify that commcoach-user role uses MY (m) access, not ALL (a)."""
        roles = getTemplateRoles()
        userRole = next(r for r in roles if r["roleLabel"] == "commcoach-user")
        dataRules = [r for r in userRole["accessRules"] if r["context"] == "DATA"]
        for rule in dataRules:
            if rule.get("read"):
                assert rule["read"] == "m", f"DATA rule for {rule.get('item')} uses '{rule['read']}' instead of 'm' (MY)"

    def test_roleHasDescription(self):
        for role in getTemplateRoles():
            assert "description" in role
            assert "de" in role["description"]

    def test_roleHasAccessRules(self):
        for role in getTemplateRoles():
            assert len(role["accessRules"]) > 0
            contexts = set(r["context"] for r in role["accessRules"])
            assert "UI" in contexts
            assert "DATA" in contexts
            assert "RESOURCE" in contexts
