# 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
from datetime import datetime
from enum import Enum
from typing import NamedTuple, Optional, List

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

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 BillingReportFormat(Enum):
    aws = 'aws'
    snowflake = 'snowflake'
    common = 'common'


@deserializable()
class CloudAccountInfo(NamedTuple):
    cz_organization_id: str
    cloud_account_id: str
    cloud_provider: str
    role_arn: str
    external_id: str
    bucket_name: str
    bucket_path: str
    provisioning_process: Optional[str]
    is_enabled: bool
    creation_date: Optional[datetime]
    metadata: dict
    billing_report_format: Optional[BillingReportFormat] = None
    cz_account_name: Optional[str] = None


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='create-connection')
def create_connection(*, cz_organization_id: str, connection_info: Tier1Connection):
    pass


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


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


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