#!/usr/bin/env python3
"""podio-merge-files tool to merge any number of podio files into one"""

import argparse
import sys
from tqdm import tqdm

parser = argparse.ArgumentParser(
    description="Merge any number of podio files into one, can merge TTree and RNTuple files"
)

parser.add_argument("-o", "--output-file", help="name of the output file", required=True)
parser.add_argument("files", nargs="+", help="which files to merge")
parser.add_argument(
    "--metadata",
    choices=["none", "all", "first"],
    default="first",
    help="metadata to include in the output file, default: "
    "only the one from the first file, other options: all files, none",
)
parser.add_argument(
    "-c",
    "--compression",
    type=int,
    default=101,
    help="ROOT compression settings for the output file "
    "(<algorithm>*100 + <level>). 1=ZLIB, 2=LZMA, 4=LZ4, 5=ZSTD. "
    "Default: 101 (ZLIB level 1). Pass 0 to use the first input file's compression.",
)
args = parser.parse_args()

# Import podio later for quick help messages
import podio  # pylint: disable=wrong-import-position # noqa: E402
from podio.root_merge import merge_files  # pylint: disable=wrong-import-position # noqa: E402

all_files = set()
for f in args.files:
    if f in all_files:
        raise ValueError(f"File {f} is present more than once in the input list")
    all_files.add(f)

first = args.files[0]

if first.endswith(".root"):
    merge_files(args.files, args.output_file, metadata=args.metadata, compression=args.compression)
elif first.endswith(".sio"):
    # Slow path: frame-by-frame copy
    from podio import reading  # pylint: disable=wrong-import-position # noqa: E402
    from podio import sio_io  # pylint: disable=wrong-import-position # noqa: E402

    reader = reading.get_reader(args.files)
    writer = sio_io.Writer(args.output_file)

    categories = list(reader.categories)
    is_metadata_available = True  # pylint: disable=invalid-name
    try:
        # All frames will be copied as they are except the metadata ones
        categories.remove("metadata")
    except ValueError:
        is_metadata_available = False  # pylint: disable=invalid-name

    for category in tqdm(categories):
        all_frames = reader.get(category)
        for frame in tqdm(all_frames, desc=f"Merging category '{category}'"):
            writer.write_frame(frame, category)

    if args.metadata == "none":
        sys.exit(0)

    if not is_metadata_available:
        print(
            "Warning: metadata category 'metadata' not found in the input files,"
            " it will be created"
        )
        all_frames = [podio.Frame()]
    else:
        if args.metadata == "first":
            all_frames = [reader.get("metadata")[0]]
        else:
            all_frames = reader.get("metadata")
    for frame in all_frames:
        frame.put_parameter("MergedInputFiles", args.files)
        writer.write_frame(frame, "metadata")
else:
    raise ValueError(f"Unsupported file type: {first}. Supported types: .root, .sio")
