# Copyright © The Debusine Developers
# See the AUTHORS file at the top-level directory of this distribution
#
# This file is part of Debusine. It is subject to the license terms
# in the LICENSE file found in the top-level directory of this
# distribution. No part of Debusine, including this file, may be copied,
# modified, propagated, or distributed except according to the terms
# contained in the LICENSE file.
"""Catalog of known task tags and task tag prefixes."""

from enum import StrEnum

from debusine.utils.tag_utils import CandidateTags, ProvenanceRestrictions


class ProvenanceProvided(StrEnum):
    """Provenances for task-provided task tags."""

    #: Tags from database queries
    SYSTEM = "system"
    #: Tags from user data
    USER = "user"
    #: Tags from task configuration defined in the workspace
    WORKSPACE = "workspace"


class ProvenanceRequired(StrEnum):
    """Provenances for worker-required task tags."""

    #: Tags required by debusine admins in static worker metadata
    ADMIN = "system"


class TaskProvidedTags(CandidateTags[ProvenanceProvided]):
    """Candidate task-provided tags."""


#: Prefix for scope tags
SCOPE_PREFIX = "task:scope:"
#: Prefix for scope tags
WORKSPACE_PREFIX = "task:workspace:"
#: Prefix for user groups
GROUP_PREFIX = "task:group:"
#: Prefix for source packages
SOURCE_PACKAGE_PREFIX = "task:source-package:"

#: Restrictions applied when adding provided tags
restrictions_when_provided = ProvenanceRestrictions("task-provided")
# task:group:* tags are only allowed to come from database queries
restrictions_when_provided.add_prefix(GROUP_PREFIX, [ProvenanceProvided.SYSTEM])
# task:scope:* tags are only allowed to come from database queries
restrictions_when_provided.add_prefix(SCOPE_PREFIX, [ProvenanceProvided.SYSTEM])
# task:source-package:* tags are only allowed to come from database queries
restrictions_when_provided.add_prefix(
    SOURCE_PACKAGE_PREFIX, [ProvenanceProvided.SYSTEM]
)
# task:workspace:* tags are only allowed to come from database queries
restrictions_when_provided.add_prefix(
    WORKSPACE_PREFIX, [ProvenanceProvided.SYSTEM]
)

#: Restrictions applied when adding required tags
# Use empty restrictions, since adding required tags is not a security
# sensitive operation, but removing them is
restrictions_when_required = ProvenanceRestrictions("task-required")
