# ----------------------------------------------------------------------------
# Copyright (c) 2026, QIIME 2 development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------
import os
import subprocess
from typing import List, Union

import pandas as pd
import skbio
from q2_types.feature_data import DNASequencesDirectoryFormat

from q2_types.feature_data_mag import MAGSequencesDirFmt
from q2_types.per_sample_sequences import MultiMAGSequencesDirFmt, ContigSequencesDirFmt

EXTERNAL_CMD_WARNING = (
    "Running external command line application(s). "
    "This may print messages to stdout and/or stderr.\n"
    "The command(s) being run are below. These commands "
    "cannot be manually re-run as they will depend on "
    "temporary files that no longer exist."
)


def run_command(cmd, env=None, verbose=True, pipe=False, **kwargs):
    if verbose:
        print(EXTERNAL_CMD_WARNING)
        print("\nCommand:", end=" ")
        print(" ".join(cmd), end="\n\n")

    if pipe:
        result = subprocess.run(
            cmd, env=env, check=True, capture_output=True, text=True
        )
        return result

    if env:
        subprocess.run(cmd, env=env, check=True, **kwargs)
    else:
        subprocess.run(cmd, check=True, **kwargs)


def _construct_param(arg_name):
    """Converts argument name into a command line parameter."""
    return f'--{arg_name.replace("_", "-")}'


def _process_common_input_params(processing_func, params: dict) -> List[str]:
    """Converts provided arguments and their values.

    Conversion is entirely dependent on the passed 'processing_func'
    that processes individual arguments. The output is a list of
    parameters with their values that can be directly passed to the
    respective command.

    Arguments without any value are skipped.
    Any other argument is processed using the 'processing_func' and
    appended to the final list.

    Args:
        processing_func: Function to be used for formatting a single argument.
        params (dict): Dictionary of parameter: value pairs to be processed.

    Returns:
        processed_args (list): List of processed arguments and their values.

    """
    processed_args = []
    for arg_key, arg_val in params.items():
        # This if condition excludes arguments which are falsy
        # (False, None, "", []), except for integers and floats.
        if type(arg_val) == int or type(arg_val) == float or arg_val:  # noqa: E721
            processed_args.extend(processing_func(arg_key, arg_val))

    return processed_args


def colorify(string: str):
    return "%s%s%s" % ("\033[1;32m", string, "\033[0m")


def get_feature_lengths(
    features: Union[
        MAGSequencesDirFmt,
        MultiMAGSequencesDirFmt,
        ContigSequencesDirFmt,
        DNASequencesDirectoryFormat,
    ],
) -> pd.DataFrame:
    """Calculate lengths of features in a feature data object."""
    ids, lengths = [], []
    per_sequence = False

    # for FeatureData[Sequence]
    if isinstance(features, DNASequencesDirectoryFormat):
        sample_dict = {"": {"": os.path.join(str(features), "dna-sequences.fasta")}}
        per_sequence = True

        # For SampleData[Contigs]
    elif isinstance(features, ContigSequencesDirFmt):
        sample_dict = {"": features.sample_dict()}
        per_sequence = True

    # For SampleData[MAGs]
    elif isinstance(features, MultiMAGSequencesDirFmt):
        sample_dict = features.sample_dict()

    # For FeatureData[MAG]
    else:
        sample_dict = {"": features.feature_dict()}

    for _, file_dict in sample_dict.items():
        for _id, fp in file_dict.items():
            sequences = skbio.io.read(fp, format="fasta", verify=False)

            if per_sequence:
                for seq in sequences:
                    ids.append(seq.metadata["id"])
                    lengths.append(len(seq))
            else:
                ids.append(_id)
                lengths.append(sum(len(seq) for seq in sequences))

    df = pd.DataFrame({"id": ids, "length": lengths})
    df.set_index("id", inplace=True)
    return df
