#!/usr/bin/env python
import logging
import argparse
import os
import sys
import sdap_ingest_manager.collections_ingester.util
from sdap_ingest_manager.collections_ingester.util import full_path
from sdap_ingest_manager import collections_ingester

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

logger.info(f"using sdap_ingest_manager {sdap_ingest_manager.__version__}")


parser = argparse.ArgumentParser(description="Run ingestion for a list of collection ingestion streams")

parser.add_argument("-c", "--config", help="configuration directory which contains the sdap_ingest_manager.ini file"
                                           "and other configuration files (list of ingestion streams)",
                    default=os.path.join(sys.prefix, ".sdap_ingest_manager"))

options = parser.parse_args()

config = collections_ingester.read_local_configuration(config_path=options.config)


def collection_row_callback(row):

    collections_ingester.collection_row_callback(row,
                                                full_path(config.get("OPTIONS", "collection_config_template")),
                                                full_path(config.get("LOCAL_PATHS", "granule_file_list_path")),
                                                full_path(config.get("LOCAL_PATHS", "collection_config_path")),
                                                full_path(config.get("LOCAL_PATHS", "history_path")),
                                                deconstruct_nfs=True,
                                                job_deployment_template=full_path(config.get("INGEST",
                                                                                             "job_deployment_template")),
                                                connection_settings=full_path(config.get("INGEST",
                                                                                         "connection_config")),
                                                profiles=config.get("INGEST", "connection_profile").split(','),
                                                namespace=config.get("INGEST", "kubernetes_namespace"),
                                                dry_run=config.getboolean("OPTIONS", "dry_run"))


if config.has_option("COLLECTIONS_YAML_CONFIG", "yaml_file"):
    collections_ingester.read_yaml_collection_config(full_path(config.get('COLLECTIONS_YAML_CONFIG', 'yaml_file')),
                                                     collection_row_callback)
elif config.has_option("COLLECTIONS_GOOGLE_SPREADSHEET", "spreadsheet_id"):
    collections_ingester.google_spreadsheet_collection_config.read_google_spreadsheet(config.get('COLLECTIONS_GOOGLE_SPREADSHEET', 'scope'),
                                                                                      config.get("COLLECTIONS_GOOGLE_SPREADSHEET", "spreadsheet_id"),
                                                                                      config.get('COLLECTIONS_GOOGLE_SPREADSHEET', 'sheet_name'),
                                                                                      config.get("COLLECTIONS_GOOGLE_SPREADSHEET", "cell_range"),
                                                                                      collection_row_callback)
else:
    logger.error("no collection configuration found, nothing done")