# 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 typing import NamedTuple, Optional, List, Mapping, Iterable
from deprecated import deprecated

import czc.internal_api.client as client
from czc.type_util import deserializable

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')
}


class LocalBucketInfo(NamedTuple):
    bucket_name: str
    bucket_path: str


@deserializable()
class Tier1Connection(NamedTuple):
    name: str
    billing_report_format: BillingReportFormat
    s3_bucket_info: Optional[LocalBucketInfo] = None
    cloud_provider: Optional[str] = None
    billing_account_id: Optional[str] = None
    expected_ingest_rate: Optional[float] = None
    metadata: dict = {}
    capabilities: dict = {}


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


@client.invoked(**BILLING_CONNECTIONS_IAPI, api='sync-organization-connections')
def sync_organization_connections(*, cz_organization_id: str, 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
