# 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
import typing

_AVAILABLE_PREFIXES = '!&'


class PartitionType(str):
    accounts: 'PartitionType'
    services: 'PartitionType'
    resources: 'PartitionType'
    product_family: 'PartitionType'
    usage_type: 'PartitionType'
    operation: 'PartitionType'
    region: 'PartitionType'
    k8s_cluster: 'PartitionType'
    k8s_pod: 'PartitionType'
    k8s_workload: 'PartitionType'
    k8s_namespace: 'PartitionType'
    cloud_local_resource_id: 'PartitionType'
    cloud_provider: 'PartitionType'
    billing_entity: 'PartitionType'
    line_item_type: 'PartitionType'
    description: 'PartitionType'
    request_type: 'PartitionType'
    transfer_type: 'PartitionType'
    pricing_term: 'PartitionType'
    invoice_id: 'PartitionType'
    payer_account_id: 'PartitionType'
    billing_period: 'PartitionType'
    committed_use_subscription: 'PartitionType'
    billing_connection_id: 'PartitionType'
    pricing_unit: 'PartitionType'
    usage_day: 'PartitionType'
    earliest_resource_date: 'PartitionType'
    tag_date: 'PartitionType'
    latest_resource_date: 'PartitionType'

    @classmethod
    def for_tag(cls, tag_value: str) -> 'PartitionType':
        return cls(f'tag:{tag_value}')

    @classmethod
    def for_k8s_label(cls, label_value: str) -> 'PartitionType':
        return cls(f'k8slabel:{label_value}')

    @classmethod
    def for_cost_context(cls, cost_context_value: str) -> 'PartitionType':
        return cls(f'costcontext:{cost_context_value}')

    def _prefix(self, p: str) -> 'PartitionType':
        x = self.lstrip(_AVAILABLE_PREFIXES)
        prefixes = p + self[: len(self) - len(x)]
        return PartitionType(''.join(c if c in prefixes else '' for c in _AVAILABLE_PREFIXES) + x)

    def exclude(self) -> 'PartitionType':
        return self._prefix('!')

    def use_contains(self) -> 'PartitionType':
        return self._prefix('&')


for v, t in typing.get_type_hints(PartitionType).items():
    if t == PartitionType and not hasattr(PartitionType, v):
        setattr(PartitionType, v, PartitionType(v))
