import os
from typing import Optional

from great_expectations.data_context.types.base import (
    DataContextConfig,
    DatasourceConfig,
    S3StoreBackendDefaults,
    FilesystemStoreBackendDefaults,
)
from great_expectations.data_context import BaseDataContext

from psi_great_expectations import log_it


@log_it
def build_data_context_config(
    datasource_name: str,
    s3_bucket_name: Optional[str] = None,
    s3_prefix: Optional[str] = None,
    base_public_path: Optional[str] = None,
    file_type: str = "json",
):
    """
    builds and returns a data context object for profilers and validators,
    it defines where the produced files from either are written.
    """
    connectors = {
        "default_runtime_data_connector_name": {
            "class_name": "RuntimeDataConnector",
            "batch_identifiers": ["default_identifier_name"],
        }
    }

    if s3_bucket_name:

        connectors["default_inferred_data_connector_name"] = {
            "class_name": "InferredAssetS3DataConnector",
            "bucket": s3_bucket_name,
            # "prefix": s3_prefix,
            "default_regex": {"pattern": f"(.*)\.{file_type}", "group_names": ["data_asset_name"]},
        }
        # cp_store_name="checkpoints_s3_store"
        # val_store_name="validations_s3_store"
        # exp_store_name="expectations_s3_store"
        # prof_store_name="profiler_s3_store"
        backend = S3StoreBackendDefaults(
            default_bucket_name=s3_bucket_name  # ,
            # expectations_store_prefix = "expectations",
            # validations_store_prefix = "validations",
            # data_docs_prefix = "great_expectations/data_docs",
            # checkpoint_store_prefix = "checkpoints",
            # profiler_store_prefix = "profilers",
            # expectations_store_name = "expectations_S3_store",
            # validations_store_name = "validations_S3_store",
            # evaluation_parameter_store_name = "evaluation_parameter_store",
            # checkpoint_store_name = "checkpoint_S3_store",
            # profiler_store_name = "profiler_S3_store"
        )
    else:
        connectors["default_inferred_data_connector_name"] = {
            "class_name": "InferredAssetFilesystemDataConnector",
            "base_directory": f"{os.getcwd()}/ge_to_profile/",
            "glob_directive": f"*.{file_type}",
            "default_regex": {
                "pattern": f"(.*)\.{file_type}",  # TODO: needs to target the desired file f"/^{datasource_name}_to_profile.{file_type}$/"#
                "group_names": ["data_asset_name"],
            },
        }
        backend = FilesystemStoreBackendDefaults(root_directory=f"{os.getcwd()}/great_expectations")
        # cp_store_name="checkpoints_local_store"
        # val_store_name="validations_local_store"
        # exp_store_name="expectations_local_store"
        # prof_store_name="profiler_local_store"

    # backend = FilesystemStoreBackendDefaults(root_directory=f"{os.getcwd()}/great_expectations")
    data_docs_sites = {
        "s3_site": {
            "class_name": "SiteBuilder",
            "show_how_to_buttons": True,
            "store_backend": {"class_name": "TupleS3StoreBackend", "bucket": s3_bucket_name},
            "site_index_builder": {"class_name": "DefaultSiteIndexBuilder"},
        }
    }

    if base_public_path:
        data_docs_sites["s3_site"]["store_backend"]["base_public_path"] = base_public_path
    if s3_prefix:
        data_docs_sites["s3_site"]["store_backend"]["prefix"] = s3_prefix

    data_context_config = DataContextConfig(
        config_version=2,
        datasources={
            datasource_name: DatasourceConfig(
                class_name="Datasource",
                execution_engine={"class_name": "PandasExecutionEngine"},
                data_connectors=connectors,
            )
        },
        # stores = {
        #     "expectations_local_store": {
        #         "class_name": "ExpectationsStore",
        #         "store_backend": {
        #             "class_name": "TupleFilesystemStoreBackend",
        #             "base_directory": f'{local_backend}/expectations'
        #         }
        #     },
        #     "expectations_s3_store": {
        #         "class_name": "ExpectationsStore",
        #         "store_backend": {
        #             "class_name": "TupleFilesystemStoreBackend",
        #             "base_directory": f'{local_backend}/expectations'
        #         }
        #     },
        #     "validations_local_store": {
        #         "class_name": "ValidationsStore",
        #         "store_backend": {
        #             "class_name": "TupleFilesystemStoreBackend",
        #             "base_directory": f'{local_backend}/validations'
        #         }
        #     },
        #     "validations_s3_store": {
        #         "class_name": "ValidationsStore",
        #         "store_backend": {
        #             "class_name": "TupleS3StoreBackend",
        #             "bucket": s3_bucket_name,
        #             "prefix": f'{s3_prefix}/validations',
        #         }
        #     },
        #     "checkpoints_local_store": {
        #         "class_name": "CheckpointStore",
        #         "store_backend": {
        #             "class_name": "TupleFilesystemStoreBackend",
        #             "base_directory": f'{local_backend}/checkpoints'
        #         }
        #     },
        #     "checkpoints_s3_store": {
        #         "class_name": "CheckpointStore",
        #         "store_backend": {
        #             "class_name": "TupleS3StoreBackend",
        #             "bucket": s3_bucket_name,
        #             "prefix": f'{s3_prefix}/checkpoints'
        #         }
        #     },
        #     "profiler_local_store": {
        #         "class_name": "ProfilerStore",
        #         "store_backend": {
        #             "class_name": "TupleFilesystemStoreBackend",
        #             "suppress_store_backend_id": True,
        #             "base_directory": f'{local_backend}/profiler'
        #         }
        #     },
        #     "profiler_s3_store": {
        #         "class_name": "ProfilerStore",
        #         "store_backend": {
        #             "class_name": "TupleS3StoreBackend",
        #             "bucket": s3_bucket_name,
        #             "prefix": f'{s3_prefix}/profiler'
        #         }
        #     },
        #     "evaluation_parameter_store": {
        #         "class_name": "EvaluationParameterStore"
        #     }
        # },
        # expectations_store_name = exp_store_name,
        # validations_store_name = val_store_name,
        # evaluation_parameter_store_name = "evaluation_parameter_store",
        # checkpoint_store_name = cp_store_name,
        # profiler_store_name = prof_store_name,
        data_docs_sites=data_docs_sites,
        # validation_operators={
        #     "action_list_operator": {
        #         "class_name": "ActionListValidationOperator",
        #         "action_list": [
        #             {
        #                 "name": "store_validation_result",
        #                 "action": {"class_name": "StoreValidationResultAction"},
        #             },
        #             {
        #                 "name": "store_evaluation_params",
        #                 "action": {"class_name": "StoreEvaluationParametersAction"},
        #             },
        #             {
        #                 "name": "update_data_docs",
        #                 "action": {"class_name": "UpdateDataDocsAction"},
        #             },
        #         ],
        #     }
        # },
        # anonymous_usage_statistics={
        # "enabled": False
        # }
        store_backend_defaults=backend,
    )
    return BaseDataContext(project_config=data_context_config)
