# Copyright (c) 2022 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 os
import warnings
from dataclasses import dataclass
from typing import Optional, List, Mapping, Iterable, Any
from deprecated import deprecated

import czc.internal_api.client as client
from czc.type_util import deserializable
from voluptuous import All, Any, SetTo, Required, Maybe, Schema, REMOVE_EXTRA

from src.common.helpers import NonEmptyString
from .types import BillingReportFormat, CloudAccountInfo

DEPRECATION_MESSAGE = 'Please use the typed version of this interface in billing_connections_typed_client instead.'
BILLING_CONNECTIONS_IAPI = {
    'namespace': lambda _: os.environ.get('BILLING_CONNECTIONS_NAMESPACE') or os.environ['NAMESPACE'],
    'feature': 'billing-connections',
    'account_id': lambda: os.environ.get('CORE_ACCOUNT_ID')
}


@dataclass(frozen=True)
class LocalBucketInfo:
    bucket_name: str
    bucket_path: str


@deserializable(schema=Schema({
    Required('name'): str,
    Required('billing_report_format'): BillingReportFormat,
    All(Any('billing_entity', 'cloud_provider'), SetTo('billing_entity')): Maybe(NonEmptyString),
    's3_bucket_info': Maybe(LocalBucketInfo),
    'billing_account_id': Maybe(str),
    'expected_ingest_rate': Maybe(float),
    'metadata': dict,
    'capabilities': dict
}, extra=REMOVE_EXTRA))
@dataclass(frozen=True)
class Tier1Connection:
    name: str
    billing_report_format: BillingReportFormat
    billing_entity: Optional[str] = None
    s3_bucket_info: Optional[LocalBucketInfo] = None
    billing_account_id: Optional[str] = None
    expected_ingest_rate: Optional[float] = None
    metadata: Optional[dict] = None
    capabilities: Optional[dict] = None

    def __post_init__(self):
        if self.metadata is None:
            object.__setattr__(self, 'metadata', {})
        if self.capabilities is None:
            object.__setattr__(self, 'capabilities', {})


@client.invoked(**BILLING_CONNECTIONS_IAPI, api='sync-connections')
def sync_connections(*, accounts: List[CloudAccountInfo]):
    pass


@deprecated(DEPRECATION_MESSAGE)
@client.invoked(**BILLING_CONNECTIONS_IAPI, api='create-connection')
def create_connection(*, cz_organization_id: str, connection_info: Tier1Connection):
    warnings.warn(DEPRECATION_MESSAGE, DeprecationWarning)
    pass


@deprecated(DEPRECATION_MESSAGE)
@client.invoked(**BILLING_CONNECTIONS_IAPI, api='update-connection')
def update_connection(*, cz_organization_id: str, connection_id: str, data: dict):
    warnings.warn(DEPRECATION_MESSAGE, DeprecationWarning)
    pass


@deprecated(DEPRECATION_MESSAGE)
@client.invoked(**BILLING_CONNECTIONS_IAPI, api='delete-connection')
def delete_connection(*, cz_organization_id: str, connection_id: str):
    warnings.warn(DEPRECATION_MESSAGE, DeprecationWarning)
    pass


@deprecated(DEPRECATION_MESSAGE)
@client.invoked(**BILLING_CONNECTIONS_IAPI, api='get-connection')
def get_connection(*, cz_organization_id: str, connection_id: str):
    warnings.warn(DEPRECATION_MESSAGE, DeprecationWarning)
    pass


@deprecated(DEPRECATION_MESSAGE)
@client.invoked(**BILLING_CONNECTIONS_IAPI, api='get-all-connections')
def get_all_connections(*, cz_organization_id: str, include_deleted: Optional[bool] = False) -> Iterable[Mapping]:  # type: ignore
    warnings.warn(DEPRECATION_MESSAGE, DeprecationWarning)
    pass
