# Copyright (c) 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 re

from botocore.config import Config

AWS_ACCOUNT_ID_REGEX = re.compile(r"^\d{12}$")
# SNOWFLAKE_ACCOUNT_ID_REGEX = re.compile(r"[a-zA-Z0-9_].*\.[\w].*")  # DISABLED - Snowflake connections no longer supported
UUID_REGEX = re.compile(r"[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}\Z", re.I)
CZOPS_WORKING_DIR = "~/.cloudzero/"

# Copyright (c) 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
#
LINKS_TO_IGNORE = ["legacy", "cloudtrail_owner", "audit"]

AUTH_TYPE_NONE = "NO-AUTH"
AUTH_TYPE_APIKEY = "APIKEY"
AUTH_TYPE_CLOUDZERO = "CLOUDZERO"
VALID_AUTH_TYPES = [AUTH_TYPE_APIKEY]

CLOUD_TYPE_AWS = "AWS"
CLOUD_TYPE_AZURE = "AZURE"
CLOUD_TYPE_GCP = "GCP"
VALID_CLOUD_TYPES = [CLOUD_TYPE_AWS, CLOUD_TYPE_AZURE, CLOUD_TYPE_GCP]

SECONDS_PER_YEAR = 31536000
SECONDS_PER_WEEK = 604800
SECONDS_PER_DAY = 86400
SECONDS_PER_HOUR = 3600

# Copyright (c) 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
#
# key is based off of what is in link_health
LINK_HEALTH_TYPES_MAP = {
    "master_payer": {"code": "P", "name": "Payer"},
    "resource_owner": {"code": "R", "name": "Resource"},
    "cloudtrail_owner": {"code": "T", "name": "CloudTrail"},
    "audit": {"code": "A", "name": "Audit"},
    "legacy": {"code": "L", "name": "Legacy"},
    "resource_collector": {"code": "RC", "name": "Resource Collection"},
    "container_insights": {"code": "K", "name": "K8s"},
    "uca": {"code": "U", "name": "UCA Generator"},
}

CLOUD_ACCOUNT_TYPES_MAP = {
    "payer": {"code": "P", "name": "Payer"},
    "resource": {"code": "R", "name": "Resource"},
    "management": {"code": "M", "name": "Management"},
    "audit": {"code": "A", "name": "Audit"},
}

CONNECTION_TYPES = {
    "payer": {
        "code": "P",
        "name": "Payer",
        "description": "Payer",
        "type_key": "COST",
    },
    "resource": {
        "code": "R",
        "name": "Resource",
        "description": "Resource",
        "type_key": "RESOURCE",
    },
}

CONNECTION_PROPERTIES = {
    "SPLIT_COST_ALLOCATION_DATA": {
        "code": "S",
        "name": "ECS Allocation",
        "description": "Split Cost Allocation for ECS",
        "enabled_key": "SPLIT_COST_ALLOCATION_DATA",
        "provider": "AWS",
    },
    "ANYCOST_CONNECTION": {
        "code": "AC",
        "name": "AnyCost",
        "description": "AnyCost Connection",
        "enabled_key": False,
        "provisioning_keywords": ["!", "cfn", "manual", "auto-link"],
        "provider": "AWS",
    },
    "MANAGEMENT_ACCOUNT": {
        "code": "M",
        "name": "Management",
        "description": "Management Account",
        "enabled_key": None,  # Deprecated in v4 - no longer used
        "provider": "AWS",
    },
    "AUDIT_ACCOUNT": {
        "code": "A",
        "name": "Audit",
        "description": "Audit Account",
        "enabled_key": "audit",
        "provider": "AWS",
    },
    "RESOURCE_COLLECTION_ENABLED": {
        "code": "RC",
        "name": "Resource Collection",
        "description": "Resource Collection",
        "enabled_key": "resource_collector_configurations",
        "provider": "AWS",
    },
    "CONTAINER_INSIGHTS_DETECTED": {
        "code": "K",
        "name": "K8s",
        "description": "K8s Container Insights",
        "enabled_key": "container_insights",
        "provider": "AWS",
    },
    "UCA_S3_INGEST": {
        "code": "U",
        "name": "UCATS",
        "description": "UCA Telemetry Services",
        "enabled_key": "uca_enabled",
        "provider": "AWS",
    },
}


def common_boto3_client_config():
    """
    Common Boto3 Client Config

    General settings we should use for all Boto3 client connections.

    Things this client config does:
    1. Configure retry adaptive mode and set max attempts to 10

    Returns:
        Config (botocore.config)

    """
    config = Config(retries=dict(max_attempts=10, mode="adaptive"))
    return config


NO_ACCOUNT_NAME = "(None)"
NO_ORG_NAME = "(None)"

# Account-link endpoint and reactor identity for `czops connection link`.
# The endpoint is the same one the CloudFormation custom resource in customer
# accounts calls during onboarding: it accepts unauthenticated POSTs by design
# (customer stacks hold no CloudZero credentials) and the backend validates the
# payload by assuming the submitted role with the submitted external ID.
# REACTOR_ID identifies the production reactor that owns linked accounts; it is
# fixed for the production environment this CLI targets.
ACCOUNT_LINK_ENDPOINT = "https://api.cloudzero.com/accounts/v1/link"
ACCOUNT_LINK_REACTOR_ID = "f7a07d02-d509-4e8d-9fb9-69c7985a6bd8"

# Providers that currently support healthchecking
HEALTHCHECK_SUPPORTED_PROVIDERS = ["AWS"]
