# 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 worker tags and worker tag prefixes."""

from enum import StrEnum

from debusine.utils.tag_utils import CandidateTags, ProvenanceRestrictions


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

    #: Tags provided in dynamic worker metadata when a worker registers as
    #: active
    WORKER = "worker"
    #: Tags provided in static worker metadata by the Debusine admins
    ADMIN = "admin"
    #: Tags from database queries
    SYSTEM = "system"


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

    #: Tags from database queries
    SYSTEM = "system"
    #: Tags from workspace task configuration
    WORKSPACE = "workspace"


class TaskRequiredTags(CandidateTags[ProvenanceRequired]):
    """Candidate task-required tags."""


#: Prefix for worker build architecture
BUILD_ARCH_PREFIX = "worker:build-arch:"

#: Prefix for worker capability tags
CAP_PREFIX = "worker:cap:"

#: Prefix for worker classes
CLASS_PREFIX = "worker:class:"

#: Prefix and tags for worker type tags
WORKER_TYPE_PREFIX = "worker:type:"
WORKER_TYPE_CELERY = WORKER_TYPE_PREFIX + "celery"
WORKER_TYPE_EXTERNAL = WORKER_TYPE_PREFIX + "external"
WORKER_TYPE_SIGNING = WORKER_TYPE_PREFIX + "signing"
WORKER_TYPE_NOT_ASSIGNABLE = WORKER_TYPE_PREFIX + "not-assignable"

#: Prefix for worker executor tags
EXECUTOR_PREFIX = "worker:executor:"

#: Prefix for worker task availability tags
TASK_PREFIX = "worker:task:"

#: Restrictions applied when adding provided tags
restrictions_when_provided = ProvenanceRestrictions("worker-provided")
# worker:class:* tags are only allowed to be provided by admins
restrictions_when_provided.add_prefix(CLASS_PREFIX, [ProvenanceProvided.ADMIN])
# worker:type:* tags are only allowed to come from database queries
restrictions_when_provided.add_prefix(
    WORKER_TYPE_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("worker-required")
