# -*- coding: utf-8 -*-
# Copyright (c) 2017 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com

"""
Contains functions for efficiently validating a CZREL.
CZRELs are used to unambiguously and uniquely identify explicit and derived relationships between resources and other
entities we extract from a cloud domain.
"""


import re

from czid.czrn.validate import CZRN_PATTERN

CZREL_CAPTURE_REGEX = re.compile(f'^czrel>({CZRN_PATTERN})>([a-z0-9-]+)>({CZRN_PATTERN})')


def is_valid(identifier):
    """
    Determine if a given ID is a Relationship ID of the form czrel>subject>predicate>object

    Args:
        identifier (str): The ID value to check

    Returns:
        True if the ID is a relationship else False

    >>> is_valid('foobar')
    False

    >>> is_valid('czrel>czrn:aws:lambda:us-west-2:123456789012:function:my-function>invoked-by>czrn:aws:lambda:us-west-2:123456789012:function:my-other-function')
    True
    """
    if not identifier:
        return False
    relationship_match = CZREL_CAPTURE_REGEX.fullmatch(identifier)
    return relationship_match is not None
