"""Permission prefix validation."""
from app.permissions.definitions import ALL_PERMISSIONS, PERMISSION_PREFIXES


def test_all_permissions_use_experience_prefix():
    assert ALL_PERMISSIONS
    for perm in ALL_PERMISSIONS:
        assert perm.startswith("experience."), perm


def test_prefixes_documented():
    assert "experience." in PERMISSION_PREFIXES
    assert "experience.workspaces." in PERMISSION_PREFIXES
    assert "experience.sites." in PERMISSION_PREFIXES
    assert "experience.pages." in PERMISSION_PREFIXES
    assert "experience.domains." in PERMISSION_PREFIXES
    assert "experience.components." in PERMISSION_PREFIXES
    assert "experience.themes." in PERMISSION_PREFIXES
    assert "experience.layouts." in PERMISSION_PREFIXES
    assert "experience.templates." in PERMISSION_PREFIXES
    assert "experience.locales." in PERMISSION_PREFIXES
    assert "experience.media_refs." in PERMISSION_PREFIXES
    assert "experience.forms." in PERMISSION_PREFIXES
    assert "experience.surveys." in PERMISSION_PREFIXES
    assert "experience.appointments." in PERMISSION_PREFIXES
    assert "experience.bundles." in PERMISSION_PREFIXES
    assert "experience.feature_toggles." in PERMISSION_PREFIXES
    assert "experience.consumer_connectors." in PERMISSION_PREFIXES
    assert "experience.widgets." in PERMISSION_PREFIXES
    assert "experience.notify." in PERMISSION_PREFIXES
    assert "experience.analytics." in PERMISSION_PREFIXES
    assert "experience.ai_hooks." in PERMISSION_PREFIXES


def test_template_permission_crud_tree():
    from app.permissions.definitions import (
        TEMPLATES_CREATE,
        TEMPLATES_DELETE,
        TEMPLATES_MANAGE,
        TEMPLATES_UPDATE,
        TEMPLATES_VIEW,
    )

    for perm in (
        TEMPLATES_VIEW,
        TEMPLATES_CREATE,
        TEMPLATES_UPDATE,
        TEMPLATES_DELETE,
        TEMPLATES_MANAGE,
    ):
        assert perm in ALL_PERMISSIONS
        assert perm.startswith("experience.templates.")


def test_locales_and_media_permission_crud_trees():
    from app.permissions.definitions import (
        LOCALES_CREATE,
        LOCALES_DELETE,
        LOCALES_MANAGE,
        LOCALES_UPDATE,
        LOCALES_VIEW,
        MEDIA_REFS_CREATE,
        MEDIA_REFS_DELETE,
        MEDIA_REFS_MANAGE,
        MEDIA_REFS_UPDATE,
        MEDIA_REFS_VIEW,
    )

    for perm in (
        LOCALES_VIEW,
        LOCALES_CREATE,
        LOCALES_UPDATE,
        LOCALES_DELETE,
        LOCALES_MANAGE,
    ):
        assert perm in ALL_PERMISSIONS
        assert perm.startswith("experience.locales.")

    for perm in (
        MEDIA_REFS_VIEW,
        MEDIA_REFS_CREATE,
        MEDIA_REFS_UPDATE,
        MEDIA_REFS_DELETE,
        MEDIA_REFS_MANAGE,
    ):
        assert perm in ALL_PERMISSIONS
        assert perm.startswith("experience.media_refs.")


def test_forms_surveys_appointments_permission_trees():
    from app.permissions.definitions import (
        APPOINTMENTS_CREATE,
        APPOINTMENTS_MANAGE,
        APPOINTMENTS_VIEW,
        FORMS_CREATE,
        FORMS_MANAGE,
        FORMS_VIEW,
        SURVEYS_CREATE,
        SURVEYS_MANAGE,
        SURVEYS_VIEW,
    )

    for perm in (FORMS_VIEW, FORMS_CREATE, FORMS_MANAGE):
        assert perm in ALL_PERMISSIONS
        assert perm.startswith("experience.forms.")
    for perm in (SURVEYS_VIEW, SURVEYS_CREATE, SURVEYS_MANAGE):
        assert perm in ALL_PERMISSIONS
        assert perm.startswith("experience.surveys.")
    for perm in (APPOINTMENTS_VIEW, APPOINTMENTS_CREATE, APPOINTMENTS_MANAGE):
        assert perm in ALL_PERMISSIONS
        assert perm.startswith("experience.appointments.")


def test_bundles_and_feature_toggle_permission_trees():
    from app.permissions.definitions import (
        BUNDLES_CREATE, BUNDLES_DELETE, BUNDLES_MANAGE, BUNDLES_UPDATE, BUNDLES_VIEW,
        FEATURE_TOGGLES_CREATE, FEATURE_TOGGLES_DELETE, FEATURE_TOGGLES_MANAGE,
        FEATURE_TOGGLES_UPDATE, FEATURE_TOGGLES_VIEW,
    )
    for perm in (BUNDLES_VIEW, BUNDLES_CREATE, BUNDLES_UPDATE, BUNDLES_DELETE, BUNDLES_MANAGE):
        assert perm in ALL_PERMISSIONS
    for perm in (FEATURE_TOGGLES_VIEW, FEATURE_TOGGLES_CREATE, FEATURE_TOGGLES_UPDATE,
                 FEATURE_TOGGLES_DELETE, FEATURE_TOGGLES_MANAGE):
        assert perm in ALL_PERMISSIONS


def test_integrations_permission_trees():
    from app.permissions.definitions import (
        CONSUMER_CONNECTORS_CREATE,
        CONSUMER_CONNECTORS_MANAGE,
        CONSUMER_CONNECTORS_VIEW,
        NOTIFY_CREATE,
        NOTIFY_MANAGE,
        NOTIFY_VIEW,
        WIDGETS_CREATE,
        WIDGETS_MANAGE,
        WIDGETS_VIEW,
    )

    for perm in (CONSUMER_CONNECTORS_VIEW, CONSUMER_CONNECTORS_CREATE, CONSUMER_CONNECTORS_MANAGE):
        assert perm in ALL_PERMISSIONS
    for perm in (WIDGETS_VIEW, WIDGETS_CREATE, WIDGETS_MANAGE):
        assert perm in ALL_PERMISSIONS
    for perm in (NOTIFY_VIEW, NOTIFY_CREATE, NOTIFY_MANAGE):
        assert perm in ALL_PERMISSIONS
