# -*- coding: utf-8 -*-
# Copyright : INSEE, 2021

import pandas as pd
import re

from pynsee.macrodata.get_dataset_list import get_dataset_list
from pynsee.macrodata._get_dataset_metadata import _get_dataset_metadata

from pynsee.utils.save_df import save_df


@save_df(day_lapse_max=30)
def get_series_list(*datasets, update=False, silent=False):
    """Download an INSEE's series key list for one or several datasets from BDM macroeconomic database

    Args:
        datasets (str) : datasets should be among the datasets list provided by get_dataset_list()

        update (bool, optional): Set to True, to update manually the metadata
        stored locally on the computer. Defaults to False.

        silent (bool, optional): Set to True, to disable messages printed in log info

    Raises:
        ValueError: datasets should be among the datasets list provided by get_dataset_list()

    Returns:
        DataFrame: contains dimension columns, series keys, dataset name

    Notes:
        Some metadata is stored for 3 months locally on the computer. It is updated automatically

    Examples:
        >>> from pynsee.macrodata import get_dataset_list, get_series_list
        >>> dataset_list = get_dataset_list()
        >>> idbank_ipc = get_series_list('IPC-2015', 'CLIMAT-AFFAIRES')
    """
    insee_dataset = get_dataset_list(silent=silent)
    insee_dataset_list = insee_dataset["id"].to_list()

    if len(datasets) == 1:
        if isinstance(datasets[0], list):
            datasets = datasets[0]

    for dt in datasets:
        if dt not in insee_dataset_list:
            raise ValueError(
                f"\n{dt} is not a dataset from INSEE\nGet a dataset "
                "list with get_dataset_list function"
            )

    idbank_list_dataset = []

    for dt in datasets:
        idbank_list_dt = _get_dataset_metadata(
            dt, update=update, silent=silent
        )

        idbank_list_dataset.append(idbank_list_dt)

    idbank_list = pd.concat(idbank_list_dataset)

    # label columns at the end

    r = re.compile(".*_label_.*")
    column_all = idbank_list.columns.to_list()
    column_label = list(filter(r.match, column_all))
    column_other = [col for col in column_all if col not in column_label]
    new_column_order = column_other + column_label

    idbank_list = pd.DataFrame(idbank_list, columns=new_column_order)

    idbank_list = idbank_list.rename(
        columns={"nomflow": "DATASET", "idbank": "IDBANK", "cleFlow": "KEY"}
    )

    idbank_list.columns = [
        col.replace("-", "_") for col in idbank_list.columns
    ]

    return idbank_list
